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
    }
});
13
  • Probably because you are running countRows before you bind the alert Commented Oct 23, 2014 at 1:31
  • the bind is not related to the countrows, whats not happening is that there is no alert when the DOM is modified. once i get that working i will run the count rows function within it. Commented Oct 23, 2014 at 1:33
  • You need to post more code then. Commented Oct 23, 2014 at 1:33
  • actually this is all the code there is to post the CSS class "sorted_table" is assigned to the <table> element, so any changes in the nested table should be heard by th DOMSubtreeModified correct? Commented Oct 23, 2014 at 1:37
  • Yes, I'm asking for more code to see when/where/how that table gets modified. Commented Oct 23, 2014 at 1:39

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.