Can't select item with select2 V4

398 views
Skip to first unread message

fire...@gmail.com

unread,
Jun 11, 2015, 5:49:07 AM6/11/15
to sel...@googlegroups.com
the code :
<div class="col-xs-7">
<select class="js-data-example-ajax form-group col-xs-12">
</select>
</div>

<script type="text/javascript">
var formatRepo =function (repo){
if (repo.loading) return repo.text;
var markup = repo.name +'-'+repo.natrualkey;
return markup;
}

var formatRepoSelection =function (repo){
return repo.name||repo.natrualkey;
}

$(function () {
$('.js-data-example-ajax').select2({
// placeholder:"选择供应商...",
// multiple: true,
ajax: {
url: "../../../system/customer/select",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: 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;
return {
results: data.items,
pagination: {
more: (params.page * 30) < data.total_count
}
};
}
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
templateResult: formatRepo,
templateSelection: formatRepoSelection
});
})
</script>
QQ截图20150611174757.png

Kevin Brown

unread,
Jun 12, 2015, 10:47:47 PM6/12/15
to sel...@googlegroups.com
Considering you appear to be using a different property for the text of your results, you most likely aren't using the `id` property for the identifier.

It's required in results, and without it you will not able to select the result. The link (also in the 4.0 announcement) provides a method that will allow you to re-map the field on the frontend if you cannot do it in your api.

--
You received this message because you are subscribed to the Google Groups "select2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to select2+u...@googlegroups.com.
To post to this group, send email to sel...@googlegroups.com.
Visit this group at http://groups.google.com/group/select2.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages