easyui datagrid editor checkbox 選中

Love丶蘭發表於2016-12-19

easyui datagrid editor checkbox 選中


在前臺如果使用easyui的datagrid
需要編輯行時遇到checkbox無法還原選中事件時
easyUI的datagrid中,列的checkbox這樣設定:
{field:'status',title:'Status',width:50,align:'center',  
    editor:{  
        type:'checkbox',  
        options:{  
            on: "true",  
            off: "false"  
        }  
    }  
},  

資料:
{"total":28,"rows":[  
    {"productid":"FI-SW-01","unitcost":10.00,"status":true,"listprice":16.50,"attr1":"Large","itemid":"EST-1"},  
    {"productid":"K9-DL-01","unitcost":12.00,"status":true,"listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},  
    {"productid":"RP-SN-01","unitcost":12.00,"status":true,"listprice":18.50,"attr1":"Venomless","itemid":"EST-11"}  
]}  

這樣子就有個問題:

當datagrid裡的行處於編輯狀態時,checkbox並未能自動帶上應有的值,比如本來對應是true,但當checkbox出現時,並沒有自動勾選!造成很不好的後果:編輯一次,如果不手動對這個checkbox再打鉤一次,那麼原本是true,現在就變成了 false !什麼毛病。

對應辦法是將資料裡的布林值變為字串:"status":true ==> "status":"true"

{"total":28,"rows":[  
    {"productid":"FI-SW-01","unitcost":10.00,"status":"true","listprice":16.50,"attr1":"Large","itemid":"EST-1"},  
    {"productid":"K9-DL-01","unitcost":12.00,"status":"true","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},  
    {"productid":"RP-SN-01","unitcost":12.00,"status":"true","listprice":18.50,"attr1":"Venomless","itemid":"EST-11"}  
]}  

如果無法更改資料來源,但是又需要還原時,就需要這樣做了:
{field:'status',title:'Status',width:50,align:'center',  
    editor:{  
        type:'checkbox',  
        options:{  
            on: "1",  
            off: "0"  
        }  
    }  
},  

這樣就完美的解決了你的問題

當然,如果你的資料  本身就是加了引號的"true"和"false"

那就只需要向最上面那樣就行了..



相關文章