Grid: How to get a column index


#1

I have a grid on the page which allows user to be able to drag and drops columns the way it makes sense for him.

How can i get a column index by id?


#2

Hello, You can make a function that passes the id, and traverses the columns of the grid, using the array’s findIndex() function.

I believe that the grid by itself does not have something specific for this. I hope it helps.

function getIndexById(idColumn) {
    return grid.config.columns.findIndex(function(element) { return element.id === idColumn});
}

#3

if the user has dragged some of the columns to another index this wont work because the columns indexing in the grid.config.columns are immutable.

Am i wrong?


#4

It is not immutable, if you change the column position by dragging and checking the columns, the index will be updated. I made an example snippet for testing.

https://snippet.dhtmlx.com/n36fq11x