locked
deleting an item in array without reindexing RRS feed

  • Question

  • User640374800 posted

    i have a scenario whereby i am highlighting a last selected item in a grid when a user returns to the page.

    this works on comparing rowid of each row to the last added rowid in an array and only adding relevant class to that row.

    however if any deselections of rows are made and rowids removed from array it gets reindexed and the previous "last" rowid is no longer in that position so a different row will be highlighted.

    this is due to splice being used to remove the rowid - 

    this.RowIds().splice($.inArray(rowId, this.RowIds()), 1);

    is there a way to replace this removal method with one whereby the original element order would stay unchanged?

    Thursday, June 20, 2019 10:06 AM

All replies

  • User-1038772411 posted

    Hello mark1961,

    Try with this ,

    You can use slice() . Try with this below example.

    var myArray = ["one","two","three"];
    var cloneArray = myArray.slice();
    
    myArray.splice(1,1);
    
    document.write(myArray);       
    document.write(cloneArray);

    For more information please refer this below link
    http://www.tothenew.com/blog/javascript-splice-vs-slice/

    or try with this 
    I
    nstead of removing the items from the array with splice(), why not just set the value to null or  undefined , Then when you're adding a new user, you can just scan through the array to find the first available slot.
    https://stackoverflow.com/questions/17387571/javascript-array-splice-without-changing-the-index/17387599#17387599

    Thank  you.

    Thursday, June 20, 2019 10:55 AM
  • User640374800 posted

    this doesnt remove anything when i try it

    Thursday, June 20, 2019 12:03 PM
  • User-1174608757 posted

    Hi mark1961,

    Could you please share your code both in front end and code behind? It will help us reproduce your problem.

    Best Regards

    Wei

    Friday, June 21, 2019 6:01 AM
  • User640374800 posted

    this.ExpandedRowIds().splice($.inArray(rowId, this.ExpandedRowIds()), 1);

    im wanting to maintain the order that rows are selected. by being able to recognise last row selected this row is then "highlighted" on return to the page.

    I want to use the last row id in the array to identify this row, this only works until one is deselected as the above code reindexes the array so the previous "last" row id can be in a different place in the array.

    so i want to remove ids from the array but maintain the same order of how they were added, then i can reliably use the last id in the array regardless of which ones are deselected.

    this method would also work in highlighting the previous selected row if its the last row deselected....

    does this make sense?

    Monday, June 24, 2019 7:15 AM
  • User-474980206 posted

    splice does not change the order, it only removes/adds to array. its suspicious that you are call splice on an array returned by a function.

    Monday, June 24, 2019 2:20 PM