Skip to content

Commit c39b926

Browse files
committed
Merged pull request "Fixing sort method for jQuery plugin rstaib#84" and fixed one issue there
2 parents 90e5e47 + 67e4cbb commit c39b926

File tree

7 files changed

+154
-146
lines changed

7 files changed

+154
-146
lines changed

dist/jQuery.Bootgrid.1.2.0.nupkg

8 Bytes
Binary file not shown.

dist/jquery.bootgrid-1.2.0.zip

43 Bytes
Binary file not shown.

dist/jquery.bootgrid.js

Lines changed: 75 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
function getParams(context)
4545
{
46-
return (context) ? $.extend({}, this.cachedParams, { ctx: context }) :
46+
return (context) ? $.extend({}, this.cachedParams, { ctx: context }) :
4747
this.cachedParams;
4848
}
4949

@@ -52,7 +52,7 @@
5252
var request = {
5353
current: this.current,
5454
rowCount: this.rowCount,
55-
sort: this.sort,
55+
sort: this.sortDictionary,
5656
searchPhrase: this.searchPhrase
5757
},
5858
post = this.options.post;
@@ -132,7 +132,7 @@
132132
that.columns.push(column);
133133
if (column.order != null)
134134
{
135-
that.sort[column.id] = column.order;
135+
that.sortDictionary[column.id] = column.order;
136136
}
137137

138138
// Prevents multiple identifiers
@@ -176,7 +176,7 @@
176176
for (var i = 0; i < that.columns.length; i++)
177177
{
178178
column = that.columns[i];
179-
if (column.searchable && column.visible &&
179+
if (column.searchable && column.visible &&
180180
column.converter.to(row[column.id]).search(searchPattern) > -1)
181181
{
182182
return true;
@@ -299,7 +299,7 @@
299299
function prepareTable()
300300
{
301301
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)) ?
303303
this.element.parent() : this.element;
304304

305305
this.element.addClass(this.options.css.table);
@@ -592,7 +592,7 @@
592592
if (that.selection)
593593
{
594594
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,
596596
{ type: "checkbox", value: row[that.identifier], checked: selected }));
597597
cells += tpl.cell.resolve(getParams.call(that, { content: selectBox, css: css.selectCell }));
598598
allRowsSelected = (allRowsSelected && selected);
@@ -613,13 +613,13 @@
613613
{
614614
if (column.visible)
615615
{
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) :
618618
column.converter.to(row[column.id]),
619619
cssClass = (column.cssClass.length > 0) ? " " + column.cssClass : "";
620620
cells += tpl.cell.resolve(getParams.call(that, {
621621
content: (value == null || value === "") ? "&nbsp;" : value,
622-
css: ((column.align === "right") ? css.right : (column.align === "center") ?
622+
css: ((column.align === "right") ? css.right : (column.align === "center") ?
623623
css.center : css.left) + cssClass,
624624
style: (column.width == null) ? "" : "width:" + column.width + ";" }));
625625
}
@@ -678,9 +678,9 @@
678678
e.stopPropagation();
679679

680680
var $this = $(this),
681-
id = (that.identifier == null) ? $this.data("row-id") :
681+
id = (that.identifier == null) ? $this.data("row-id") :
682682
that.converter.from($this.data("row-id") + ""),
683-
row = (that.identifier == null) ? that.currentRows[id] :
683+
row = (that.identifier == null) ? that.currentRows[id] :
684684
that.currentRows.first(function (item) { return item[that.identifier] === id; });
685685

686686
if (that.selection && that.options.rowSelect)
@@ -759,84 +759,41 @@
759759

760760
if (this.selection)
761761
{
762-
var selectBox = (this.options.multiSelect) ?
762+
var selectBox = (this.options.multiSelect) ?
763763
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,
765765
css: css.selectCell }));
766766
}
767767

