46
46
//Calculate the index for the new row
47
47
var $curr_last_row = $("#usersForm").find('tbody').find('tr:last');
48
var $curr_first_row = $("#usersForm").find('tbody').find('tr:first');
49
var first_row_initial = $curr_first_row.find('label').html().substr(0, 1).toUpperCase();
50
var curr_shown_initial = $curr_last_row.find('label').html().substr(0, 1).toUpperCase();
51
var curr_last_row_index_string = $curr_last_row.find('input:checkbox').attr('id').match(/\d+/)[0];
52
var curr_last_row_index = parseFloat(curr_last_row_index_string);
53
var new_last_row_index = curr_last_row_index + 1;
48
if ($curr_last_row.length) {
49
// at least one tr exists inside the tbody
50
var $curr_first_row = $("#usersForm").find('tbody').find('tr:first');
51
var first_row_initial = $curr_first_row.find('label').html().substr(0, 1).toUpperCase();
52
var curr_shown_initial = $curr_last_row.find('label').html().substr(0, 1).toUpperCase();
53
var curr_last_row_index_string = $curr_last_row.find('input:checkbox').attr('id').match(/\d+/)[0];
54
var curr_last_row_index = parseFloat(curr_last_row_index_string);
55
var new_last_row_index = curr_last_row_index + 1;
56
var is_show_all = (first_row_initial != curr_shown_initial) ? true : false;
57
var $insert_position = $curr_last_row;
58
var dummy_tr_inserted = false;
60
// no tr exists inside the tbody
61
var $tbody = $("#usersForm").find('tbody');
63
$tbody.append('<tr></tr>');
64
var dummy_tr_inserted = true;
65
var $insert_position = $tbody.find('tr:first');
66
var is_show_all = true;
67
//todo: the case when the new user's initial does not match
68
// the currently selected initial
69
var curr_shown_initial = '';
70
var new_last_row_index = 0;
54
72
var new_last_row_id = 'checkbox_sel_users_' + new_last_row_index;
55
var is_show_all = (first_row_initial != curr_shown_initial) ? true : false;
57
74
//Append to the table and set the id/names correctly
58
75
if ((curr_shown_initial == new_user_initial) || is_show_all) {
60
.insertAfter($curr_last_row)
77
.insertAfter($insert_position)
61
78
.find('input:checkbox')
62
79
.attr('id', new_last_row_id)