diff options
Diffstat (limited to 'lib/jquery.jtable.js')
-rw-r--r-- | lib/jquery.jtable.js | 281 |
1 files changed, 164 insertions, 117 deletions
diff --git a/lib/jquery.jtable.js b/lib/jquery.jtable.js index 9cee197..0db71c6 100644 --- a/lib/jquery.jtable.js +++ b/lib/jquery.jtable.js @@ -1,6 +1,6 @@ /* -jTable 2.2.0 +jTable 2.2.1 http://www.jtable.org --------------------------------------------------------------------------- @@ -48,6 +48,7 @@ THE SOFTWARE. dialogHideEffect: 'fade', showCloseButton: false, loadingAnimationDelay: 500, + saveUserPreferences: true, ajaxSettings: { type: 'POST', @@ -133,6 +134,8 @@ THE SOFTWARE. this._createBusyPanel(); this._createErrorDialogDiv(); this._addNoDataRow(); + + this._cookieKeyPrefix = this._generateCookieKeyPrefix(); }, /* Normalizes some options for all fields (sets default values). @@ -890,22 +893,22 @@ THE SOFTWARE. _parseDate: function (dateString) { if (dateString.indexOf('Date') >= 0) { //Format: /Date(1320259705710)/ return new Date( - parseInt(dateString.substr(6)) + parseInt(dateString.substr(6), 10) ); } else if (dateString.length == 10) { //Format: 2011-01-01 return new Date( - parseInt(dateString.substr(0, 4)), - parseInt(dateString.substr(5, 2)) - 1, - parseInt(dateString.substr(8, 2)) + parseInt(dateString.substr(0, 4), 10), + parseInt(dateString.substr(5, 2), 10) - 1, + parseInt(dateString.substr(8, 2), 10) ); } else if (dateString.length == 19) { //Format: 2011-01-01 20:32:42 return new Date( - parseInt(dateString.substr(0, 4)), - parseInt(dateString.substr(5, 2)) - 1, - parseInt(dateString.substr(8, 2)), - parseInt(dateString.substr(11, 2)), - parseInt(dateString.substr(14, 2)), - parseInt(dateString.substr(17, 2)) + parseInt(dateString.substr(0, 4), 10), + parseInt(dateString.substr(5, 2), 10) - 1, + parseInt(dateString.substr(8, 2, 10)), + parseInt(dateString.substr(11, 2), 10), + parseInt(dateString.substr(14, 2), 10), + parseInt(dateString.substr(17, 2), 10) ); } else { this._logWarn('Given date is not properly formatted: ' + dateString); @@ -1098,6 +1101,72 @@ THE SOFTWARE. _getKeyValueOfRecord: function (record) { return record[this._keyField]; }, + + /************************************************************************ + * COOKIE * + *************************************************************************/ + + /* Sets a cookie with given key. + *************************************************************************/ + _setCookie: function (key, value) { + key = this._cookieKeyPrefix + key; + + var expireDate = new Date(); + expireDate.setDate(expireDate.getDate() + 30); + document.cookie = encodeURIComponent(key) + '=' + encodeURIComponent(value) + "; expires=" + expireDate.toUTCString(); + }, + + /* Gets a cookie with given key. + *************************************************************************/ + _getCookie: function (key) { + key = this._cookieKeyPrefix + key; + + var equalities = document.cookie.split('; '); + for (var i = 0; i < equalities.length; i++) { + if (!equalities[i]) { + continue; + } + + var splitted = equalities[i].split('='); + if (splitted.length != 2) { + continue; + } + + if (decodeURIComponent(splitted[0]) === key) { + return decodeURIComponent(splitted[1] || ''); + } + } + + return null; + }, + + /* Generates a hash key to be prefix for all cookies for this jtable instance. + *************************************************************************/ + _generateCookieKeyPrefix: function () { + + var simpleHash = function (value) { + var hash = 0; + if (value.length == 0) { + return hash; + } + + for (var i = 0; i < value.length; i++) { + var ch = value.charCodeAt(i); + hash = ((hash << 5) - hash) + ch; + hash = hash & hash; + } + + return hash; + }; + + var strToHash = ''; + if (this.options.tableId) { + strToHash = strToHash + this.options.tableId + '#'; + } + + strToHash = strToHash + this._columnList.join('$') + '#c' + this._$table.find('thead th').length; + return 'jtable#' + simpleHash(strToHash); + }, /************************************************************************ * EVENT RAISING METHODS * @@ -2877,10 +2946,9 @@ THE SOFTWARE. _create: function () { if (this.options.selecting && this.options.selectingCheckboxes) { ++this._firstDataColumnOffset; + this._bindKeyboardEvents(); } - this._bindKeyboardEvents(); - //Call base method base._create.apply(this, arguments); }, @@ -2891,18 +2959,18 @@ THE SOFTWARE. var self = this; //Register to events to set _shiftKeyDown value $(document) - .keydown(function(event) { + .keydown(function (event) { switch (event.which) { - case 16: - self._shiftKeyDown = true; - break; + case 16: + self._shiftKeyDown = true; + break; } }) - .keyup(function(event) { + .keyup(function (event) { switch (event.which) { - case 16: - self._shiftKeyDown = false; - break; + case 16: + self._shiftKeyDown = false; + break; } }); }, @@ -2916,7 +2984,7 @@ THE SOFTWARE. selectedRows: function () { return this._getSelectedRows(); }, - + /* Makes row/rows 'selected'. *************************************************************************/ selectRows: function ($rows) { @@ -2955,21 +3023,27 @@ THE SOFTWARE. /* Overrides base event to store selection list *************************************************************************/ _onLoadingRecords: function () { - this._storeSelectionList(); + if (this.options.selecting) { + this._storeSelectionList(); + } + base._onLoadingRecords.apply(this, arguments); }, /* Overrides base event to restore selection list *************************************************************************/ _onRecordsLoaded: function () { - this._restoreSelectionList(); + if (this.options.selecting) { + this._restoreSelectionList(); + } + base._onRecordsLoaded.apply(this, arguments); }, /* Overrides base event to check is any selected row is being removed. *************************************************************************/ _onRowsRemoved: function ($rows, reason) { - if ((reason != 'reloading') && this.options.selecting && ($rows.filter('.jtable-row-selected').length > 0)) { + if (this.options.selecting && (reason != 'reloading') && ($rows.filter('.jtable-row-selected').length > 0)) { this._onSelectionChanged(); } @@ -2994,13 +3068,13 @@ THE SOFTWARE. self._$selectAllCheckbox = $('<input type="checkbox" />') .appendTo($headerContainer) - .click(function() { + .click(function () { if (self._$tableRows.length <= 0) { self._$selectAllCheckbox.attr('checked', false); return; } - var allRows = self._$tableBody.find('tr.jtable-data-row'); + var allRows = self._$tableBody.find('>tr.jtable-data-row'); if (self._$selectAllCheckbox.is(':checked')) { self._selectRows(allRows); } else { @@ -3058,7 +3132,7 @@ THE SOFTWARE. *************************************************************************/ _getSelectedRows: function () { return this._$tableBody - .find('.jtable-row-selected'); + .find('>tr.jtable-row-selected'); }, /* Adds selectable feature to a row. @@ -3075,10 +3149,8 @@ THE SOFTWARE. //'select/deselect' checkbox column if (self.options.selectingCheckboxes) { - var $cell = $('<td></td>') - .addClass('jtable-selecting-column'); - var $selectCheckbox = $('<input type="checkbox" />') - .appendTo($cell); + var $cell = $('<td></td>').addClass('jtable-selecting-column'); + var $selectCheckbox = $('<input type="checkbox" />').appendTo($cell); if (!self.options.selectOnRowClick) { $selectCheckbox.click(function () { self._invertRowSelection($row); @@ -3147,14 +3219,13 @@ THE SOFTWARE. /* Makes row/rows 'selected'. *************************************************************************/ _selectRows: function ($rows) { - if (!this.options.multiselect) { this._deselectRows(this._getSelectedRows()); } $rows.addClass('jtable-row-selected'); if (this.options.selectingCheckboxes) { - $rows.find('td.jtable-selecting-column input').attr('checked', true); + $rows.find('>td.jtable-selecting-column >input').prop('checked', true); } this._refreshSelectAllCheckboxState(); @@ -3165,7 +3236,7 @@ THE SOFTWARE. _deselectRows: function ($rows) { $rows.removeClass('jtable-row-selected'); if (this.options.selectingCheckboxes) { - $rows.find('td.jtable-selecting-column input').removeAttr('checked'); + $rows.find('>td.jtable-selecting-column >input').prop('checked', false); } this._refreshSelectAllCheckboxState(); @@ -3196,7 +3267,7 @@ THE SOFTWARE. /************************************************************************ * EVENT RAISING METHODS * *************************************************************************/ - + _onSelectionChanged: function () { this._trigger("selectionChanged", null, {}); } @@ -3266,6 +3337,7 @@ THE SOFTWARE. _create: function () { base._create.apply(this, arguments); if (this.options.paging) { + this._loadPagingSettings(); this._createBottomPanel(); this._createPageListArea(); this._createGotoPageInput(); @@ -3273,6 +3345,19 @@ THE SOFTWARE. } }, + /* Loads user preferences for paging. + *************************************************************************/ + _loadPagingSettings: function () { + if (!this.options.saveUserPreferences) { + return; + } + + var pageSize = this._getCookie('page-size'); + if (pageSize) { + this.options.pageSize = this._normalizeNumber(pageSize, 1, 1000000, this.options.pageSize); + } + }, + /* Creates bottom panel and adds to the page. *************************************************************************/ _createBottomPanel: function () { @@ -3413,7 +3498,20 @@ THE SOFTWARE. var currentPageCount = this._calculatePageCount(); if (oldPageCount != currentPageCount) { this._$gotoPageInput.empty(); - for (var i = 1; i <= currentPageCount; i++) { + + //Skip some pages is there are too many pages + var pageStep = 1; + if (currentPageCount > 10000) { + pageStep = 100; + } else if (currentPageCount > 5000) { + pageStep = 10; + } else if (currentPageCount > 2000) { + pageStep = 5; + } else if (currentPageCount > 1000) { + pageStep = 2; + } + + for (var i = pageStep; i <= currentPageCount; i += pageStep) { this._$gotoPageInput.append('<option value="' + i + '">' + i + '</option>'); } @@ -3461,6 +3559,9 @@ THE SOFTWARE. if (this._currentPageNo > pageCount) { this._currentPageNo = pageCount; } + if (this._currentPageNo <= 0) { + this._currentPageNo = 1; + } //if user sets one of the options on the combobox, then select it. var $pageSizeChangeCombobox = this._$bottomPanel.find('.jtable-page-size-change select'); @@ -3473,9 +3574,20 @@ THE SOFTWARE. } } + this._savePagingSettings(); this._reloadTable(); }, + /* Saves user preferences for paging + *************************************************************************/ + _savePagingSettings: function () { + if (!this.options.saveUserPreferences) { + return; + } + + this._setCookie('page-size', this.options.pageSize); + }, + /* Overrides _createRecordLoadUrl method to add paging info to URL. *************************************************************************/ _createRecordLoadUrl: function () { @@ -3525,10 +3637,13 @@ THE SOFTWARE. /* Overrides _onRecordsLoaded method to to do paging specific tasks. *************************************************************************/ _onRecordsLoaded: function (data) { - this._totalRecordCount = data.TotalRecordCount; - this._createPagingList(); - this._createPagingInfo(); - this._refreshGotoPageInput(); + if (this.options.paging) { + this._totalRecordCount = data.TotalRecordCount; + this._createPagingList(); + this._createPagingInfo(); + this._refreshGotoPageInput(); + } + base._onRecordsLoaded.apply(this, arguments); }, @@ -3552,7 +3667,7 @@ THE SOFTWARE. /* Creates and shows the page list. *************************************************************************/ _createPagingList: function () { - if (!this.options.paging || this.options.pageSize <= 0) { + if (this.options.pageSize <= 0) { return; } @@ -3579,13 +3694,13 @@ THE SOFTWARE. .html('<<') .data('pageNumber', 1) .appendTo(this._$pagingListArea); - + var $previous = $('<span></span>') .addClass('jtable-page-number-previous') .html('<') .data('pageNumber', this._currentPageNo - 1) .appendTo(this._$pagingListArea); - + if (this._currentPageNo <= 1) { $first.addClass('jtable-page-number-disabled'); $previous.addClass('jtable-page-number-disabled'); @@ -3638,8 +3753,8 @@ THE SOFTWARE. .html(pageNumber) .data('pageNumber', pageNumber) .appendTo(this._$pagingListArea); - - if(this._currentPageNo == pageNumber) { + + if (this._currentPageNo == pageNumber) { $pageNumber.addClass('jtable-page-number-active'); $pageNumber.addClass('jtable-page-number-disabled'); } @@ -3713,7 +3828,7 @@ THE SOFTWARE. }); }, - /* Changes current page tp given value. + /* Changes current page to given value. *************************************************************************/ _changePage: function (pageNo) { pageNo = this._normalizeNumber(pageNo, 1, this._calculatePageCount(), 1); @@ -3940,8 +4055,7 @@ THE SOFTWARE. options: { tableId: undefined, columnResizable: true, - columnSelectable: true, - saveUserPreferences: true + columnSelectable: true }, /************************************************************************ @@ -3966,7 +4080,6 @@ THE SOFTWARE. this._createColumnResizeBar(); this._createColumnSelection(); - this._cookieKeyPrefix = this._generateCookieKeyPrefix(); if (this.options.saveUserPreferences) { this._loadColumnSettings(); } @@ -4358,7 +4471,7 @@ THE SOFTWARE. columnSettings[fieldName] = { columnVisibility: settings[0], columnWidth: settings[1] - }; ; + }; }); var headerCells = this._$table.find('>thead >tr >th.jtable-column-header'); @@ -4374,72 +4487,6 @@ THE SOFTWARE. $cell.data('width-in-percent', columnSettings[fieldName].columnWidth).css('width', columnSettings[fieldName].columnWidth + '%'); } }); - }, - - /************************************************************************ - * COOKIE * - *************************************************************************/ - - /* Sets a cookie with given key. - *************************************************************************/ - _setCookie: function (key, value) { - key = this._cookieKeyPrefix + key; - - var expireDate = new Date(); - expireDate.setDate(expireDate.getDate() + 30); - document.cookie = encodeURIComponent(key) + '=' + encodeURIComponent(value) + "; expires=" + expireDate.toUTCString(); - }, - - /* Gets a cookie with given key. - *************************************************************************/ - _getCookie: function (key) { - key = this._cookieKeyPrefix + key; - - var equalities = document.cookie.split('; '); - for (var i = 0; i < equalities.length; i++) { - if (!equalities[i]) { - continue; - } - - var splitted = equalities[i].split('='); - if (splitted.length != 2) { - continue; - } - - if (decodeURIComponent(splitted[0]) === key) { - return decodeURIComponent(splitted[1] || ''); - } - } - - return null; - }, - - /* Generates a hash key to be prefix for all cookies for this jtable instance. - *************************************************************************/ - _generateCookieKeyPrefix: function () { - - var simpleHash = function (value) { - var hash = 0; - if (value.length == 0) { - return hash; - } - - for (var i = 0; i < value.length; i++) { - var ch = value.charCodeAt(i); - hash = ((hash << 5) - hash) + ch; - hash = hash & hash; - } - - return hash; - }; - - var strToHash = ''; - if (this.options.tableId) { - strToHash = strToHash + this.options.tableId + '#'; - } - - strToHash = strToHash + this._columnList.join('$') + '#c' + this._$table.find('thead th').length; - return 'jtable#' + simpleHash(strToHash); } }); |