3.2.0

Re-sorting after inserting/deleting rows

Is there a way of maintaining the grid sort after inserting or deleting rows. I have tried using the following code but it does not work.

Thanks in advance for any help.

function addRow()
{
    var rowData = ["1", "2", "3", "4", "5"];
    myData.push(rowData);

    // re-initialize the data values
    obj.setDataProperty("count", myData.length);

    // re-initialize the row values
    var newCount = obj.getDataProperty("count");
    var rowValues = [];
    var sort_value = obj.getSortProperty("index");
    
    for(var i=0; i < newCount; ++i) { rowValues[i] = i; }
    obj.setRowProperty("values", rowValues);
    obj.setSortProperty("index", -1);
    obj.setSortProperty("index", sort_value);

    // refresh the grid
    obj.refresh();
}
Rag!
February 26,
After some experimentation I have solved my problem and though it would be worth sharing my results.

The functions below can be used to maintain the current sort whilst inserting or deleting rows from the grid. It should be noted that when inserting the new row always appears at the end of the grid.

Hope someone finds this useful.

function addRow()
{
    var rowData = ["", "", "", "", ""];

    // Save existing sort values
    var array = obj.getRowProperty("values");

    // Add an extra row
    myData.push(rowData);

    // re-initialize the data values
    obj.setDataProperty("count", myData.length);

    // Reset sort property (does not re-sort propertly if this is not done
    obj.setSortProperty("index", -1);

    // Put back the sort values (new row stays at bottom of grid)
    array.push(myData.length-1);
    obj.setRowProperty("values", array);

    // refresh the grid
    obj.refresh();
}

function removeRow()
{
    var index = obj.getProperty("selection/index");

    if (index != -1 && confirm("Are you sure?"))
    {
        // Get existing sort values, re-number then deleted element
        var array = obj.getRowProperty("values");
        var splice_point=-2;
        var new_selected_row=-1;
 
        for (var i=0;i<array.length;i++)
        {
            if (array[i] == index)
                splice_point=i;

            if (array[i] > index)
                array[i]--;

            if (splice_point == i-1)
                new_selected_row=array[i];
        }

        // Check the values are ok just to be sure
        if (splice_point >= 0)
            array.splice(splice_point,1);
        if (new_selected_row == -1)
            new_selected_row=array[array.length-1];

        myData.splice(index,1);
        obj.setRowProperty("count", myData.length);

        // re-initialize the data values
        obj.setDataProperty("count", myData.length);

        // Put back the sort values
        obj.setRowProperty("values", array);
        obj.setProperty("selection/index",new_selected_row);

        // refresh the grid
        obj.refresh();
    }
}
Rag!
February 27,
thanks rag
I don't understand everything but it works perfect.
it answers my last question

you are great
Thierry
July 4,
Great job on this add and remove function. I can use this perfectly (thumbs up).
Remco
July 30,
I need to do the same thing, but with multiple rows. I select rows 1,3,4 and then I want to click a button to remove the rows. How can I do that?
Vurk
March 22,
Hi

I tried to use the example given by Rag! to insert a new row via javascript in a grid but it doesn't works in my scripts. What I'm doing wrong ?

This is the code:
vArray = split(pResponseText, ";")
obj_data.push(vArray);
obj.setDataProperty("count", obj_data.length);
obj.setRowCount(obj_data.length);
obj.refresh();

When I run the script, I receive the error message: obj_data[...] is null or not an object.

Thank you very much!
jinxs_78
April 10,
Hi,

Thanks for the script. However, how to put the new row on the top of the grid?

Thanks.
flashsnake
July 6,

This topic is archived.

See also:


Back to support forum