gitguddoge
gitguddoge

Reputation: 172

Modifying row index javascript

I have a table that is able to sort the columns and it is working fine.

The rows in the table consists of parent and child rows which when click on the parent row will show the child rows. Otherwise the child rows will remain hidden.

I intend to sort the rows based on the parent rows but not the child rows result or else the table will be a mess. I try to use jQuery to remove all the child rows before sorting and append them after the sorting function but the sorting function for the column wont work for the second time. This could be the row indexes had messed up. I suspect this happened because when I remove and store child rows in a variable, the row indexes still remain. So when I re-insert the child rows based on the parent rows, the row indexes of those child rows will remain same.

Is there anyway to explicitly modify the row index of the rows without swapping their positions? Tried to use jQuery in such way as:

$('tr.child-row')[0].rowIndex = 3

But it just wont work.

Upvotes: 0

Views: 754

Answers (1)

Gary Chen
Gary Chen

Reputation: 278

You can change the order of two rows in a table.

http://jsfiddle.net/4v2owx37/2

This is a simple code that changes order of two rows.

function swap_position(first_index, second_index){

  if(second_index > first_index){
    $("tr").eq(first_index).insertBefore($("tr").eq(second_index));
    $("tr").eq(second_index).insertBefore($("tr").eq(first_index));
  }

  else if(first_index > second_index){
    $("tr").eq(second_index).insertBefore($("tr").eq(first_index));
    $("tr").eq(first_index).insertBefore($("tr").eq(second_index));
  }

}

swap_position(1,4);
swap_position(5,3);

Upvotes: 1

Related Questions