Recently I have a web project that requires the use of third-party controls (EasyUi). After all, the effect produced by using third-party controls is slightly more beautiful than the original one. There is a requirement in this project, which needs to be in the data list. Direct editing of data saves is called inline editing in jargon. This article mainly introduces the relevant information of Datagrid in EasyUi control in detail. Friends who need it can refer to it. I hope it can help everyone.
Before talking about in-line editing, we need to first understand how to use EasyUi to create a DataGrid. Of course, there are many ways (1.easyui.js, or direct html code plus easyui Style), I use the JS method:
1. Use Js to create the DataGrid
The above is the rendering,
The Html code is as follows: Define a table on the page
##
<!--数据展示 --> <p> <table id="DataGridInbound"></table> </p>
$("#DataGridInbound").datagrid({ title: '入库详情', idField: 'Id', rownumbers: 'true', url: '/Inbound/GetPageInboundGoodsDetail', pagination: true,//表示在datagrid设置分页 rownumbers: true, singleSelect: true, striped: true, nowrap: true, collapsible: true, fitColumns: true, remoteSort: false, loadMsg: "正在努力加载数据,请稍后...", 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>暂无数据</h1></td></tr>'); $(this).closest('p.datagrid-wrap').find('p.datagrid-pager').hide(); } //如果通过调用reload方法重新加载数据有数据时显示出分页导航容器 else $(this).closest('p.datagrid-wrap').find('p.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: '状态', 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); //通过正则表达式来获取到时间戳的字符串 return getTime(unixtime); } }, { field: 'ProductName', title: '产品名称', width: '100', align: 'left', sortable: true }, { field: 'ProductType', title: '产品类型', width: '100', align: 'left', sortable: true }, { field: 'Num', title: '数量', 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 }, ]], });
2. Today’s focus, DataGrid inline editing
As shown in the above rendering, we directly change the data in the DataGrid row The Js code is as follows: How to implement in-row editing, you need to add it to the cell you are editing Editor attribute, the editor attribute has a type (it supports many control types, you can check it on the official website), which is the type of editing control. For example, in the "warehousing status" in the picture above, we first define the data source, and the json format is the focus.
var InboundStatus = [{ "value": "1", "text": "入库" }, { "value": "-1", "text": "待入库" }];
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; } } }
{ field: 'Status', title: '入库状态', formatter: unitformatter, editor: { type: 'combobox', options: { data: InboundStatus, valueField: "value", textField: "text" } } }, //这部分代码请结合下面的创建Grid的Js代码查看。 $("#dataGrid").datagrid({ title: "产品列表", idField: 'ProductID', treeField: 'ProductName', onClickCell: onClickCell, striped: true, nowrap: true, collapsible: true, fitColumns: true, remoteSort: false, sortOrder: "desc", pagination: true,//表示在datagrid设置分页 rownumbers: true, singleSelect: false, loadMsg: "正在努力加载数据,请稍后...", 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>暂无数据</h1></td></tr>'); $(this).closest('p.datagrid-wrap').find('p.datagrid-pager').hide(); } //如果通过调用reload方法重新加载数据有数据时显示出分页导航容器 else $(this).closest('p.datagrid-wrap').find('p.datagrid-pager').show(); }, columns: [[ { field: 'ck', checkbox: true }, { field: 'ProductID', title: '产品ID', hidden: true }, { field: 'CategoryID', title: '分类ID', hidden: true }, { field: 'ProductName', title: '产品名称', 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: '数量', editor: 'numberbox' }, { field: 'Status', title: '入库状态', 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: '区' }, { field: 'Address', title: '详细地址' }, ]], 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); } } } } });
Third, the highlight is also the problem I encountered.
Description: I added a drop-down datagrid control to the datagrid. When I select it for the first time, if I click on the datagrid row, the value of the selected drop-down datagrid control will be erased. , this problem really bothered me for a long time, but later I solved it, and the feeling was extremely refreshing! As shown in the rendering above, in the "Into Warehouse" column, the drop-down is a datagrid, and its professional vocabulary is called "Combogird". It's just that the first selection of this thing is fine, but the second click will erase the value of the first selection. This is also the first time I didn't understand EasyUi's OnClickRow event. Let me start with my previous error code:onClickRow: function (index, row) {//getEditor var ed = $(this).datagrid('getEditor', { index: index, field: 'Storage' }); $(ed.target).combogrid('setValue', row.Name); } } }
var ed = $(this).datagrid('getEditor', { index: index, field: 'Storage' }) ;", and then found that ed was null, Js threw an exception, but the interface could not see it, and it just erased the selected data. After finding the problem, I was still not sure. After modifying the code, I ran it again and it displayed normally without erasing the value I selected.
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); } } } }
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');
Detailed explanation of EasyUI's DataGrid binding Json data source method
Solution to sending two requests after easyui's datagrid component page is loaded
EasyUI's dataGrid inline editing
The above is the detailed content of Detailed explanation of Datagrid in EasyUi control. For more information, please follow other related articles on the PHP Chinese website!