私はあなたがするようにselect2をdataInit
関数で直接使用しようとします(違いは私がajaxデータソースを使用することです)、見た目は良いですが正しく動作しません:
- 値をサーバーに送信できません。
- 1行を選択 - >編集しますが、select2は古い値で初期化されません。
最後に、私はこの方法をあきらめて使用しようとしましたedittype:'custom'
colModel:[
{name:'model_id',label:'mID',
hidden: true,
hidedlg: true, // the hidden model_id column
search: false, // used for offer id info to select2
},
{name:'model',label:'model',width:150,
editable:true,
edittype:'custom',
editoptions:{
custom_element: myselect2elem,
custom_value: myselect2value,
dataInit: myselect2init('180','chose model','search/servermodel','model_id','model'),
},
editrules:{
edithidden: true,
required: true,
},
},
3 つの関数を定義しました。
- myselect2elem(value, options) // 共通要素を初期化
- myselect2value(elem, operation, value) // 要素の値を取得|設定
- myselect2init(width,holder,url,opt,cel_name_id,cel_name) // 初期化要素で select2 を使用
詳細
function myselect2elem(value, options) {
var el = document.createElement("select");
el.value = value;
return el;
}
function myselect2value(elem, operation, value) {
if(operation === 'get') {
return $(elem).val();
} else if(operation === 'set') { // this doesn't work,
$(elem).value=value; // I don't know whether have side effect
}
}
function myselect2init(width,holder,url,cel_name_id,cel_name){
var option = {
width: width,
placeholder: holder,
ajax: {
url: url,
/*
the other ajax configuration option that only selet2 used
*/
},
}
return function(element) {
$(element).children(".customelement").select2(option)
// do the init 'set' operation that previous function should do
selRowId = $(this).jqGrid ('getGridParam', 'selrow'),
celId = $(this).jqGrid ('getCell', selRowId, cel_name_id);
celValue = $(this).jqGrid ('getCell', selRowId, cel_name);
if (celValue){
var newOption = new Option(celValue, celId, true, true);
$(element).children(".customelement").append(newOption).trigger('change');
}
}
}
これについてご質問いただきありがとうございます。@Oleg の回答に感謝します。私の答えが他の人を助けることができるようにしたい