我正在尝试使用select2来加载以json格式返回字符串的查询结果。 我在select2中加载了数据,但是在点击时我没有得到选择器的工作方式。
这里是代码:
$(".js-data-example-ajax").select2({
ajax: {
url: "{{ path("ajax_search_client") }}",
dataType: 'json',
delay: 250,
data: function (params) {
return {
data: params.term, // search term
page: params.page
};
},
processResults: function (data, params) {
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used
params.page = params.page || 1;
obj = JSON.parse(data);
console.log(obj.length);
return {
results: obj,
pagination: {
more: (params.page * 30) < obj.length
}
};
},
cache: true
},
placeholder: 'Buscar un cliente',
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
templateResult: formatRepo,
templateSelection: formatRepoSelection
});
function formatRepo (repo) {
if (repo.loading) {
return repo.text;
}
console.log(repo);
var client_data = repo.split('"');
console.log(client_data);
var item = "'"+client_data[3]+"'";
var markup = '<div class="select2-result-repository clearfix" onClick="tradename(' + item + ')">' +
'<div class="select2-result-repository__avatar"><img src="" /></div>' +
'<div class="select2-result-repository__meta">' +
'<div class="select2-result-repository__title"><strong>' + client_data[3] + '</strong></div>';
/*if (repo.description) {
markup += "<div class='select2-result-repository__description'>" + + "</div>";
}*/
markup += '<div class="select2-result-repository__statistics">' +
'<div class="select2-result-repository__forks"> ' + client_data[7] + '</div>' +
'<div class="select2-result-repository__stargazers"> ' + client_data[11] + '</div>' +
'</div>' +
'</div></div>';
return markup;
}
function formatRepoSelection (repo) {
return repo.full_name || repo.text;
}
数据内容:
[“{\”“tradename \”:\“pascual \”,\“fiscalname \”:\“pascual sl \”,\“nif \” :\“4568424 \”},“,”{\“商品名\”:\“Jacinto \”,\“fiscalname \”:\“Jacinto sl \“,\”nif \“:\”8764564 \“},”,“{”商品名“ :\“Calzapeques \”,\“financialname \”:\“Calzapeques sl \”,\“nif \” :\ “4568753 \”},“]
obj内容:
[“{”商号“:”pascual“,”financialname“:”pascual sl“,”nif“ :“4568424”},“,”{“商号”:“Jacinto”,“财政名称”:“Jacinto sl“,”“nif”:“8764564”},“,”{“商号”:“Calzapeques”,“财政名称” :“Calzapeques sl”,“nif”:“4568753”},“]