我正在使用以下符号在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。 到目前为止,这可以显示正确的值,但是然后排序 使用基础对象。
我认为可能有一种方法可以进一步推动这一点,从而超越排序 该表格,但我希望尽可能简单。
此外,我希望防止弹出性能问题。
任何想法?
答案 0 :(得分: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参数始终为null。
这可能是因为使用了新的Store API(store.Memory)而不是ItemFileReadStore,但我还没有成功解决这个难题。
此外,我无法使用这种新方法测试Gird排序,所以我不会标记 这个溶剂已经解决了。
<强>更新强>
myGrid.getContactName = function(colIndex,record){
if(record)return record.contact.name;
else return null;
};
一旦我避免了空案例,就可以这样工作。
但是,Grid的客户端类似似乎不能通过get函数访问,而是直接使用该字段。这样可以防止在嵌套字段上进行正确排序。
<强>更新强>
我终于解决了我的问题:
第一个问题:指定DataGrid字段的嵌套数据已经全部使用get-function解决,以深入到数组子结构中。 (如上所述)
然而,排序仍取决于字段属性。 如果field属性包含数组的名称,则此列将无法正确排序。
我不得不修改一些dojo类来适应它。我稍后会用更模块化的形式,但现在是原始结果:
首先,我需要在Grid Definition中允许定义一个addtional比较器回调。 为此,我在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”是我上面修改过的Cell。
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;
}
现在我可以为每列添加比较器并将它们定义在我想要的位置:
声明性DataGrid设置:
...
<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;
}