From 3ab3b35a76b6000cf8748b6e64c2adbbe8c80818 Mon Sep 17 00:00:00 2001 From: ninadsp Date: Tue, 3 Aug 2010 20:22:36 +0530 Subject: [PATCH] Created a function to sort a table after a new row has been appended to it. Required for Create Table, Add User and Create Database actions --- js/functions.js | 69 +++++++++++++++++++++++++---------------- js/server_privileges.js | 29 +---------------- 2 files changed, 43 insertions(+), 55 deletions(-) diff --git a/js/functions.js b/js/functions.js index 5dfb27e75..b54983579 100755 --- a/js/functions.js +++ b/js/functions.js @@ -1827,6 +1827,47 @@ jQuery.fn.PMA_confirm = function(question, url, callbackFn) { .dialog({buttons: button_options}); }; +/** + * 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(); + + //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 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'); +} + /** * jQuery coding for 'Create Table'. Used on db_operations.php, * db_structure.php and db_tracking.php (i.e., wherever @@ -1896,33 +1937,7 @@ $(document).ready(function() { .appendTo(tables_table); //sort the table - var rows = $(tables_table).find('tr').get(); - - $.each(rows, function(index, row) { - row.sortKey = $(row).find('th').text().toLowerCase(); - }) - - rows.sort(function(a,b) { - if(a.sortKey < b.sortKey) { - return -1; - } - if(a.sortKey > b.sortKey) { - return 1; - } - return 0; - }) - - $.each(rows, function(index, row) { - $(tables_table).append(row); - row.sortKey = null; - }) - - //fix the row classes - $(tables_table) - .find('tr:even') - .removeClass('odd').addClass('even') - .end().find('tr:odd') - .removeClass('even').addClass('odd'); + PMA_sort_table(tables_table, 'th'); //Refresh navigation frame window.parent.refreshNavigation(); diff --git a/js/server_privileges.js b/js/server_privileges.js index 30a566d2e..431b79b2a 100755 --- a/js/server_privileges.js +++ b/js/server_privileges.js @@ -127,34 +127,7 @@ function appendNewUser(new_user_string, new_user_initial, new_user_initial_strin .end(); //Let us sort the table alphabetically - var rows = $("#usersForm").find('tbody tr').get(); - - $.each(rows, function(index, row) { - row.sortKey = $(row).find('label').text().toLowerCase(); - }) - - rows.sort(function(a,b) { - if(a.sortKey < b.sortKey) { - return -1; - } - if(a.sortKey > b.sortKey) { - return 1; - } - return 0; - }) - - $.each(rows, function(index, row) { - $('#usersForm').find('tbody').append(row); - row.sortKey = null; - }) - - //Re-check the classes of each row - $("#usersForm") - .find('tbody').find('tr:odd') - .removeClass('even').addClass('odd') - .end() - .find('tr:even') - .removeClass('odd').addClass('even'); + PMA_sort_table($("#usersForm").find('tbody'), 'label'); $("#initials_table").find('td:contains('+new_user_initial+')') .html(new_user_initial_string);