Javascript 如何使用dataIndex Extjs 4查找列索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13742723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to find column index using dataIndex Extjs 4
提问by Hmxa Mughal
Well in ExtJS 3 i used the following code:
在 ExtJS 3 中,我使用了以下代码:
grid.getColumnModel().findColumnIndex("Tasks")
grid.getColumnModel().findColumnIndex("Tasks")
I tried finding it on the api docs, but no luck...so how is ir possible that i can find the column index of the grid by the dataIndex of the column or the header name of that column.
我尝试在 api 文档上找到它,但没有运气......所以我怎么可能通过列的 dataIndex 或该列的标题名称找到网格的列索引。
回答by Murrah
You can use the component query:
您可以使用组件查询:
var fname = grid.down('[dataIndex=firstname]');
It took a while to work that out - there doesnt seem to be an example in the docs. ;-)
解决这个问题需要一段时间 - 文档中似乎没有示例。;-)
回答by Krzysztof
I guess you should find index by iterating through grid.columnsarray and comparing dataIndexproperty of each column.
我想您应该通过遍历grid.columns数组并比较dataIndex每列的属性来找到索引。
Example:
例子:
var findColumnIndex = function(columns, dataIndex) {
var index;
for (index = 0; index < columns.length; ++index) {
if (columns[index].dataIndex == dataIndex) { break; }
}????????
return index == columns.length ? -1 : index;
}
console.log(findColumnIndex(grid.columns, 'Task'));
console.log(findColumnIndex(grid.columns, 'Something'));
回答by Mohammadreza
function getColumnIndex(grid, dataIndex) {
gridDataIndices = Ext.Array.pluck(grid.columns, 'dataIndex');
return Ext.Array.indexOf(gridDataIndices, desireDataIndex);
}
回答by Nux
The most standard way to get a column by dataIndexwould be:
获取列的最标准方法dataIndex是:
var column = grid.columnManager.getHeaderByDataIndex('Tasks')
Note that this does return a column (contrary to the function name). This is because a grid header in ExtJS is actually both a header and a column contents.
请注意,这确实返回一列(与函数名称相反)。这是因为 ExtJS 中的网格标题实际上既是标题又是列内容。
This class specifies the definition for a column inside a Ext.grid.Grid. It encompasses both the grid header configuration as well as displaying data within the grid itself.
此类指定 Ext.grid.Grid 内的列的定义。它包含网格标题配置以及在网格本身内显示数据。
See also: getHeaderByDataIndex docs.
另请参阅:getHeaderByDataIndex 文档。
回答by Srinivas B
var gridColumns = grid.headerCt.getGridColumns();
for (var i = 0; i < gridColumns.length; i++) {
if (gridColumns[i].dataIndex == 'yourdataIndex') {
alert(i);
}
}
回答by 1-14x0r
Component Query can get a bit slow and wont guarantee only one result. Its a bit faster to just iterate over the array of columns that belong to the grid.
组件查询可能会有点慢,并且不能保证只有一个结果。仅迭代属于网格的列数组会更快一些。
Here is a simple static util function that does the trick using ext framework.
这是一个简单的静态 util 函数,它使用 ext 框架来解决这个问题。
findColumnDataIndex: function(columns, dataIndex) {
var column = null;
Ext.Array.each(columns, function(c) {
if (c.dataIndex === dataIndex) {
column = c;
return false;
}
}, this);
return column;
}
use this code to get the columns from your grid panels instance
使用此代码从您的网格面板实例中获取列
var columns = grid.headerCt.items.items,
回答by Macedonian
var RowEditor = new Ext.grid.plugin.RowEditing({...});
RowEditor.editor.form.findField('Task');

