Dojox Grid将两个字段传递给格式化程序

时间:2010-08-08 14:00:26

标签: ruby-on-rails dojo dojox.grid

我成功创建了一个dojox.Grid,但在一种情况下我需要将两个字段传递给格式化程序函数而不是一个。 例如:

   {
        field: 'id',
        name: 'Id',
        formatter: formatterFunction,
    },

我需要将formatterFunction()传递给'id'和'name'。我怎样才能做到这一点? 谢谢。

4 个答案:

答案 0 :(得分:4)

我知道这已经在IRC频道中提到了,但我在这里回答其他人都知道,并且还要解决你的进一步问题,我不确定是否有人回答。

  

1.4中的新功能如果将字段的值设置为“_item”,则将使用商店中的整个项目调用格式化程序 - 而不是仅使用一个字段值

这样就可以使用格式化程序执行您想要的操作。

http://www.dojotoolkit.org/reference-guide/dojox/grid/DataGrid.html#usage

在最简单的情况下,如果不设置网格的formatterScope,可以通过this.grid.store从格式化程序中访问网格的存储,例如:

function fmtItem(value) {
  var store = this.grid.store;
  return store.getValue(value, 'id') + ': ' + store.getValue(value, 'name');
}

以下是上述格式化程序的一个非常简单的示例:

http://jsbin.com/upico4/edit

在其中一个测试页面中还有一个示例,它创建了一个用于保存和限定格式化程序的对象:

http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/grid/tests/test_grid_formatters.html

答案 1 :(得分:3)

从dojo 1.4开始,您还可以从商店获取多个字段。应该看起来像:

    var layout = [{
        rows: [
          {name: 'Title', fields:['Title', 'url'], formatter:formatLink}
        ]}]

    function formatLink(value){
        return '<a href="'+value[1]+'">'+value[0]+'</a>';
    }

使用字段“url”中的值指向您的链接,显示的标题将填充商店“标题”字段中的数据。

答案 2 :(得分:0)

您确定要格式化,也许不使用get吗? 使用格式化程序时,传递给函数的 only 值是该字段表示的值。

但是,如果您要使用get,则可以使用该项来访问其他值。 (但是你会丢失排序)。

所以你的专栏有

   {
        field: 'id',
        name: 'Id',
        get: getFunction
    },

然后有

getFunction: function(index,row) {
    return row.id + row.name;
}

答案 3 :(得分:-1)

function formatterFunction(val, rowIdx, cell){

  var name=this.name,
  field=this.field;

}