2

我正在使用以下符号在 dojo 1.6 中创建一个 dojox.grid.DataGrid:

<table dojoType="dojox.grid.DataGrid">
  <thead>
    <tr>
      <th field="id">ID</th>
      <th field="contact.name">Name</th>
      <th field="contact.tel">Telephone</th>
      <th field="contact.birthdate.time">Birthday</th>
    </tr>
  </thead>
</table>

数据看起来像这样:

[{
'id':1,
'contact':{
  'name':'Doh', 
  'firstname':'John',
  'tel':'123-123456',
  'birthdate':{
    'javaClass':'java.sql.Timestamp',
    'time':1234567893434}}
}]

ID 正确呈现,但所有其他呈现为“...”。我试图指定一个格式化程序,将基本对象“联系人”设置为 FIELD,然后返回 FIELD.name。到目前为止,这适用于显示正确的值,但排序然后使用基础对象。

我认为可能有一种方法可以进一步推动这一点,覆盖表格的排序,但我希望尽可能简单。

我还希望防止出现性能问题。

有任何想法吗?

4

1 回答 1

2

我发现 DataGrid 行定义有一个名为“get”的属性。“get”指定返回要在 DataGrid 列中显示的值的函数的名称。

从理论上讲,这应该可以解决我的问题。

get-function 的实现方式如下:

网格定义:

<table dojoType="dojox.grid.DataGrid">
  <thead>
    <tr>
      <th field="id">ID</th>
      <th field="contact" get="myGrid.getContactName">Name</th>
      <th field="contact" get="myGrid.getContactTel">Telephone</th>
      <th field="contact" get="myGrid.getContactBirthdateTime">Birthday</th>
    </tr>
  </thead>
</table>

回调函数(示例):

myGrid.getContactName = function(colIndex,item){
  return item.name;
};

我目前无法判断此实现是否正确,因为 item 参数在我的应用程序中始终为空。

这可能是由于使用了新的 Store API (store.Memory) 而不是 ItemFileReadStore,但我还没有成功解决这个难题。

此外,我无法使用这种新方法测试网格排序,因此我不会将此解决方案标记为已解决。

更新

myGrid.getContactName = function(colIndex,record){
  if(record)return record.contact.name;
  else return null;
};

一旦我避免了空情况,它就可以正常工作。

然而,客户端类型的 Grid 似乎没有通过 get 函数访问,而是直接使用该字段。这会阻止对嵌套字段进行正确排序。

更新

我终于解决了我的问题:

第一个问题:为 DataGrid 字段指定嵌套数据已经解决了,使用 get 函数深入到数组子结构中。(上面已经解释过了)

然而,排序仍然取决于字段属性。如果字段属性包含数组的名称,则此列将无法正确排序。

我不得不修改一些 dojo 类来适应它。稍后我将把它放在一个更模块化的形式中,但这里是现在的原始结果:

首先,我需要允许在网格定义中定义额外的比较器回调。为此,我向 dojox.grid.cells._base 添加了一些代码

dgc._Base.markupFactory = function(node, cellDef){
var d = dojo;
...
var comparator = d.trim(d.attr(node,"comparator")||"");
if(comparator){
  cellDef.comparator = dojo.getObject(comparator);
}
...
}

接下来,DataGrid 需要将此新参数提供给查询以进行排序。这是在 dojox.grid.DataGrid 中完成的。“c”是我在上面修改的单元格。

getSortProps:function(){
...
return[{attribute:c.field,descending:desc,comparator:c.comparator}];
}

最后,我需要更改在 dojo.store.util.SimpleQueryEngine 中定义的排序本身。SimpleQueryEngine 是 MemoryStore (dojo.store.Memory) 的默认引擎。

function execute(array){
    // execute the whole query, first we filter
    var results = dojo.filter(array, query);
    // next we sort
    if(options && options.sort){
        results.sort(function(a, b){
            for(var sort, i=0; sort = options.sort[i]; i++){
                var aValue = a[sort.attribute];
                var bValue = b[sort.attribute];
                if (aValue != bValue) {
                    // changed Part start
                    if(options.sort[i].comparator){ 
                        return !!sort.descending == options.sort[i].comparator(aValue,bValue) ? -1 : 1;
                    }
                    else{
                        return !!sort.descending == aValue > bValue ? -1 : 1;
                    }
                    // changed Part end
                }
            }
            return 0;
        });
    }
    ...
    return results;
}

现在我可以将比较器添加到每一列并在我想要的位置定义它们:

声明性数据网格设置:

...
<td field="myArray" get="getsNameFromArray" comparator="comparesNames">Name</td>
...

比较器函数的 Javascript 定义(a 和 b 是“myArray”对象):

compareNames = function(a,b){
  return a.name > b.name;
}

getter 函数的 Javascript 定义(记录是整行并包含“myArray”对象):

getNamesFromArray= function(idx,record){
  return record.myArray.name;
}
于 2011-05-26T11:21:15.787 回答