Easyui Datagrid 的Combobox 如何動態修改下拉選項,以及值的轉換


我是先將下拉選項的值通過datagrid的url查出來了,在每一行的row中

//項目結果選項卡的列表
    $('#project_table').datagrid({
        width : '100%',
        height: '378',
        url : 'getSeparationProjectInf',
        //title : '待分發條碼列表',
        striped : true,
        nowrap : true,
        rownumbers : true,
        singleSelect : true,
        showHeader : true,
        showFooter : false,
        loadMsg : '努力展開中...',
        scrollbarSize:0,
        fitColumns : true,
        checkOnSelect : true,
        onClickRow: function (rowIndex, rowData) {
            //$(this).datagrid('unselectRow', rowIndex);
            var _this = this;
            if (editIndex != undefined && editIndex != rowIndex) {
                //結束行編輯
                endEdit(_this,editIndex);
            }
            $(_this).datagrid('beginEdit', rowIndex);
            $("input.datagrid-editable-input").on("keypress",function(e){
                if(e.keyCode==13){
                    endEdit(_this,editIndex);
                }
            });
            editIndex = rowIndex;            
        },
        onBeginEdit: function(index, rowData){ //動態修改檢測結果下拉項
            var resultDictionaryDetailList = rowData.resultDictionaryDetailList
            //監測結果下拉框  
            var goEditor = $('#project_table').datagrid('getEditor', {  
                      index : index,    
                       field : 'result'    
            });  
            $(goEditor.target).combobox({  
                onLoadSuccess: function () {
                    if(rowData.result){
                        $(goEditor.target).combobox('setValue', rowData.result);
                    }else{
                        $(goEditor.target).combobox('setValue', rowData.resultDictionaryDetailList[0].id);
                    }
                },
                onBeforeLoad: function(){   //下拉展開時動態修改options  
                    if(resultDictionaryDetailList.length){
                        var data = [];  
                        for ( var index in resultDictionaryDetailList) {
                            var resultDictionaryDetail = resultDictionaryDetailList[index];
                            data.push({'id': resultDictionaryDetail.id, 'name':resultDictionaryDetail.name});  
                        }
                        $(goEditor.target).combobox("loadData", data);  
                    }                 
                }  
            });  
        },
        columns : [[
            {
                field : 'ck',
                checkbox: true
            },
            {
                field : 'projectId',
                title : '項目ID',
                align : 'center',
                sortable : false,
                resizable : false,
                hidden: true
            },
            {
                field : 'projectName',
                title : '項目',
                align : 'center',
                sortable : false,
                resizable : false,
                width : 120                 
            },
            {
                field : 'data',
                title : '檢測數據',
                align : 'center',
                sortable : false,
                resizable : false,
                width : 100,
                editor:{
                    type:'text'
                }                                                
            },            
            {
                field : 'result',
                title : '檢測結果',
                align : 'center',
                sortable : false,
                resizable : false,
                width : 100,
                formatter: function (value, rowData, rowIndex) {
                    var resultDictionaryDetailList = rowData.resultDictionaryDetailList;
                    if(!value){
                        rowData.result = resultDictionaryDetailList[0].id;
                        value = resultDictionaryDetailList[0].name;
                    }
                    console.log("resultDictionaryDetailList.length=="+resultDictionaryDetailList.length);
                    for (var i = 0; i < resultDictionaryDetailList.length; i++) {  
                        if (resultDictionaryDetailList[i].id == value) {  
                            return resultDictionaryDetailList[i].name;  
                        }  
                    }  
                    return value;  
                },
                editor:{
                    type:'combobox',
                    options:{
                        valueField:'id',
                        textField:'name',
                        //method:'get',
                        //url:'products.json',
                        //data: resultDictionaryDetailList,
                        data: [{
                            productid: '0',
                            checkResult: '高'
                        },{
                            productid: '1',
                            checkResult: '低'
                        }],                     
                        required:true
                    }
                }
            },
            {
                field : 'remark',
                title : '結果備注',
                align : 'center',
                sortable : false,
                resizable : false,
                width : 120 ,
                editor : {type:"text"}               
            },
            {
                field : 'suggestion',
                title : '建議內容',
                align : 'center',
                sortable : false,
                resizable : false,
                width : 150,
                editor:{ type:'text' }
            },            
            {
                field : 'explanation',
                title : '醫學解釋',
                align : 'center',
                sortable : false,
                resizable : false,
                width : 150,
                editor:{ type:'text' }
            },
            {
                field : 'reason',
                title : '常見原因',
                align : 'center',
                sortable : false,
                resizable : false,
                width : 150,
                hidden: true
            },
            {
                field : 'operate',
                title : '操作',
                align : 'center',
                sortable : false,
                resizable : false,
                width : 80,
                formatter: function(value,row,index){
//                    return '<a href="javascript:void(0);" onclick="cancelDialog(event)">'+value+'</a>';
                }                
            }              
        ]],
        data : [
            /*{
                project : 'ERCC1基因表達',
                checkData : '≥3.4%',
                checkResult : '高',
                resultRemark : 'xxxxx',
                advise : '您患2型糖尿病的基因位.....',
                medicalExplanation : '合理安排休息,保證充分....',
                operate : '刪除',                              
            }*/
        ],
        pagination : false,
        /*pageSize : 10,
        pageList : [10],
        pageNumber : 1,*/
        pagePosition : 'bottom',
        remoteSort : false,
    });

具體解析如下:

onBeginEdit是將row中的下拉項數據拿出來並動態加載到Combobox 中,onBeforeLoad很重要,不然執行onLoadSuccess方法的時候,Combobox還沒有動態加載下拉選項,導致顯示的是值,而不是值對應的名,所以用onBeforeLoad可以先加載Combobox的下拉選項,然后再回填值

 
         
onBeginEdit: function(index, rowData){ //動態修改檢測結果下拉項
            var resultDictionaryDetailList = rowData.resultDictionaryDetailList
            //監測結果下拉框  
            var goEditor = $('#project_table').datagrid('getEditor', {  
                      index : index,    
                       field : 'result'    
            });  
            $(goEditor.target).combobox({  
                onLoadSuccess: function () {
                    if(rowData.result){
                        $(goEditor.target).combobox('setValue', rowData.result);
                    }else{
                        $(goEditor.target).combobox('setValue', rowData.resultDictionaryDetailList[0].id);
                    }
                },
                onBeforeLoad: function(){   //下拉展開時動態修改options  
                    //datatype處理統計方法  
                    if(resultDictionaryDetailList.length){
                        var data = [];  
                        for ( var index in resultDictionaryDetailList) {
                            var resultDictionaryDetail = resultDictionaryDetailList[index];
                            data.push({'id': resultDictionaryDetail.id, 'name':resultDictionaryDetail.name});  
                        }
                        $(goEditor.target).combobox("loadData", data);  
                    }  
                   /* //設置值                              
                    if(rowData.result){
                        $(goEditor.target).combobox('setValue', rowData.result);  
                    }else{
                        $(goEditor.target).combobox('setValue', rowData.resultDictionaryDetailList[0].name);
                    }*/                      
                }  
            });  
        },
 

formatter是將值轉換成對應的name

formatter: function (value, rowData, rowIndex) { 
    var resultDictionaryDetailList = rowData.resultDictionaryDetailList;
    if(!value){
        value = resultDictionaryDetailList[0].id;
        rowData.result = value;
    }
    for (var i = 0; i < resultDictionaryDetailList.length; i++) {  
        if (resultDictionaryDetailList[i].id == value) {  
            return resultDictionaryDetailList[i].name;  
        }  
    }  
    return value;  
},

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM