gpt4 book ai didi

javascript - select2 - 将获取远程数据与多个选择和预数据相结合

转载 作者:数据小太阳 更新时间:2023-10-29 04:21:34 24 4
gpt4 key购买 nike

您好,我正在寻找使用 select 2 的方法,到目前为止我所看到的看起来不错。不过,我正在尝试做一件事。

我希望通过对 json 文件的 ajax 调用来获取我的数据 - 他们的网站上有一个关于如何执行此操作的示例,但我正在尝试有一个预填充列表。

我的意思是,例如,当用户点击在此链接上搜索电影时

http://ivaynberg.github.io/select2/#infinite

列出了 json 文件中的前 10 部电影,因此有一些预先选择。

谁能指出我正确的选择

到目前为止,这是我的代码

function movieFormatResult(movie) {
var markup = "<table class='movie-result'><tr>";
if (movie.posters !== undefined && movie.posters.thumbnail !== undefined) {
markup += "<td class='movie-image'><img src='" + movie.posters.thumbnail + "'/></td>";
}
markup += "<td class='movie-info'><div class='movie-title'>" + movie.title + "</div>";
if (movie.critics_consensus !== undefined) {
markup += "<div class='movie-synopsis'>" + movie.critics_consensus + "</div>";
}
else if (movie.synopsis !== undefined) {
markup += "<div class='movie-synopsis'>" + movie.synopsis + "</div>";
}
markup += "</td></tr></table>"
return markup;
}

function movieFormatSelection(movie) {
return movie.title;
}

$(document).ready(function() {
$("#e7").select2({
placeholder: "More",
minimumInputLength: 3,
ajax: {
url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
dataType: 'jsonp',
quietMillis: 100,
data: function (term, page) { // page is the one-based page number tracked by Select2
return {
q: term, //search term
page_limit: 10, // page size
page: page, // page number
apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
};
},
results: function (data, page) {
var more = (page * 10) < data.total; // whether or not there are more results available

// notice we return the value of more so Select2 knows if more results can be loaded
return {results: data.movies, more: more};
}
},
formatResult: movieFormatResult, // omitted for brevity, see the source of this page
formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
multiple: true,
escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
});
});

和html

    <article class="row" id="infinite">
<div class="span12">

<p>
<input type="hidden" class="bigdrop" id="e7" style="width:200px"/>



</p>

</div>
</article>

最佳答案

您可以将 minimumInputLength 参数设置为 0,然后它将尝试查询没有搜索值的 URL。然后设置您的服务器响应以在没有搜索字符串时返回 10 个选项。

关于javascript - select2 - 将获取远程数据与多个选择和预数据相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16826442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com