如何在 jqGrid 中创建非数据库列? [英] How do I make a non database column in jqGrid?

查看:14
本文介绍了如何在 jqGrid 中创建非数据库列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to calculate Total on the fly without havng to do it in calctotal.php. In essense this is a calculated column. I thought of using some event like afterInsertRow, but even with no event it moves the column data by one because the XML file is only 3 columns instead of four. So my Total column now has the data from Notes I did add a fake column to the php file, but why should I have to do that? Thanks

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 中实现虚拟列"的最佳方法是使用 自定义格式化程序.例如

The bast way to implement "virtual columns" in jqGrid is the usage of the custom formatter. For example

{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方法的调用可以帮助我们简化工作.

The main disadvantage of the usage of the custom formatter is that you use make full formatting inside. The call of the method $.fmatter.util.NumberFormat can help us to simplify the work.

如果您使用远程数据类型(datatype: 'xml'datatype: 'json'),则服务器负责数据排序.因此,服务器不仅应该能够对真实"数据字段的数据进行排序,还应该能够对虚拟"列的数据进行排序.我们在上面使用 index:'total'.因此,如果用户单击Total"列的标题,将发送到服务器的 sidx 参数将是 total.所以服务器应该能够产生按total排序的数据.

If you use remote datatype (datatype: 'xml' or datatype: 'json') the server is responsible for data sorting. So the server should be able to sort the data not only for the "real" data field but for the "virtual" columns also. We use index:'total' above. So if the user click on the header of the 'Total' column the sidx parameter which will be send to the server will be total. So the server should be able to produce the data sorted by the total.

如果你使用本地数据,你可以使用 sorttype 作为函数来实现排序:

If you use local data you can use sorttype as the function to implement the sorting:

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

查看演示这里.

这篇关于如何在 jqGrid 中创建非数据库列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