768768
$.each(this.columns, function (index, column)
769769
{
770770
if (column.visible)
771771
{
772-
var sortOrder = that.sort[column.id],
772+
var sortOrder = that.sortDictionary[column.id],
773773
iconCss = ((sorting && sortOrder && sortOrder === "asc") ? css.iconUp :
774774
(sorting && sortOrder && sortOrder === "desc") ? css.iconDown : ""),
775775
icon = tpl.icon.resolve(getParams.call(that, { iconCss: iconCss })),
776776
align = column.headerAlign,
777777
cssClass = (column.headerCssClass.length > 0) ? " " + column.headerCssClass : "";
778778
html += tpl.headerCell.resolve(getParams.call(that, {
779779
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") ?
781781
css.center : css.left) + cssClass,
782782
style: (column.width == null) ? "" : "width:" + column.width + ";" }));
783783
}
784784
});
785785

786786
headerRow.html(html);
787787

788-
// todo: create a own function for that piece of code
789788
if (sorting)
790789
{
791-
var sortingSelector = getCssSelector(css.sortable),
792-
iconSelector = getCssSelector(css.icon);
790+
var sortingSelector = getCssSelector(css.sortable);
793791
headerRow.off("click" + namespace, sortingSelector)
794792
.on("click" + namespace, sortingSelector, function (e)
795793
{
796794
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-
}
839795

796+
setTableHeaderSortDirection.call(that, $(this));
840797
sortRows.call(that);
841798
loadData.call(that);
842799
});
@@ -863,6 +820,53 @@
863820
}
864821
}
865822

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+
866870
function replacePlaceHolder(placeholder, element)
867871
{
868872
placeholder.each(function (index, item)
@@ -916,13 +920,13 @@
916920
{
917921
var that = this;
918922

919-
for (var key in this.sort)
923+
for (var key in this.sortDictionary)
920924
{
921925
if (this.options.multiSort || sortArray.length === 0)
922926
{
923927
sortArray.push({
924928
id: key,
925-
order: this.sort[key]
929+
order: this.sortDictionary[key]
926930
});
927931
}
928932
}
@@ -963,7 +967,7 @@
963967
this.rows = [];
964968
this.searchPhrase = "";
965969
this.selectedRows = [];
966-
this.sort = {};
970+
this.sortDictionary = {};
967971
this.total = 0;
968972
this.totalPages = 0;
969973
this.cachedParams = {
@@ -1031,7 +1035,7 @@
10311035
rowSelect: false,
10321036

10331037
/**
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
10351039
* (even if the selected rows are not visible).
10361040
*
10371041
* @property keepSelection
@@ -1081,7 +1085,7 @@
10811085
},
10821086

10831087
/**
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`
10851089
* that returns a `PlainObject` can be passed. Default value is `{}`.
10861090
*
10871091
* @property post
@@ -1093,7 +1097,7 @@
10931097
post: {}, // or use function () { return {}; } (reserved properties are "current", "rowCount", "sort" and "searchPhrase")
10941098

10951099
/**
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`
10971101
* that returns a `String` can be passed. Default value is `""`.
10981102
*
10991103
* @property url
@@ -1504,7 +1508,7 @@
15041508
{
15051509
rowIds = rowIds || this.currentRows.propValues(this.identifier);
15061510

1507-
var id, i,
1511+
var id, i,
15081512
selectedRows = [];
15091513

15101514
while (rowIds.length > 0 && !(!this.options.multiSelect && selectedRows.length === 1))
@@ -1602,7 +1606,7 @@
16021606
.removeClass(this.options.css.selected)._bgAria("selected", "false")
16031607
.find(selectBoxSelector).prop("checked", false);
16041608
}
1605-
1609+
16061610
this.element.trigger("deselected" + namespace, [deselectedRows]);
16071611
}
16081612
}
@@ -1622,13 +1626,13 @@
16221626
Grid.prototype.sort = function(dictionary)
16231627
{
16241628
var values = (dictionary) ? $.extend({}, dictionary) : {};
1625-
if (values === this.sort)
1629+
1630+
if (values === this.sortDictionary)
16261631
{
16271632
return this;
16281633
}
16291634

1630-
this.sort = values;
1631-
1635+
this.sortDictionary = values;
16321636
renderTableHeader.call(this);
16331637
sortRows.call(this);
16341638
loadData.call(this);

dist/jquery.bootgrid.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)