在 ExtJS 3 EditorGridPanel 中按渲染值排序

Posted

技术标签:

【中文标题】在 ExtJS 3 EditorGridPanel 中按渲染值排序【英文标题】:Sorting by the rendered value in an ExtJS 3 EditorGridPanel 【发布时间】:2012-04-04 01:36:38 【问题描述】:

我是 Ext 的新手,所以如果我的问题不够清楚/太基本,我提前道歉......

我有一个带有 Ext.data.Store 和 Ext.data.JsonReader 的 Ext.grid.EditorGridPanel。

我有以下问题:

我想让我的网格中的列可排序,但从我的商店返回的值不是我想要排序的值(返回的值是一个 ID,我想按这个字符串排序ID 映射到)。

我有一个渲染函数可以在这个 ID 和字符串值之间进行转换,但我不知道如何将它集成到我的代码中。

我找到了这篇文章:

http://www.sencha.com/forum/showthread.php?45324-Grid-column-sorting-on-rendered-value

这看起来与我的需要相似,但是当我尝试添加时:

name: 'my_field', sortType: my_render_function

对我的 JsonReader 来说它不起作用。

我哪里弄错了?

谢谢。


应佩特的要求:

var my_render = function(val, metaData, record, rowIndex, colIndex, store)
   if (val == null) 
       return '';
   var n = another_store.getById(val);
   if (n == null) 
       return '';
   return n.data.name;
;

var my_reader = new Ext.data.JsonReader(
   root: 'my_root',
   id: 'tissue_num',
   , [
   name: 'tissue_num',
   type: 'int'
   , 'tissue_desc', 'organ']
);

var my_store = new Ext.data.Store(
url: request_url,
baseParams: 
    _state: request_state,
    _action: 'get_conditions'
,
sortInfo: 
    field: 'tissue_num',
    direction: "ASC"
,
reader: my_reader,
pruneModifiedRecords: true
);

【问题讨论】:

你能提供一些代码吗?排序函数和 JSON 阅读器? 【参考方案1】:

由于您想要的不仅仅是显示转换,我不会在这里使用渲染器,而是convert 记录定义中的数据。

val convertName = function(val, rec) 
   // val will always be null since there's no field named 'name'
   var id = rec.data.tissue_num;
   if (id == null) 
       return '';
   var n = another_store.getById(id);
   if (n == null) 
       return '';
   return n.data.name;
;

var my_reader = new Ext.data.JsonReader(
   root: 'my_root',
   id: 'tissue_num',
   , [
   name: 'tissue_num',
   type: 'int'
   , name: 'name', convert: convertName,
   'tissue_desc', 'organ']
);

这将为您提供name 字段中的数据记录,您可以对从服务器获得的实际数据执行任何操作 - 排序、过滤、显示等。比试图找出每次需要时呈现价值。

【讨论】:

优秀的解决方案!!!这非常有效! convert 的使用确实是我所缺乏的。谢谢你,韦斯。

以上是关于在 ExtJS 3 EditorGridPanel 中按渲染值排序的主要内容,如果未能解决你的问题,请参考以下文章

ExtJS EditorGridPanel ~ 设置行高和单元格宽度以显示所有单元格内容

extjs动态向可编辑表格EditorGridPanel 添加一个列ColumnModel,并让列能够被修改

请问,extjs中怎样才能动态的设置EditorGridPanel的单元格不可编辑?

ExtJS 3.3 中的水平滚动网格

Ext.grid.EditorGridPanel 中的启用-禁用单列单元格

ExtJs 编辑器网格面板的问题