如何在jqGrid中创建非数据库列?

use*_*749 4 jqgrid

我想在没有havng的情况下动态计算Total在calctotal.php中.在本质上,这是一个计算列.我想过使用像afterInsertRow这样的事件,但即使没有事件,它也会将列数据移动一,因为XML文件只有3列而不是4列.所以我的Total列现在有来自Notes的数据我确实在php文件中添加了一个假列,但为什么我必须这样做呢?谢谢

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'}

],
Run Code Online (Sandbox Code Playgroud)

Ole*_*leg 6

在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);
 }}
Run Code Online (Sandbox Code Playgroud)

使用自定义格式化程序的主要缺点是您在内部使用make完全格式化.调用该方法$.fmatter.util.NumberFormat可以帮助我们简化工作.

如果使用远程数据类型(datatype: 'xml'datatype: 'json'),则服务器负责数据排序.因此,服务器应该能够不仅为"真实"数据字段而且对"虚拟"列对数据进行排序.我们index:'total'在上面使用.因此,如果用户单击"总计"列的标题sidx,将发送到服务器的参数将是total.所以服务器应该能够生成按照排序的数据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;
 }}
Run Code Online (Sandbox Code Playgroud)

请在此处查看演示.