:s based on the "position"-field values.
// This is a very simple ordering which only works with correct position number sequences,
// which the rest of this script (hopefully) guarantees.
rows = [];
table.find('tbody tr').each(function() {
position = $(this).find('td.' + position_field + ' input').val();
rows[position] = $(this);
// Add move cursor to table row.
// Also remove row coloring, as it confuses when using drag-and-drop for ordering
table.find('tr:has(td)').css('cursor', 'move').removeClass('row1').removeClass('row2');
});
for (var i in rows) { table.append(rows[i]); } // Move
to its correct position
update_positions($(this), true);
}
else
position_field = '';
});
});
// Function for creating fancy delete buttons
function create_delete_button(td)
{
// Replace checkbox with image
td.find('input:checkbox').hide();
td.append('' + delete_link_html + '');
td.find('a.delete').click(function(){
current_row = $(this).parent('td').parent('tr');
table = current_row.parent().parent();
if (current_row.is('.has_original')) // This row has already been saved once, so we must keep checkbox
{
$(this).prev('input').attr('checked', true);
current_row.addClass('deleted_row').hide();
}
else // This row has never been saved so we can just remove the element completely
{
current_row.remove();
}
update_positions(table, true);
}).removeAttr('href').css('cursor', 'pointer');
}
// Updates "position"-field values based on row order in table
function update_positions(table, update_ids)
{
even = true
num_rows = 0
position = 0;
// Set correct position: Filter through all trs, excluding first th tr and last hidden template tr
table.find('tbody tr:not(.add_template):not(.deleted_row)').each(function() {
if (position_field != '')
{
// Update position field
$(this).find('td.' + position_field + ' input').val(position + 1);
position++;
}
else
{
// Update row coloring
$(this).removeClass('row1 row2');
if (even)
{
$(this).addClass('row1');
even = false;
}
else
{
$(this).addClass('row2');
even = true;
}
}
});
table.find('tbody tr.has_original').each(function() {
num_rows++;
});
table.find('tbody tr:not(.has_original):not(.add_template)').each(function() {
if (update_ids) update_id_fields($(this), num_rows);
num_rows++;
});
table.find('tbody tr.add_template').each(function() {
if (update_ids) update_id_fields($(this), num_rows)
num_rows++;
});
table.parent().parent('div.tabular').find("input[id$='TOTAL_FORMS']").val(num_rows);
}
// Updates actual id and name attributes of inputs, selects and so on.
// Required for Django validation to keep row order.
function update_id_fields(row, new_position)
{
// Fix IDs, names etc.
//