function addCallBack(data) { var row = $('#List').treegrid('getSelected'); $('#List').treegrid('append', { parent: data.Parent_Id, data: [{ Id: data.Id, Name: data.Name, En_Name:data.En_Name, Code: data.Code, Enable: data.Enable, Sort: data.Sort, }] }); layer.msg('@CommonResource.AddSuccess', { icon: 1, time: 1000 }); } function editCallBack(data) { var row = $('#List').treegrid('getSelected'); $('#List').treegrid('update', { id: row.Id, row: { Name: data.Name, En_Name: data.En_Name, Code: data.Code, Enable: data.Enable, Sort: data.Sort, } }); layer.msg('@CommonResource.ModifySuccess', { icon: 1, time: 1000 }); } $("#btnDelete").click(function () { var row = $('#List').treegrid('getSelected'); if (row != null) { layer.confirm('@CommonResource.ConfirmDelete', { btn: ['@CommonResource.Sure', '@CommonResource.Cancel'], shadeClose: true, }, function () { if (row.ChildCount == 0 || typeof (row.ChildCount) == 'undefined') { $.post("@Url.Action("Delete")/" + row.Id, function (data) { if (data == "1") { $("#List").treegrid('remove', row.Id); layer.msg('@CommonResource.DeleteSuccess', { icon: 1, time: 1000 }); } else { layer.msg('@CommonResource.DeleteFailed', { icon: 2, time: 1000 }); } }, "json"); } else { layer.msg('@CommonResource.Noselectedrecord', { icon: 2, time: 1000 }); } }, function () { }); } else { layer.msg('@CommonResource.Noselectedrecord', { icon: 2, time: 1000 }); } });
6,编辑单元格
具体代码实现
var taxTypeList = JSON.parse($("#taxTypeList").val()); var manufactureList = JSON.parse($("#manufactureList").val()); $.extend($.fn.datagrid.methods, { editCell: function (jq, param) { return jq.each(function () { var opts = $(this).datagrid('options'); var fields = $(this).datagrid('getColumnFields', true).concat($(this).datagrid('getColumnFields')); for (var i = 0; i < fields.length; i++) { var col = $(this).datagrid('getColumnOption', fields[i]); col.editor1 = col.editor; if (fields[i] != param.field) { col.editor = null; } } $(this).datagrid('beginEdit', param.index); for (var i = 0; i < fields.length; i++) { var col = $(this).datagrid('getColumnOption', fields[i]); col.editor = col.editor1; } }); } }); var editIndex = -1; function endEditCal() { if (editIndex == -1) { return true; } if ($('#Detail').datagrid('validateRow', editIndex)) { $('#Detail').datagrid('endEdit', editIndex); editIndex = -1; return true; } else { return false; } } $('#Detail').datagrid({ loadMsg: '@CommonResource.Processing', toolbar: '#tb', width: "100%", height: "100%", data: JSON.parse($("#MaterialDetailListStr").val()), rownumbers: true, autoRowHeight: false, fit: true, fitColumns: true, striped: true, singleSelect: true, collapsible: false, pagination: false, queryParams: { }, columns: [[ { field: 'Material_Use', title: '用途', width: 100, sortable: false, formatter: function (value) { for (var i = 0; i < manufactureList.length; i++) { if (manufactureList[i].Key == value) return manufactureList[i].Value; } return value; }, editor: { type: 'combobox', options: { valueField: 'Key', textField: 'Value', data: manufactureList, required: true, panelHeight: "auto", editable:false, } }, }, { field: 'Tax_Type', title: '税别', width: 100, sortable: false, formatter: function (value) { for (var i = 0; i < taxTypeList.length; i++) { if (taxTypeList[i].Key == value) return taxTypeList[i].Value; } return value; }, editor: { type: 'combobox', options: { valueField: 'Key', textField: 'Value', data: taxTypeList, required: true, panelHeight: "auto", editable: false, } }, }, { field: 'Tax_Bcd', title: 'BCD', width: 100, sortable: false, editor: { type: 'numberbox', options: { required: true, suffix: '%', precision: 2, min: 0, max: 100, } } }, { field: 'Tax_Cess', title: 'CESS', width: 100, sortable: false, editor: { type: 'numberbox', options: { required: true, suffix: '%', precision: 2, min: 0, max: 100, } } }, { field: 'Tax_Igst', title: 'IGST', width: 100, sortable: false, editor: { type: 'numberbox', options: { required: true, suffix: '%', precision: 2, min: 0, max: 100, } } }, ]], @if (Request.Params["Operate"] != "View") { <text> onClickCell: function (index, field, value) { if (endEditCal()) { $(this).datagrid('selectRow', index).datagrid('editCell', { index: index, field: field }); //编辑一个单元格 //$(this).datagrid('beginEdit', index); //编辑一行 editIndex = index; } else { layer.msg('当前行的数据编辑有误', { icon: 2, time: 1000 }); } }, onAfterEdit: function (index, row, changes) { var rowData = $(this).datagrid('getData').rows[index]; $('#Detail').datagrid('updateRow', { index: index, row: {}, }); }, onLoadSuccess: function (data) { for (var index = 0; index < data.rows.length; index++) { $(this).datagrid('beginEdit', index); } }, </text> } }); $("#btnLineCreate").click(function () { if (endEditCal()) { editIndex = 0; $('#Detail').datagrid('insertRow', { index: editIndex, row: {}, }); $('#Detail').datagrid('selectRow', editIndex); $('#Detail').datagrid('beginEdit', editIndex); } else { layer.msg('当前行的数据编辑有误', { icon: 2, time: 1000 }); } }); $("#btnLineDelete").click(function () { var row = $('#Detail').treegrid('getSelected'); if (row != null) { var rowIndex = $('#Detail').datagrid('getRowIndex', row); $('#Detail').datagrid('deleteRow', rowIndex); layer.msg('@CommonResource.DeleteSuccess', { icon: 1, time: 1000 }); } else { layer.msg('@CommonResource.Noselectedrecord', { icon: 2, time: 1000 }); } }); $("#btnSave").click(function () { var summaryValidate = true; var rows = $("#Detail").datagrid("getRows"); $(rows).each(function (index, itemData) { if ($('#Detail').datagrid('validateRow', index)) { $('#Detail').datagrid('endEdit', index); } else { summaryValidate = false; return false; } }); if (summaryValidate) { if (rows.length == 2) { $("#MaterialDetailListStr").val(JSON.stringify(rows)); } else { layer.msg('税别,用途应该设置为2行数据', { icon: 2, time: 1000 }); return false; } } else { layer.msg('当前表单数据编辑有误', { icon: 2, time: 1000 }); return false; } var check = $('form').form('validate'); if (check) { $.ajax({ url: "@Url.Action("CreateMaterial")", type: "Post", data: $("form").serialize(), dataType: "json", success: function (data) { if (data.Key == "1") { parent.$("#List").datagrid('reload'); var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index); parent.layer.msg('@CommonResource.AddSuccess', { icon: 1, time: 1000 }); } else { layer.msg("物料编号'" + data.Value.Item_Number + "'在数据库中已添加", { icon: 2, time: 1000 }); } }, error: function (jqXHR, textStatus, errorThrown) { layer.msg('@CommonResource.AddFailed', { icon: 2, time: 1000 }); } }); } });
7,重置datagrid布局 $('#List').datagrid("resize");
$(function () { $(".more").click(function () { $(this).closest(".conditions").siblings().toggleClass("hide"); $('#List').datagrid("resize"); }); })
四,总结
这些技巧,在帮助文档里也说的很详细,我只是把这些技术用于实践。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章: