【问题标题】:Dojox Grid pass two fields to formatterDojox Grid 将两个字段传递给格式化程序
【发布时间】:2011-03-26 23:05:35
【问题描述】:

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

{ 字段:'id', 名称:“身份证”, 格式化程序:格式化程序函数, },

例如,我需要将“id”和“name”都传递给 formatterFunction()。我怎样才能做到这一点? 谢谢。

【问题讨论】:

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


    【解决方案1】:

    我知道 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

    【讨论】:

      【解决方案2】:

      从 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”字段中的值来指向您的链接,并且显示的标题将填充您商店中“标题”字段中的数据。

      【讨论】:

        【解决方案3】:

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

        但是,如果您要改用 get,则可以使用该项目来访问其他值。 (但是,您将失去排序)。

        所以对于你的专栏有

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

        然后有

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

        【讨论】:

          【解决方案4】:
          function formatterFunction(val, rowIdx, cell){
          
            var name=this.name,
            field=this.field;
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-11
            • 1970-01-01
            相关资源
            最近更新 更多