0

I have a script that counts the number of rows in a table and assigns a value attribute to an input field. I am adding the draggable plugin Sortable to table rows, what would be the best way to run this script then on document ready, and on change. The first part works, but i am not getting alerted when the table rows change.

this is now my revised code:

function countRows(){
    var i = 0;
    $('#offices td input').each(function(){
        $(this).attr("value", ++i);
    });  
}
$(document).ready(countRows);

// Sortable rows
$('.sorted_table').sortable({
  containerSelector: 'table',
  itemPath: '> tbody',
  itemSelector: 'tr',
  placeholder: '<tr class="placeholder"/>'
})

$('.sorted_table').children("tbody").sortable({
    stop: function (event, ui) {
        countRows(); // re-number rows after sorting
    }
});
TEN Design
  • 717
  • 3
  • 13
  • 31

1 Answers1

1

Assuming you are using jQuery UI Sortable:

$('.sorted_table').children("tbody").sortable({
    stop: function (event, ui) {
        countRows(); // re-number rows after sorting
    }
});

jsFiddle demo here: http://jsfiddle.net/7vmf1c4L/

nothingisnecessary
  • 6,099
  • 36
  • 60