$(document).ready(function(){ //stert of select2 $('.global-select2').each(function(){ var urlglobal = base_url + '/fetch/globalfetch'; var id = $(this).attr('id'); // ID dari element yang di definisikan sebagai select 2 var link = $(this).attr('link') == 'globalfetch' ? urlglobal : $(this).attr('link'); // link yang ditembak untuk diambil oleh select 2 var t = $(this).attr('t'); // t = table(penyamaran) ,hanya jika menggunakan global link var s = $(this).attr('s'); // s = select(penyamaran) ,hanya jika menggunakan global link var placeholder = $(this).attr('placeholder') != '' ? $(this).attr('placeholder') : 'choose the option'; // placeholder var allowclear = $(this).attr('required') ? false : true; // untuk menentukan dia boleh di clear atau tidak var tag = $(this).attr('tags') == true ? true : false; var maxselect = (typeof $(this).attr('maxs') !== "undefined") ? (($(this).attr('maxs') != '') ? $(this).attr('maxs') : 1000) : 1000; //untuk memberi rule , maksimal value yg di select hanya sejumlah berapa var exception = (typeof $(this).attr('exception') !== "undefined") ? $(this).attr('exception') : ''; var onlyin = (typeof $(this).attr('onlyin') !== "undefined") ? $(this).attr('onlyin') : ''; var w = new Array(); var exceptid = exception != '' ? getvaluefrom(exception) : ''; var onlyinid = onlyin != '' ? getvaluefrom(onlyin) : ''; var indextones = 'select-index-1'; if (typeof link === "undefined") { link = '#'; } if (typeof placeholder === "undefined") { placeholder = 'choose the option'; } if (typeof s === "undefined") { w = []; } else { if (s != '' && typeof s.split(',')[1] === 'undefined') { indextones = 'select-index-1'; }else{ indextones = s.split(',')[1]; } var w = [ {field:indextones , operator:'LIKE' , value:'-NMSearch-'} //NMSearch untuk di replace dengan keyword ]; } var parameters = { 'q': '', 't': t, 's': s, 'w': w, }; // if (destroy == true) { // $('#'+id).select2('destroy'); // }else{ if (link != '#') { $('#' + id).select2({ tokenSeparators: [','], allowClear: allowclear, theme: 'bootstrap4', placeholder: placeholder, maximumSelectionLength: maxselect, ajax: { url: link, dataType: 'json', type: 'post', data: function(params) { if (exception != '') { // $('#'+id).on('select2:opening',function(e){ exceptid = getvaluefrom(exception); // }) } if (onlyin != '') { // $('#'+id).on('select2:opening',function(e){ onlyinid = getvaluefrom(onlyin); // }) } return { "_token": $('meta[name="csrf-token"]').attr('content'), q: params.term, // search term page: params.page, parameter: parameters, except: exceptid, onlyin: onlyinid, }; }, processResults: function(data, params) { return { results: data.item } }, cache: true }, tags: tag, tokeSparator: [','], escapeMarkup: function(markup) { return markup; }, // let our custom formatter work minimumInputLength: 0, templateResult: formatResult, // omitted for brevity, see the source of this page templateSelection: formatResult // omitted for brevity, see the source of this page }); } else { $('#' + id).select2({ allowClear: allowclear, theme: 'bootstrap4', placeholder: placeholder, }); } // } }); function formatResult(result) { return result.text; } //end of select2 });