Memberi Nilai Default Select2 yang Menggunakan Ajax (initSelection)

Berikut salah satu contohnya :

<script type="text/javascript">

jQuery(function(){

$("#fk_uzer").select2({

initSelection: function(element, callback) {

var data = {"id":"<?php echo $penilai?$penilai->getIdPenilai():""?>","nama":"<?php echo $penilai?$penilai->T_UZER->getNama():""?>"} ;

callback(data);

},

placeholder: "Pilih User",

allowClear:true,

multiple:false,

minimumInputLength: 3,

ajax: {

url: "<?php echo url_for("@event_admin_pelatihan?act=get_user_jsonp");?>",

dataType: 'jsonp',

data: function (term, page) {

return {

q: term,

page_limit: 10,

};

},

results: function (data, page) {

return {results: data.user};

}

},

formatResult: userFormatResult, // omitted for brevity, see the source of this page

formatSelection: userFormatSelection, // omitted for brevity, see the source of this page

}) ;

});

function userFormatResult(user) {

var markup = "<span>" ;

markup += user.nama ;

markup += "</span>";

return markup ;

}

function userFormatSelection(user) {

return user.nama ;

}

</script>

Contoh lain bisa dilihat di [1]

Referensi