Gautam
Gautam

Reputation: 255

how to sort elements in the array: jQuery

Summary: I have a table where rows are added for each btn click. I am using jquery-ui sortable() function to move/sort the rows.

Let say i have this:

<tbody>
  <tr><td>A</td></tr>
  <tr><td>B</td></tr>
  <tr><td>C</td></tr>
  <tr><td>D</td></tr>
  <tr><td>E</td></tr>
  <tr><td>F</td></tr>
</tbody>

Issue: I can move the rows using sortable() function. But i am not able to update the elements in the rows. i am using this

$('.report-configuration .configuration-table tbody').sortable({
'change': function(event, ui){
    arr=['a','b','c','d','e','f'];
    console.log(ui);
    var tmp = arr.splice(ui.originalPostion);//i am missing logic here
    arr.splice(ui.postion, tmp);
    console.log(arr);

}});

I am looking for the logic to update the positions in the array.

Upvotes: 1

Views: 69

Answers (1)

Jasper Seinhorst
Jasper Seinhorst

Reputation: 1074

After sortable has ended the html is also changed. With jQuery you can select the changed html and determine the correct sequence.

In your change callback function do something like this, note arr has the new array with correct sequence:

var arr = []
$(this).find('td').each(function() {
  arr.push($(this).html());
});
console.log(arr);

Upvotes: 5

Related Questions