export/serialize selected rows

I’m currently working with v.2.1 build 90226 pro. What I’m trying to do is this:



I have a grid setup with movable columns and multi select abilities. It’s populated by data from a database. When I select one, two, three or 100 different records from the grid at random and hit an “Export” button, I need to dump all the selected data to a CSV/Excel file. I’ve experimented a bit with serialize, and it works great for maintaining current sort order and column order, but haven’t been able to get it to serialize only my selected rows. I see it’s possible to serialize certain columns, is this also true for rows?



An alternative is for me to capture the selected row ID numbers AND get the current sorting state and column order. I can then query the database with the information and build the CSV/Excel file from there. I’m having issues with this because of the following:



getSelectedRowID() works for getting the row ID numbers, but it doesn’t maintain the sort order that’s current on the grid. For example, the order of the IDs returned by that method will not change regardless of the sort order they’re displayed in on the grid (1, 3, 5 … resort … still returns 1, 3, 5).



getSortingState() works for getting the current sort order, however, it only returns the ID of the column relative to its position on the grid. Example: If I have a grid setup with columns such as: Name, Date, Position - and it’s sorted by Date ASC, running getSortingState() will return 1, asc. Now if I reorder the grid so the columns are: Date, Name, Position and re-run getSortingState() it returns 0, asc. Is there a way to return an ID number, or perhaps column label (getHeaderCol() ??), that’s not relative to the position of the column on the grid?



Thanks for your help!

Unfortunately dhtmlxGrid hasn’t appropriate functionality to serialize only necessary rows.
You can get id’s of the selected rows in the soring order with following code:

var ids = grid.getSelectedId().split(",")
ids.sort(function(a,b){
grid.getRowIndex(a)>grid.getRowIndex(b)?1:-1
})

Great, thank you.

My last question is related to the column display order (not necessarily sort order). I have grid tied into a database and it dynamically pulls columns to display based on user preferences. There are a total of about 25 possible columns to display. Each column has a unique ID number in the database. To get these unique ID numbers to correlate with the appropriate column in grid, do I need to use setColumnId method? Taking an example from my previous question:

If I have a column order like:
Name, Date, Position
The unique ID number in the database for the columns are 0, 3, 10

How can I assign the unique numbers to the specific columns? So that when I change the order of the columns and maybe add one:
Date, Position, Date Registered, Name
The list of IDs would change to 3, 10, 14, 0

Ultimately, I need to get a list of the column ID numbers, in the order they’re displayed on the grid. I will use the block of code you sent me to gather the selected records from our database, sort them appropriately, and order the columns the same way they are on the grid.

To sum up the entire goal of what we’re trying to do:
Export only the SELECTED records to an Excel spreadsheet in the same sort order and column order that they have on the grid.

Thanks so much in advance for any assistance you can provide.


Yes, you can use column IDs for that, when columns moved , their indexes are changed by IDs are preserved

mygrid.setColumnIds(“0,3,10”)

… moving column …


var final = [];
for (var i=0; i<mygrid.getColumnsNum(); i++)
final.push(mygrid.getColumnId(i))
final = final.join(","); //new order of column’s IDs