Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Matt on Dec 10, 2007 11:11
open dhtmlx forum
moving rows withing the same grid

Hello.

I'm writing to inquire about moving a row directly from one index to another within the same dhtmlXGrid. I've created a special sort algorithm that returns an array of row IDs in the order of the rows (ie. the index of the array becomes the new index for the particular row ID). I know there are the functions "moveRowUp" and "moveRowDown" but we only want to make the function call once. We're experiencing tremendous delays in moving rows.

Any help would be greatly appreciated.


- Matt
Answer posted by Support on Dec 11, 2007 02:39
If you not using any complex mode ( paging, split, smart rendering ) you can use direct dom manipulation to set new order
Code below is not exact solution, but shows necessary steps

var new_order= //array of IDs in necessary order
var count=grid.getRowsNum();

//remove all rows from DOM
for (var i=0; i<col; i++)
    grid.obj.rows[0].parentNode.removeChild(grid.obj.rows[1]);

//add in new order
for (var i=0; i<col; i++)
    grid.obj.rows[0].parentNode.appendChild(grid.rowsAr[new_order[i]]);

//store new order
grid.rowsCol=dhtmlxArray(new_order);


Answer posted by Matt on Dec 11, 2007 14:10
Thank you for such a speedy reply. This solution is much faster than the one I was using previously but I'm running into a new issue.
The scenario I outlined above is in a table that has implemented filtering. After rebuilding the table with this new solution I am receiving the following error:

        c.childNodes has no properties
           var c = this.getRowById(row_id);var cell=(c._childIndexes?c.childNodes[c._child...

Also, as I've mentioned before, I know that the functions "moveRowUp" and "moveRowDown" allows a row to be moved one index up or down. Is there a similar function that allows for movement any number of rows up/down? For instance, "moveRowUp(row_id, 5)" which would move a row up five places.

I am thankful for any help you can offer.
(In case it is pertinent, I am using version 1.4 Pro.)

Answer posted by Support on Dec 12, 2007 05:59
>> For instance, "moveRowUp(row_id, 5)" which would move a row up five places.
There is no exactly same functionality, but there is
    grid.moveRowTo(...
function, which allows to move row in necessary position
    grid.moveRowTo(sid,tid,"sibling","move")   - move row "sid" to position next of row "tid"

Answer posted by Support on Dec 12, 2007 06:07
>>The scenario I outlined above is in a table that has implemented filtering. After rebuilding the table with this new solution I am receiving the following error:
Are you using paging or smartrendering in grid ?
The mentioned error occurs when row can't be found by ID, but proposed code doesn't change collection of ID's so it must not cause error.

If problem still occurs for you - please send any kind of sample where error can be reconstructed. ( you can send it directly to support@dhtmlx.com )
Answer posted by Matt on Dec 12, 2007 12:35
The following is a code snippit that emulates moveRowUp/moveRowDown that moves any number of rows (to the targetIndex). We aren't handling something correctly but seem to have mimicked the functionality.

function moveRowToIndex(xTable, rowID, targetIndex) {
    var row = xTable.getRowById(rowID);
    var rIndex = xTable.rowsCol._dhx_find(row);
    var targetRow = xTable.getRowById(xTable.getRowId(targetIndex));

    if (targetIndex < rIndex) { //move up
        if ((row.previousSibling)&&(rIndex!=0)){
            xTable.rowsCol._dhx_swapItems(rIndex,targetIndex);
            row.parentNode.insertBefore(row,targetRow);
        }
    } else if (targetIndex > rIndex) { //move down
        if (row.nextSibling){
            xTable.rowsCol._dhx_swapItems(rIndex,targetIndex);
            if (targetRow.nextSibling) {
                row.parentNode.insertBefore(row,targetRow.nextSibling);
            } else {                                       
                row.parentNode.appendChild(row);
            }
        }      
    }
}

Answer posted by Support on Dec 13, 2007 02:11
You are setting incorrect order in rowsCol collection

function moveRowToIndex(xTable, rowID, targetIndex) {
    var row = xTable.getRowById(rowID);
    var rIndex = xTable.rowsCol._dhx_find(row);
    var targetRow = xTable.getRowById(xTable.getRowId(targetIndex));

    if (targetIndex < rIndex) { //move up
        if ((row.previousSibling)&&(rIndex!=0)){
            xTable.rowsCol._dhx_removeAt(rIndex);
            xTable.rowsCol._dhx_insertAt(targetIndex,row);
            row.parentNode.insertBefore(row,targetRow);
        }
    } else if (targetIndex > rIndex) { //move down
        if (row.nextSibling){
            xTable.rowsCol._dhx_insertAt(targetIndex+1,row);
            xTable.rowsCol._dhx_removeAt(rIndex);
            if (targetRow.nextSibling) {
                row.parentNode.insertBefore(row,targetRow.nextSibling);
            } else {                                       
                row.parentNode.appendChild(row);
            }
        }      
    }
}