how to find column index using dataIndex Extjs 4
Asked Answered
D

7

11

Well in ExtJS 3 i used the following code:

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.

Desdamona answered 6/12, 2012 at 11:31 Comment(0)
M
33

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. ;-)

Minelayer answered 3/3, 2013 at 2:6 Comment(1)
I prefer this answer which uses a selector over the accepted answer that involves potentially iterating every column in the grid.Obreption
B
9

I guess you should find index by iterating through grid.columns array and comparing dataIndex property of each column.

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'));
Balliol answered 6/12, 2012 at 12:14 Comment(1)
Instead of grid.columns use grid.columnManager.columns. That way if the columns are reordered, your function returns the correct index.Perce
S
4

The most standard way to get a column by dataIndex would be:

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.

Ext.grid.column.Column docs:

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.

See also: getHeaderByDataIndex docs.

Stansbury answered 7/6, 2017 at 17:8 Comment(1)
This is the most "by the docs" method in Ext JS v6. Thanks!Nilsanilsen
C
2
function getColumnIndex(grid, dataIndex) {
    gridDataIndices = Ext.Array.pluck(grid.columns, 'dataIndex');

return Ext.Array.indexOf(gridDataIndices, desireDataIndex);
}

answered in sencha forums

Curare answered 13/10, 2015 at 9:42 Comment(0)
I
0
 var gridColumns = grid.headerCt.getGridColumns();
 for (var i = 0; i < gridColumns.length; i++) {
   if (gridColumns[i].dataIndex == 'yourdataIndex') {
    alert(i);
   }  
 }
Indium answered 6/12, 2012 at 13:4 Comment(0)
C
0

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.

 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,
Chemush answered 31/3, 2015 at 19:54 Comment(0)
P
0
var RowEditor = new Ext.grid.plugin.RowEditing({...});
    RowEditor.editor.form.findField('Task');
Pennoncel answered 5/10, 2015 at 14:29 Comment(1)
Please explain your answer so that it would be useful for future users.Frozen

© 2022 - 2024 — McMap. All rights reserved.