【问题标题】:How do I make a non database column in jqGrid?如何在 jqGrid 中创建非数据库列?
【发布时间】:2011-07-13 22:57:55
【问题描述】:

我想即时计算 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'}

],

【问题讨论】:

    标签: jqgrid


    【解决方案1】:

    在 jqGrid 中实现“虚拟列”的最糟糕的方法是使用custom formatter。例如

    {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);
     }}
    

    使用自定义格式化程序的主要缺点是您在内部使用 make full 格式化。方法$.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;
     }}
    

    查看演示 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-05
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 2017-06-06
      相关资源
      最近更新 更多