Converted the PMA_sort_table into a jQuery function as there was a race condition caused due to simultaneous adding of a row and sorting of the table

This commit is contained in:
ninadsp
2010-08-03 21:01:20 +05:30
parent 3ab3b35a76
commit a7f2cbd2ba
2 changed files with 46 additions and 34 deletions

View File

@@ -1828,44 +1828,46 @@ jQuery.fn.PMA_confirm = function(question, url, callbackFn) {
};
/**
* Function to sort a table's body after a new row has been appended to it.
* jQuery function to sort a table's body after a new row has been appended to it.
* Also fixes the even/odd classes of the table rows at the end.
*
* @param jQuery obj table_body jQuery object that refers to the table's body
* @param string text_selector string to select the sortKey's text
*/
function PMA_sort_table(table_body, text_selector) {
//collect all rows
var rows = $(table_body).find('tr').get();
jQuery.fn.PMA_sort_table = function(text_selector) {
return this.each(function() {
var table_body = $(this);
//collect all rows
var rows = $(this).find('tr').get();
//get the text of the field that we will sort by
$.each(rows, function(index, row) {
row.sortKey = $(row).find(text_selector).text().toLowerCase();
//get the text of the field that we will sort by
$.each(rows, function(index, row) {
row.sortKey = $.trim($(row).find(text_selector).text().toLowerCase());
})
//get the sorted order
rows.sort(function(a,b) {
if(a.sortKey < b.sortKey) {
return -1;
}
if(a.sortKey > b.sortKey) {
return 1;
}
return 0;
})
//pull out each row from the table and then append it according to it's order'
$.each(rows, function(index, row) {
$(table_body).append(row);
row.sortKey = null;
})
//Re-check the classes of each row
$(this).find('tr:odd')
.removeClass('even').addClass('odd')
.end()
.find('tr:even')
.removeClass('odd').addClass('even');
})
//get the sorted order
rows.sort(function(a,b) {
if(a.sortKey < b.sortKey) {
return -1;
}
if(a.sortKey > b.sortKey) {
return 1;
}
return 0;
})
//pull out each row from the table and then append it according to it's order'
$.each(rows, function(index, row) {
$(table_body).append(row);
row.sortKey = null;
})
//Re-check the classes of each row
$(table_body).find('tr:odd')
.removeClass('even').addClass('odd')
.end()
.find('tr:even')
.removeClass('odd').addClass('even');
}
/**
@@ -1936,8 +1938,10 @@ $(document).ready(function() {
.end()
.appendTo(tables_table);
$(tables_table).PMA_sort_table('th');
//sort the table
PMA_sort_table(tables_table, 'th');
//PMA_sort_table(tables_table, 'th');
//Refresh navigation frame
window.parent.refreshNavigation();
@@ -2081,6 +2085,14 @@ $(document).ready(function() {
$.post($(this).attr('action'), $(this).serialize(), function(data) {
if(data.success == true) {
PMA_ajaxShowMessage(data.message);
//Append database's row to table
$("#tabledatabases")
.find('tbody')
.append(data.new_db_string)
.PMA_sort_table('.name');
//PMA_sort_table($("#tabledatabases tbody"), '.name');
}
else {
PMA_ajaxShowMessage(data.error);

View File

@@ -127,7 +127,7 @@ function appendNewUser(new_user_string, new_user_initial, new_user_initial_strin
.end();
//Let us sort the table alphabetically
PMA_sort_table($("#usersForm").find('tbody'), 'label');
$("#usersForm").find('tbody').PMA_sort_table('label');
$("#initials_table").find('td:contains('+new_user_initial+')')
.html(new_user_initial_string);