|
43 | 43 |
|
44 | 44 | function getParams(context) |
45 | 45 | { |
46 | | - return (context) ? $.extend({}, this.cachedParams, { ctx: context }) : |
| 46 | + return (context) ? $.extend({}, this.cachedParams, { ctx: context }) : |
47 | 47 | this.cachedParams; |
48 | 48 | } |
49 | 49 |
|
|
52 | 52 | var request = { |
53 | 53 | current: this.current, |
54 | 54 | rowCount: this.rowCount, |
55 | | - sort: this.sort, |
| 55 | + sort: this.sortDictionary, |
56 | 56 | searchPhrase: this.searchPhrase |
57 | 57 | }, |
58 | 58 | post = this.options.post; |
|
132 | 132 | that.columns.push(column); |
133 | 133 | if (column.order != null) |
134 | 134 | { |
135 | | - that.sort[column.id] = column.order; |
| 135 | + that.sortDictionary[column.id] = column.order; |
136 | 136 | } |
137 | 137 |
|
138 | 138 | // Prevents multiple identifiers |
|
176 | 176 | for (var i = 0; i < that.columns.length; i++) |
177 | 177 | { |
178 | 178 | column = that.columns[i]; |
179 | | - if (column.searchable && column.visible && |
| 179 | + if (column.searchable && column.visible && |
180 | 180 | column.converter.to(row[column.id]).search(searchPattern) > -1) |
181 | 181 | { |
182 | 182 | return true; |
|
299 | 299 | function prepareTable() |
300 | 300 | { |
301 | 301 | var tpl = this.options.templates, |
302 | | - wrapper = (this.element.parent().hasClass(this.options.css.responsiveTable)) ? |
| 302 | + wrapper = (this.element.parent().hasClass(this.options.css.responsiveTable)) ? |
303 | 303 | this.element.parent() : this.element; |
304 | 304 |
|
305 | 305 | this.element.addClass(this.options.css.table); |
|
592 | 592 | if (that.selection) |
593 | 593 | { |
594 | 594 | var selected = ($.inArray(row[that.identifier], that.selectedRows) !== -1), |
595 | | - selectBox = tpl.select.resolve(getParams.call(that, |
| 595 | + selectBox = tpl.select.resolve(getParams.call(that, |
596 | 596 | { type: "checkbox", value: row[that.identifier], checked: selected })); |
597 | 597 | cells += tpl.cell.resolve(getParams.call(that, { content: selectBox, css: css.selectCell })); |
598 | 598 | allRowsSelected = (allRowsSelected && selected); |
|
613 | 613 | { |
614 | 614 | if (column.visible) |
615 | 615 | { |
616 | | - var value = ($.isFunction(column.formatter)) ? |
617 | | - column.formatter.call(that, column, row) : |
| 616 | + var value = ($.isFunction(column.formatter)) ? |
| 617 | + column.formatter.call(that, column, row) : |
618 | 618 | column.converter.to(row[column.id]), |
619 | 619 | cssClass = (column.cssClass.length > 0) ? " " + column.cssClass : ""; |
620 | 620 | cells += tpl.cell.resolve(getParams.call(that, { |
621 | 621 | content: (value == null || value === "") ? " " : value, |
622 | | - css: ((column.align === "right") ? css.right : (column.align === "center") ? |
| 622 | + css: ((column.align === "right") ? css.right : (column.align === "center") ? |
623 | 623 | css.center : css.left) + cssClass, |
624 | 624 | style: (column.width == null) ? "" : "width:" + column.width + ";" })); |
625 | 625 | } |
|
678 | 678 | e.stopPropagation(); |
679 | 679 |
|
680 | 680 | var $this = $(this), |
681 | | - id = (that.identifier == null) ? $this.data("row-id") : |
| 681 | + id = (that.identifier == null) ? $this.data("row-id") : |
682 | 682 | that.converter.from($this.data("row-id") + ""), |
683 | | - row = (that.identifier == null) ? that.currentRows[id] : |
| 683 | + row = (that.identifier == null) ? that.currentRows[id] : |
684 | 684 | that.currentRows.first(function (item) { return item[that.identifier] === id; }); |
685 | 685 |
|
686 | 686 | if (that.selection && that.options.rowSelect) |
|
759 | 759 |
|
760 | 760 | if (this.selection) |
761 | 761 | { |
762 | | - var selectBox = (this.options.multiSelect) ? |
| 762 | + var selectBox = (this.options.multiSelect) ? |
763 | 763 | tpl.select.resolve(getParams.call(that, { type: "checkbox", value: "all" })) : ""; |
764 | | - html += tpl.rawHeaderCell.resolve(getParams.call(that, { content: selectBox, |
| 764 | + html += tpl.rawHeaderCell.resolve(getParams.call(that, { content: selectBox, |
765 | 765 | css: css.selectCell })); |
766 | 766 | } |
767 | 767 |
|
768 | 768 | $.each(this.columns, function (index, column) |
769 | 769 | { |
770 | 770 | if (column.visible) |
771 | 771 | { |
772 | | - var sortOrder = that.sort[column.id], |
| 772 | + var sortOrder = that.sortDictionary[column.id], |
773 | 773 | iconCss = ((sorting && sortOrder && sortOrder === "asc") ? css.iconUp : |
774 | 774 | (sorting && sortOrder && sortOrder === "desc") ? css.iconDown : ""), |
775 | 775 | icon = tpl.icon.resolve(getParams.call(that, { iconCss: iconCss })), |
776 | 776 | align = column.headerAlign, |
777 | 777 | cssClass = (column.headerCssClass.length > 0) ? " " + column.headerCssClass : ""; |
778 | 778 | html += tpl.headerCell.resolve(getParams.call(that, { |
779 | 779 | column: column, icon: icon, sortable: sorting && column.sortable && css.sortable || "", |
780 | | - css: ((align === "right") ? css.right : (align === "center") ? |
| 780 | + css: ((align === "right") ? css.right : (align === "center") ? |
781 | 781 | css.center : css.left) + cssClass, |
782 | 782 | style: (column.width == null) ? "" : "width:" + column.width + ";" })); |
783 | 783 | } |
784 | 784 | }); |
785 | 785 |
|
786 | 786 | headerRow.html(html); |
787 | 787 |
|
788 | | - // todo: create a own function for that piece of code |
789 | 788 | if (sorting) |
790 | 789 | { |
791 | | - var sortingSelector = getCssSelector(css.sortable), |
792 | | - iconSelector = getCssSelector(css.icon); |
| 790 | + var sortingSelector = getCssSelector(css.sortable); |
793 | 791 | headerRow.off("click" + namespace, sortingSelector) |
794 | 792 | .on("click" + namespace, sortingSelector, function (e) |
795 | 793 | { |
796 | 794 | e.preventDefault(); |
797 | | - var $this = $(this), |
798 | | - columnId = $this.data("column-id") || $this.parents("th").first().data("column-id"), |
799 | | - sortOrder = that.sort[columnId], |
800 | | - icon = $this.find(iconSelector); |
801 | | - |
802 | | - if (!that.options.multiSort) |
803 | | - { |
804 | | - $this.parents("tr").first().find(iconSelector).removeClass(css.iconDown + " " + css.iconUp); |
805 | | - that.sort = {}; |
806 | | - } |
807 | | - |
808 | | - if (sortOrder && sortOrder === "asc") |
809 | | - { |
810 | | - that.sort[columnId] = "desc"; |
811 | | - icon.removeClass(css.iconUp).addClass(css.iconDown); |
812 | | - } |
813 | | - else if (sortOrder && sortOrder === "desc") |
814 | | - { |
815 | | - if (that.options.multiSort) |
816 | | - { |
817 | | - var newSort = {}; |
818 | | - for (var key in that.sort) |
819 | | - { |
820 | | - if (key !== columnId) |
821 | | - { |
822 | | - newSort[key] = that.sort[key]; |
823 | | - } |
824 | | - } |
825 | | - that.sort = newSort; |
826 | | - icon.removeClass(css.iconDown); |
827 | | - } |
828 | | - else |
829 | | - { |
830 | | - that.sort[columnId] = "asc"; |
831 | | - icon.removeClass(css.iconDown).addClass(css.iconUp); |
832 | | - } |
833 | | - } |
834 | | - else |
835 | | - { |
836 | | - that.sort[columnId] = "asc"; |
837 | | - icon.addClass(css.iconUp); |
838 | | - } |
839 | 795 |
|
| 796 | + setTableHeaderSortDirection.call(that, $(this)); |
840 | 797 | sortRows.call(that); |
841 | 798 | loadData.call(that); |
842 | 799 | }); |
|
863 | 820 | } |
864 | 821 | } |
865 | 822 |
|
| 823 | + function setTableHeaderSortDirection(element) |
| 824 | + { |
| 825 | + var css = this.options.css, |
| 826 | + iconSelector = getCssSelector(css.icon), |
| 827 | + columnId = element.data("column-id") || element.parents("th").first().data("column-id"), |
| 828 | + sortOrder = this.sortDictionary[columnId], |
| 829 | + icon = element.find(iconSelector); |
| 830 | + |
| 831 | + if (!this.options.multiSort) |
| 832 | + { |
| 833 | + element.parents("tr").first().find(iconSelector).removeClass(css.iconDown + " " + css.iconUp); |
| 834 | + this.sortDictionary = {}; |
| 835 | + } |
| 836 | + |
| 837 | + if (sortOrder && sortOrder === "asc") |
| 838 | + { |
| 839 | + this.sortDictionary[columnId] = "desc"; |
| 840 | + icon.removeClass(css.iconUp).addClass(css.iconDown); |
| 841 | + } |
| 842 | + else if (sortOrder && sortOrder === "desc") |
| 843 | + { |
| 844 | + if (this.options.multiSort) |
| 845 | + { |
| 846 | + var newSort = {}; |
| 847 | + for (var key in this.sortDictionary) |
| 848 | + { |
| 849 | + if (key !== columnId) |
| 850 | + { |
| 851 | + newSort[key] = this.sortDictionary[key]; |
| 852 | + } |
| 853 | + } |
| 854 | + this.sortDictionary = newSort; |
| 855 | + icon.removeClass(css.iconDown); |
| 856 | + } |
| 857 | + else |
| 858 | + { |
| 859 | + this.sortDictionary[columnId] = "asc"; |
| 860 | + icon.removeClass(css.iconDown).addClass(css.iconUp); |
| 861 | + } |
| 862 | + } |
| 863 | + else |
| 864 | + { |
| 865 | + this.sortDictionary[columnId] = "asc"; |
| 866 | + icon.addClass(css.iconUp); |
| 867 | + } |
| 868 | + } |
| 869 | + |
866 | 870 | function replacePlaceHolder(placeholder, element) |
867 | 871 | { |
868 | 872 | placeholder.each(function (index, item) |
|
916 | 920 | { |
917 | 921 | var that = this; |
918 | 922 |
|
919 | | - for (var key in this.sort) |
| 923 | + for (var key in this.sortDictionary) |
920 | 924 | { |
921 | 925 | if (this.options.multiSort || sortArray.length === 0) |
922 | 926 | { |
923 | 927 | sortArray.push({ |
924 | 928 | id: key, |
925 | | - order: this.sort[key] |
| 929 | + order: this.sortDictionary[key] |
926 | 930 | }); |
927 | 931 | } |
928 | 932 | } |
|
963 | 967 | this.rows = []; |
964 | 968 | this.searchPhrase = ""; |
965 | 969 | this.selectedRows = []; |
966 | | - this.sort = {}; |
| 970 | + this.sortDictionary = {}; |
967 | 971 | this.total = 0; |
968 | 972 | this.totalPages = 0; |
969 | 973 | this.cachedParams = { |
|
1031 | 1035 | rowSelect: false, |
1032 | 1036 |
|
1033 | 1037 | /** |
1034 | | - * Defines whether the row selection is saved internally on filtering, paging and sorting |
| 1038 | + * Defines whether the row selection is saved internally on filtering, paging and sorting |
1035 | 1039 | * (even if the selected rows are not visible). |
1036 | 1040 | * |
1037 | 1041 | * @property keepSelection |
|
1081 | 1085 | }, |
1082 | 1086 |
|
1083 | 1087 | /** |
1084 | | - * Enriches the request object with additional properties. Either a `PlainObject` or a `Function` |
| 1088 | + * Enriches the request object with additional properties. Either a `PlainObject` or a `Function` |
1085 | 1089 | * that returns a `PlainObject` can be passed. Default value is `{}`. |
1086 | 1090 | * |
1087 | 1091 | * @property post |
|
1093 | 1097 | post: {}, // or use function () { return {}; } (reserved properties are "current", "rowCount", "sort" and "searchPhrase") |
1094 | 1098 |
|
1095 | 1099 | /** |
1096 | | - * Sets the data URL to a data service (e.g. a REST service). Either a `String` or a `Function` |
| 1100 | + * Sets the data URL to a data service (e.g. a REST service). Either a `String` or a `Function` |
1097 | 1101 | * that returns a `String` can be passed. Default value is `""`. |
1098 | 1102 | * |
1099 | 1103 | * @property url |
|
1504 | 1508 | { |
1505 | 1509 | rowIds = rowIds || this.currentRows.propValues(this.identifier); |
1506 | 1510 |
|
1507 | | - var id, i, |
| 1511 | + var id, i, |
1508 | 1512 | selectedRows = []; |
1509 | 1513 |
|
1510 | 1514 | while (rowIds.length > 0 && !(!this.options.multiSelect && selectedRows.length === 1)) |
|
1602 | 1606 | .removeClass(this.options.css.selected)._bgAria("selected", "false") |
1603 | 1607 | .find(selectBoxSelector).prop("checked", false); |
1604 | 1608 | } |
1605 | | - |
| 1609 | + |
1606 | 1610 | this.element.trigger("deselected" + namespace, [deselectedRows]); |
1607 | 1611 | } |
1608 | 1612 | } |
|
1622 | 1626 | Grid.prototype.sort = function(dictionary) |
1623 | 1627 | { |
1624 | 1628 | var values = (dictionary) ? $.extend({}, dictionary) : {}; |
1625 | | - if (values === this.sort) |
| 1629 | + |
| 1630 | + if (values === this.sortDictionary) |
1626 | 1631 | { |
1627 | 1632 | return this; |
1628 | 1633 | } |
1629 | 1634 |
|
1630 | | - this.sort = values; |
1631 | | - |
| 1635 | + this.sortDictionary = values; |
1632 | 1636 | renderTableHeader.call(this); |
1633 | 1637 | sortRows.call(this); |
1634 | 1638 | loadData.call(this); |
|
0 commit comments