一、需要引入的文件
二、代码详解
/* 允许编辑单元格 */
$('#contentTable').datagrid().datagrid('enableCellEditing');
/* 数据的自定义封装 */
$('#contentTables').datagrid("loadData",{"total":1,"rows": List
datagrid表格
options 参数包含两个属性:index:行的索引。 field:字段名
editor:定义编辑行时的编辑器。
$('#contentTable').datagrid({
url:"${ctx}/faccounting/glVoucher/getGlDetailList",
iconCls: 'icon-tip',
singleSelect: true,
fitColumns: true,
remoteSort: false,
resizable : false,
nowrap : true,
autoRowHeight : true,
striped : true,
rownumbers: true,
checkOnSelect: false,
selectOnCheck: true,
queryParams:
{
searchData : JSON.stringify($("#searchForm").serializeArray())
},
onBeginEdit: onBeginEdit,/* 允许编辑开始 */
onEndEdit: onEndEdit,/* 允许编辑结束 */
height:340,
columns:[
[
{field:'explanation',title:'摘要',width:80,align:'left',halign:'center',editor:'selectExplanation'},
{field:'pkAccsubj.dispname',title:'科目',width:100,align:'left',halign:'center',
formatter:function(value,row){
var dispname = row.pkAccsubj.dispname;
return dispname == '' || dispname == null ? '' : dispname;
},
editor:'selectSubjassForm'
},
{field:'valueName',title:'辅助核算',width:100,align:'left',halign:'center',editor:'selectAccsubjForm'},
{field:'currtypecode',title:'币种',width:40,align:'left',halign:'center',
formatter:function(value,row){
if(row.pkCurrtype.currtypecode==""||typeof(row.pkCurrtype.currtypecode) == "undefined"){
var obj = new Object();
obj.pkCurrtype='00010000000000000001';
obj.currtypecode='CNY';
row.pkCurrtype=obj;
}
var currtypecode = row.pkCurrtype.currtypecode;
return currtypecode == ''|| currtypecode == null ? '' : currtypecode;
},
editor:{
type:'combobox',
options:{
valueField:'pkCurrtype',
textField:'currtypecode',
panelHeight:48,
method:'get',
url:'${ctx}/customization/bdCurrtype/tableDataForm?s="+ Math.random()'
}
}
},
{field:'creditamount',title:'原币',width:40,align:'right',halign:'center',
formatter:function(value,row){
if(row.creditamount!=0){
return thousandBitSeparator(row.creditamount);
}else{
return thousandBitSeparator(row.debitamount);
}
},
editor:{}
},
{field:'localdebitamount',title:'借方',width:40,align:'right',halign:'center',
formatter:function(value,row){
if(row.localdebitamount==0){
return "";
}else{
return thousandBitSeparator(row.localdebitamount);
}
},
editor:'selectGenDebit'
},
{field:'localcreditamount',title:'贷方',width:40,align:'right',halign:'center',
formatter:function(value,row){
if(row.localcreditamount==0){
return "";
}else{
return thousandBitSeparator(row.localcreditamount);
}
},
editor:'selectGenLcredit'
}
]
]
});
});
onBeginEdit:当用户编辑一行时触发该动作
$(this).datagrid('getEditor', {index,field'}):获取指定的编辑器,
$(this).datagrid('endEdit',index):结束对一行进行编辑。
$('#autocom').combobox('setText', ''):文本内容
$('#autocom').combobox('setValue', ''):文本值
function onBeginEdit(index,row){
var flag = false;
var subj = $("#subjCode").val();
var subjCode = new Array();
subjCode.push(subj.split(","));
var subjRow= row.pkAccsubj.subjcode;
for(var j=0;j
//添加行
$("#addRow").on('click',function(){
var lastIndex = $('#contentTable').datagrid('getRows').length;
var tr = $('#assistpage .datagrid-view2').eq(0).find('tbody tr').eq(lastIndex);
var free1 = $(tr).attr('free1-value');
window.rows = $('#contentTable').datagrid('getChanges', 'inserted');
$('#contentTable').datagrid('endEdit', lastIndex);
$('#contentTable').datagrid('appendRow',{
explanation:'',
pkAccsubj:'',
vouchetypename:'',
valueName:'',
pkCurrtype:'',
creditamount:'',
localdebitamount:'',
localcreditamount:''
});
var rows = $('#contentTable').datagrid('getRows');
if(rows.length>=3 && typeof(rows[0].pkVoucher) != "undefined"){
rows[rows.length-1].explanation = rows[rows.length-2].explanation;
}else if(rows.length>=3){
rows[rows.length-2].explanation = rows[rows.length-3].explanation;
}
rows[0].free1 = typeof(free1) == 'undefined' ? '' : free1;
$('#contentTable').datagrid("loadData",{"total":rows.length,"rows":rows});
});
//删除行
$("#delRow").on('click',function(){
var row = $('#contentTable').datagrid('getSelected');
if(null == row){
layer.msg('请选择要删除的分录!', {icon: 0});
return false;
}else if(typeof(row.pkVoucher) != 'undefined'){
layer.msg('此分录为协同分录不能删除!', {icon: 0});
return false;
}else{
var rowIndex = $('#contentTable').datagrid('getRowIndex', row);
$('#contentTable').datagrid('deleteRow', rowIndex);
}
});
//插入行
$("#insRow").on('click',function(){
var row = $("#contentTable").datagrid('getSelected');
if(row == null){
$("#addRow").click();
return false;
}
var index = $("#contentTable").datagrid('getRowIndex', row);
$('#contentTable').datagrid('endEdit', index);
$('#contentTable').datagrid('appendRow',{
explanation:'',
pkAccsubj:'',
vouchetypename:'',
valueName:'',
pkCurrtype:'',
creditamount:'',
localdebitamount:'',
localcreditamount:''
});
var toup = $("#contentTable").datagrid('getData').rows[index + 1];
var todown = $("#contentTable").datagrid('getData').rows[index];
$("#contentTable").datagrid('getData').rows[index] = toup;
$("#contentTable").datagrid('getData').rows[index + 1] = todown;
$("#contentTable").datagrid('refreshRow', index);
$("#contentTable").datagrid('refreshRow', index + 1);
$("#contentTable").datagrid('selectRow', index + 1);
});
//协同确认
$("#saveRow").on('click',function(){
var flag = sumtotal();
if(!flag) return false;
var rows = $('#contentTable').datagrid('getData');
var glDetailList = new Array();
//分录非空限制
for(var i=0;i
//摘要文本
$.extend($.fn.datagrid.defaults.editors, {
selectExplanation: {
init: function(container, options){
var html = ''
+''
+''
+' ';
var input = $(html).appendTo(container);
return input;
},
getValue: function(target){
return $(target).val();
},
setValue: function(target, value){
$(target).val(value);
$(target).find("input").val(value);
},
resize: function(target, width){
var input = $(target);
if ($.boxModel == true){
input.width(width - (input.outerWidth() - input.width()));
} else {
input.width(width);
}
}
}
});
//科目
$.extend($.fn.datagrid.defaults.editors, {
selectSubjassForm: {
init: function(container, options){
var html = '';
var input = $(html).appendTo(container);
$('#autocom').combobox({
data:$("#accSubjCode").data('accSubjCode'),
valueField:'id',
textField:'label',
icons:[
{
iconCls:'icon-clear',
handler:function(e){
subjassForm();
}
}
],
});
return input;
},
getValue: function(target){
return $(target).val();
},
setValue: function(target, value){
$(target).val(value);
$(target).find("input").val(value);
},
resize: function(target, width){
var input = $(target);
if ($.boxModel == true){
input.width(width - (input.outerWidth() - input.width()));
} else {
input.width(width);
}
}
}
});
//科目弹出框
function subjassForm(){
var rowIndex = $('#contentTable').datagrid('getRowIndex',$('#contentTable').datagrid('getSelected'));
var ed = $('#contentTable').datagrid('getEditor', {
index: rowIndex,
field: 'pkAccsubj.dispname'
});
var row = $('#contentTable').datagrid('getSelected');
var id = row.pkAccsubj.pkAccsubj;
var pkGlorgbook = $("#bdGlorgId").val();
top.layer.open({
type: 2, area: ['1100px', '680px'],
title:"选择会计科目",content: "${ctx}/customization/bdAccsubj/searchForm?id="+id+"&pkGlorgbook="+pkGlorgbook+"&parentFrameID="+"",
btn: ['确定', '关闭'],
yes: function(index, layero){
var tree = layero.find("iframe")[0].contentWindow.zTree;
var ids = [], names = [], nodes = [], dispnames = [], pkAccsubjs = [], isHasFreeValues = [];
nodes = tree.getSelectedNodes();
for(var i=0; i