0
我在select2中嘗試了initSelection
以在我的多選框中加載一些預選數據。用戶可以在創建項目時選擇多個選項,當他/她試圖再次編輯該項目時,他/她可以看到他/她選擇的所有選定值並從選擇中刪除/添加值。Select2預先填充的數據值在表單提交時顯示爲空
它工作正常,並加載所有預選的選項。它也將它們顯示在多個選擇框中。但是,當我提交我的表單選擇輸入具有空值。
這裏是我的腳本
$('.select2').select2({
minimumInputLength: 2,
"language": {
"noResults": function(){
return "{{Lang::get('lang.no-department-found')}}";
},
searching: function() {
return "{{Lang::get('lang.searching')}}";
},
inputTooShort: function(args) {
// args.minimum is the minimum required length
// args.input is the user-typed text
var remaining = args.minimum - args.input.length;
return "{{Lang::get('lang.please-enter')}} "+remaining+" {{Lang::get('lang.or-more-character')}}";
},
},
ajax: {
url: "{{URL::route('api-get-department-names')}}",
dataType: 'json',
delay: 250,
type: "GET",
quietMillis: 50,
data: function (params) {
return {
name: params.term, // search term
page: params.page
};
},
processResults: function (data) {
return {
results: data
};
},
cache: true
},
initSelection: function(element, callback) {
var id;
id = $(element).val();
if (id !== "") {
$.ajax({
url: "{{URL::route('get-canned-department', $canned->id)}}",
type: "GET",
dataType: "json",
}).done(function(data) {
callback(data);
});;
}
}
});
HTML
<select class="form-control select2" name="d_id[]" multiple="multiple" data-placeholder="{!! Lang::get('lang.enter-department-name') !!}" style="width: 50%;" disabled="true">
</select>
按照鏈接查看選擇框的選項,它是一個空值。 https://drive.google.com/file/d/0B_JQE_eICBj6elpRYVZhU2w5eTA/view?usp=sharing
另外,當我嘗試刪除一個預加載選項時,它會從選擇框中刪除所有預加載的選項。
請幫我設置select2中預填充選項的值並處理刪除值選項。 TIA