詳解EasyUi控件中的Datagrid
最近手頭有個(gè)web項(xiàng)目需要用到第三方控件(EasyUi),用第三方控件做出來的效果畢竟比原生態(tài)的要稍微好看那么一點(diǎn),該項(xiàng)目中有個(gè)需求,需要在數(shù)據(jù)列表中直接編輯數(shù)據(jù)保存,行話叫做行內(nèi)編輯。
在講行內(nèi)編輯之前,我們需要先了解如何使用EasyUi創(chuàng)建一個(gè)DataGrid,當(dāng)然方式有很多(1.easyui.js,或者直接html代碼加easyui的Style),我采用的是JS的方式:
一、使用Js創(chuàng)建DataGrid
上面是效果圖,
Html代碼如下:在頁面定義一個(gè)table
<!--數(shù)據(jù)展示 --> <div> <table id="DataGridInbound"></table> </div>
Js代碼如下:
有幾個(gè)我自己認(rèn)為比較重要的屬性在此標(biāo)記下
url:這里是datagrid獲取數(shù)據(jù)集的地址,就是你的Action,需要注意的是,你的Action需要返回Json格式的數(shù)據(jù)。
pagination:設(shè)置datagrid是否分頁顯示
queryParams:你的查詢條件參數(shù)
formatter:格式化,在日期列用到了,EasyUi的datagrid顯示日期如果不格式話,日期會亂顯示
這些屬性在EasyUi的官網(wǎng)都有詳細(xì)介紹,我就不深入解釋了。
$("#DataGridInbound").datagrid({ title: '入庫詳情', idField: 'Id', rownumbers: 'true', url: '/Inbound/GetPageInboundGoodsDetail', pagination: true,//表示在datagrid設(shè)置分頁 rownumbers: true, singleSelect: true, striped: true, nowrap: true, collapsible: true, fitColumns: true, remoteSort: false, loadMsg: "正在努力加載數(shù)據(jù),請稍后...", queryParams: { ProductName: "", Status: "",SqNo:"" }, onLoadSuccess: function (data) { if (data.total == 0) { var body = $(this).data().datagrid.dc.body2; body.find('table tbody').append('<tr><td width="' + body.width() + '" style="height: 35px; text-align: center;"><h1>暫無數(shù)據(jù)</h1></td></tr>'); $(this).closest('div.datagrid-wrap').find('div.datagrid-pager').hide(); } //如果通過調(diào)用reload方法重新加載數(shù)據(jù)有數(shù)據(jù)時(shí)顯示出分頁導(dǎo)航容器 else $(this).closest('div.datagrid-wrap').find('div.datagrid-pager').show(); }, columns: [[ { field: 'ck', checkbox: true }, { field: 'Id', hidden: 'true' }, { field: 'InBoundId', hidden: 'true' }, { field: 'ProductId', hidden: 'true' }, { field: 'ProductTypeId', hidden: 'true' }, { field: 'SqNo', title: '入庫參考號', width: '100', align: 'left', sortable: true }, { field: 'Status', title: '狀態(tài)', width: '100', align: 'left', sortable: true, formatter: function (value, index, row) { if (value == "1") { return '<span style="color:green;">已入庫</span>'; } else if (value == "-1") { return '<span style="color:#FFA54F;">待入庫</span>'; } } }, { field: 'InboundDate', title: '入庫日期', width: '100', align: 'left', sortable: true, formatter: function (date) { var pa = /.*\((.*)\)/; var unixtime = date.match(pa)[1].substring(0, 10); //通過正則表達(dá)式來獲取到時(shí)間戳的字符串 return getTime(unixtime); } }, { field: 'ProductName', title: '產(chǎn)品名稱', width: '100', align: 'left', sortable: true }, { field: 'ProductType', title: '產(chǎn)品類型', width: '100', align: 'left', sortable: true }, { field: 'Num', title: '數(shù)量', width: '100', align: 'left', sortable: true }, { field: 'Storage', title: '所屬倉庫', width: '100', align: 'left', sortable: true }, { field: 'CompanyCode', title: '所屬公司', width: '100', align: 'left', sortable: true }, { field: 'CreateBy', title: '操作人', width: '100', align: 'left', sortable: true }, ]], });
二、今天的重點(diǎn),DataGrid行內(nèi)編輯
如上效果圖,我們在DataGrid行內(nèi)直接變數(shù)據(jù)
Js代碼如下:
如何實(shí)現(xiàn)行內(nèi)編輯,需要在你所編輯的單元格中加入editor屬性,editor屬性有個(gè)type(他支持很多控件類型,可以到官網(wǎng)查看)就是編輯的控件類型。
比如說,上圖中“入庫狀態(tài)”,首先我們定義數(shù)據(jù)源,json格式是重點(diǎn)。
var InboundStatus = [{ "value": "1", "text": "入庫" }, { "value": "-1", "text": "待入庫" }];
然后需要格式轉(zhuǎn)換函數(shù),不然你選擇的時(shí)候只會顯示value值,不是顯示文本值。代碼如下:
function unitformatter(value, rowData, rowIndex) { if (value == 0) { return; } for (var i = 0; i < InboundStatus.length; i++) { if (InboundStatus[i].value == value) { return InboundStatus[i].text; } } }
如何把數(shù)據(jù)源綁定到DataGrid列中,代碼如下:
formatter:使用我們前面定義的轉(zhuǎn)換格式函數(shù)。
options:中的data就是我們定義的數(shù)據(jù)源。
valueField:選中后的value值,不用詳細(xì)解釋了吧
textField:選中后顯示的值,文本值。
type:combobox,就是下拉選項(xiàng)的樣式。
{ field: 'Status', title: '入庫狀態(tài)', formatter: unitformatter, editor: { type: 'combobox', options: { data: InboundStatus, valueField: "value", textField: "text" } } }, //這部分代碼請結(jié)合下面的創(chuàng)建Grid的Js代碼查看。 $("#dataGrid").datagrid({ title: "產(chǎn)品列表", idField: 'ProductID', treeField: 'ProductName', onClickCell: onClickCell, striped: true, nowrap: true, collapsible: true, fitColumns: true, remoteSort: false, sortOrder: "desc", pagination: true,//表示在datagrid設(shè)置分頁 rownumbers: true, singleSelect: false, loadMsg: "正在努力加載數(shù)據(jù),請稍后...", url: "/Inbound/GetProductPage", onLoadSuccess: function (data) { if (data.total == 0) { var body = $(this).data().datagrid.dc.body2; body.find('table tbody').append('<tr><td width="' + body.width() + '" style="height: 35px; text-align: center;"><h1>暫無數(shù)據(jù)</h1></td></tr>'); $(this).closest('div.datagrid-wrap').find('div.datagrid-pager').hide(); } //如果通過調(diào)用reload方法重新加載數(shù)據(jù)有數(shù)據(jù)時(shí)顯示出分頁導(dǎo)航容器 else $(this).closest('div.datagrid-wrap').find('div.datagrid-pager').show(); }, columns: [[ { field: 'ck', checkbox: true }, { field: 'ProductID', title: '產(chǎn)品ID', hidden: true }, { field: 'CategoryID', title: '分類ID', hidden: true }, { field: 'ProductName', title: '產(chǎn)品名稱', width: '100', align: 'left', sortable: true }, { field: 'CompanyCode', title: '所屬公司', width: '100', align: 'center', sortable: true }, { field: 'CategoryName', title: '所屬分類', width: '100', align: 'center', sortable: true }, { field: 'Num', title: '數(shù)量', editor: 'numberbox' }, { field: 'Status', title: '入庫狀態(tài)', formatter: unitformatter, editor: { type: 'combobox', options: { data: InboundStatus, valueField: "value", textField: "text" } } }, { field: 'InDate', title: '入庫日期', width: '100', editor: { type: 'datebox' } }, { field: 'Storage', width: '100', title: '所入倉庫', formatter: function (value, row) { return row.Storage || value; }, editor: { type: 'combogrid', options: { //url: '/Storage/GetAllStorage', //url:'/Product/GetAllCustomerAddress', rownumbers: true, data: $.extend(true, [], sdata), idField: 'AddressID', textField: 'Name', columns: [[ { field: 'AddressID', hidden: true }, { field: 'Name', title: '庫名' }, { field: 'Country', title: '國家' }, { field: 'Province', title: '省份' }, { field: 'City', title: '市' }, { field: 'Area', title: '區(qū)' }, { field: 'Address', title: '詳細(xì)地址' }, ]], loadFilter: function (sdata) { if ($.isArray(sdata)) { sdata = { total: sdata.length, rows: sdata } } return sdata; }, } } } ]], onBeginEdit: function (index, row) { var ed = $(this).datagrid('getEditor', { index: index, field: 'Storage' }); $(ed.target).combogrid('setValue', { AddressID: row.AddressID, Name: row.Name }); }, onEndEdit: function (index, row) { var ed = $(this).datagrid('getEditor', { index: index, field: 'Storage' }); row.Storage = $(ed.target).combogrid('getText'); }, onClickRow: function (index, row) {//getEditor var ed = $(this).datagrid('getEditor', { index: index, field: 'Storage' }); if (ed != undefined) { var s = row.Storage; for (var i = 0; i < sdata.length; i++) { if (s == sdata[i].Name) { $(ed.target).combogrid('setValue', sdata[i].AddressID); } } } } });
三、重頭戲,也是我遇到的問題。
描述:我在datagrid中添加了下拉datagrid控件,當(dāng)我第一次選中后,如果在去點(diǎn)擊datagrid行,選中的下拉datagrid控件的值會被刷掉,這個(gè)問題確實(shí)困擾我很久,不過后來處理了,那種感覺也是無比的爽啊!
如上效果圖,“所入倉庫”一列,下拉是個(gè)datagrid,他的專業(yè)詞匯叫“Combogird”。就是這個(gè)玩意第一次選中沒問題,第二次點(diǎn)擊會把第一次選中的值刷掉。這也是一開始我對EasyUi的一個(gè)OnClickRow事件不了解。
先來上我之前的錯(cuò)誤代碼:
onClickRow: function (index, row) {//getEditor var ed = $(this).datagrid('getEditor', { index: index, field: 'Storage' }); $(ed.target).combogrid('setValue', row.Name); } } }
大家伙一定很苦惱這個(gè)row.Name是個(gè)什么玩意?what?其實(shí)我一開始也不知道,因?yàn)檫@個(gè)是錯(cuò)誤代碼,我是病急亂投醫(yī),胡亂寫的,哈哈,也不是胡亂寫啦,因?yàn)槲业南吕璯rid中有個(gè)字段是Name,然而我把他混淆了,此row是指你點(diǎn)擊的datagrid的row,而不是你數(shù)據(jù)源的row。我也是不斷調(diào)試Js看出來的端倪。我點(diǎn)擊datagrid的時(shí)候,代碼跳入OnClickRow事件中,有句代碼:“var ed = $(this).datagrid('getEditor', { index: index, field: 'Storage' })
;”,然后發(fā)現(xiàn)ed為null, Js拋異常,但是界面看不出來,只是把選中的數(shù)據(jù)刷掉了。找到問題后,還是不確定,代碼修改完,再運(yùn)行,正常顯示,也不刷掉我選中的值。
正確代碼如下:
onClickRow: function (index, row) {//getEditor var ed = $(this).datagrid('getEditor', { index: index, field: 'Storage' }); if (ed != undefined) { var s = row.Storage; for (var i = 0; i < sdata.length; i++) { if (s == sdata[i].Name) { $(ed.target).combogrid('setValue', sdata[i].AddressID); } } } }
一下是下拉Grid的數(shù)據(jù)源
function synchroAjaxByUrl(url) { var temp; $.ajax({ url: url, async: false, type: 'get', dataType: "json", success: function (data) { temp = data; } }); return temp; } var sdata = synchroAjaxByUrl('/Product/GetAllCustomerAddress');
總結(jié)
以上所述是小編給大家介紹的EasyUi控件中的Datagrid,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- jQuery EasyUI API 中文文檔 - DataGrid數(shù)據(jù)表格
- Jquery下EasyUI組件中的DataGrid結(jié)果集清空方法
- jQuery easyui datagrid動(dòng)態(tài)查詢數(shù)據(jù)實(shí)例講解
- 擴(kuò)展easyui.datagrid,添加數(shù)據(jù)loading遮罩效果代碼
- jQuery EasyUI datagrid實(shí)現(xiàn)本地分頁的方法
- jQuery EasyUI之DataGrid使用實(shí)例詳解
- jQuery Easyui DataGrid點(diǎn)擊某個(gè)單元格即進(jìn)入編輯狀態(tài)焦點(diǎn)移開后保存數(shù)據(jù)
- 實(shí)現(xiàn)easyui的datagrid導(dǎo)出為excel的示例代碼
- jquery Easyui Datagrid實(shí)現(xiàn)批量操作(編輯,刪除,添加)
- EasyUI使用DataGrid實(shí)現(xiàn)動(dòng)態(tài)列數(shù)據(jù)綁定
相關(guān)文章
jQuery Pagination Ajax分頁插件(分頁切換時(shí)無刷新與延遲)中文翻譯版
此jQuery插件為Ajax分頁插件,一次性加載,故分頁切換時(shí)無刷新與延遲,如果數(shù)據(jù)量較大不建議用此方法,因?yàn)榧虞d會比較慢,接下來詳細(xì)介紹使用方法,感興趣的朋友可以了解下2013-01-01jquery基本選擇器匹配多個(gè)元素的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨query基本選擇器匹配多個(gè)元素的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09Jquery實(shí)現(xiàn)仿京東商城省市聯(lián)動(dòng)菜單
這篇文章主要介紹了Jquery實(shí)現(xiàn)仿京東商城省市聯(lián)動(dòng)菜單的簡單實(shí)例演示,可以選擇對應(yīng)省、市、縣,希望大家可以喜歡。2015-11-11移動(dòng)端jQuery修正Web頁面滑動(dòng)時(shí)div問題的兩則實(shí)例
這篇文章主要介紹了移動(dòng)端jQuery修正Web頁面滑動(dòng)時(shí)div問題的兩則實(shí)例,分別針對頂部固定fixed不為0時(shí)滑動(dòng)出現(xiàn)的閃動(dòng)以及touchmove的受阻止的相關(guān)問題,需要的朋友可以參考下2016-05-05基于JQuery實(shí)現(xiàn)CheckBox全選全不選
做項(xiàng)目時(shí)需要實(shí)現(xiàn)CheckBox的全選,我想用JQuery實(shí)現(xiàn),從網(wǎng)上找了找,網(wǎng)上的確有很多例子,但都不能實(shí)現(xiàn)我想要的全部效果2011-06-06jQuery中.attr()和.data()的區(qū)別分析
$.attr()和$.data()本質(zhì)上屬于 DOM屬性 和 Jquery對象屬性 的區(qū)別。下面通過一個(gè)示例給大家介紹jQuery中.attr()和.data()的區(qū)別,一起看看吧2017-09-09jquery調(diào)用wcf并展示出數(shù)據(jù)的方法
網(wǎng)上看了很多jquery調(diào)用wcf的例子,可能是主機(jī)的原因,我用的是gd主機(jī),所以都沒有成功,昨天自己搞了一天,終于成功了,現(xiàn)把方法和代碼和大家分享2011-07-07