如何在jqGrid中创建非数据库列?
我想动态计算总数,而不必在calctotal.php中进行计算。本质上,这是一个计算列。我曾想过使用一些事件,比如afterInsertRow,但即使没有事件,它也会将列数据移动1,因为XML文件只有3列而不是4列。因此,我的Total列现在包含来自Notes的数据。我确实在php文件中添加了一个伪列,但为什么我必须这样做呢?谢谢如何在jqGrid中创建非数据库列?,jqgrid,Jqgrid,我想动态计算总数,而不必在calctotal.php中进行计算。本质上,这是一个计算列。我曾想过使用一些事件,比如afterInsertRow,但即使没有事件,它也会将列数据移动1,因为XML文件只有3列而不是4列。因此,我的Total列现在包含来自Notes的数据。我确实在php文件中添加了一个伪列,但为什么我必须这样做呢?谢谢 url:'./calctotal', datatype: 'xml', mtype: 'GET', colNames:['Inv No', 'Amount','Tax
url:'./calctotal',
datatype: 'xml',
mtype: 'GET',
colNames:['Inv No', 'Amount','Tax','Total'm 'Notes'],
colModel :[
{name:'invid', index:'invid', width:55},
{name:'amount', editable: false, index:'amount', width:80, align:'right'},
{name:'tax', index:'tax', width:80, align:'right'},
**{name:'total', index:'total', width:80, align:'right'},**
{name:'notes', index:'notes', width:80, align:'left'}
],
在jqGrid中实现“虚拟列”的基本方法是使用。比如说
{name:'amount',index:'amount',width:70, formatter:'currency', align:'right'},
{name:'tax',index:'tax',width:50, formatter:'currency', align:'right'},
{name:'total',index:'total',width:60, align:'right',
formatter:function(cellvalue, options, rowObject) {
var amount = parseInt(rowObject.amount,10),
tax = parseInt(rowObject.tax,10);
return $.fmatter.util.NumberFormat(amount+tax,$.jgrid.formatter.currency);
}}
使用自定义格式化程序的主要缺点是在内部使用makefullformatting。调用方法$.fmatter.util.NumberFormat
可以帮助我们简化工作
如果使用远程数据类型(datatype:'xml'
或datatype:'json'
),服务器负责数据排序。因此,服务器不仅应该能够对“真实”数据字段的数据进行排序,还应该能够对“虚拟”列的数据进行排序。我们使用上面的索引:'total'
。因此,如果用户单击“总计”列的标题,将发送到服务器的sidx
参数将是Total
。因此,服务器应该能够生成按总数排序的数据
如果使用本地数据,则可以使用sorttype
作为实现排序的函数:
{name:'amount',index:'amount',width:70, formatter:'currency', sorttype:'number',
align:'right'},
{name:'tax',index:'tax',width:50, formatter:'currency', sorttype:'number',
align:'right'},
{name:'total',index:'total',width:60, align:'right',
formatter:function(cellvalue, options, rowObject) {
var amount = parseInt(rowObject.amount,10),
tax = parseInt(rowObject.tax,10);
return $.fmatter.util.NumberFormat(amount+tax,$.jgrid.formatter.currency);
},
sorttype:function(cellvalue, rowObject) {// like for sorttype:'number',
var amount = parseInt(rowObject.amount,10),
tax = parseInt(rowObject.tax,10);
return amount+tax;
}}
请看演示