Skip to content

Commit 0012ea6

Browse files
committed
fix(pfTableView): Fixed sort col when show/hiding checkbox col
1 parent d5293d0 commit 0012ea6

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/table/tableview/examples/table-view-basic.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@
103103
angular.module('patternfly.tableview.demo').controller('TableCtrl', ['$scope', '$timeout', 'itemsService',
104104
function ($scope, $timeout, itemsService) {
105105
$scope.dtOptions = {
106-
order: [[2, "asc"]],
106+
// order column(s) should NOT account for 1st checkbox column, table component will adjust col. numbers accordingly
107+
// Sort by City, then Name
108+
order: [[3, "asc"], [1, "desc"]],
107109
};
108110
109111
$scope.columns = [

src/table/tableview/table-view.component.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ angular.module('patternfly.table').component('pfTableView', {
1414
templateUrl: 'table/tableview/table-view.html',
1515
controller: function (DTOptionsBuilder, DTColumnDefBuilder, $element, pfUtils, $log, $filter, $timeout, $sce) {
1616
'use strict';
17-
var ctrl = this, prevDtOptions, prevItems, prevPageConfig;
17+
var ctrl = this, prevDtOptions, prevItems, prevPageConfig, prevShowCheckboxes;
1818

1919
// Once datatables is out of active development I'll remove log statements
2020
ctrl.debug = false;
@@ -25,7 +25,7 @@ angular.module('patternfly.table').component('pfTableView', {
2525
ctrl.defaultDtOptions = {
2626
autoWidth: false,
2727
destroy: true,
28-
order: [[1, "asc"]],
28+
order: [[0, "asc"]], //default to 1st (col 0) for sorting, updateConfigOptions() will adjust based on showCheckboxes
2929
dom: "t",
3030
paging: false,
3131
select: {
@@ -117,20 +117,23 @@ angular.module('patternfly.table').component('pfTableView', {
117117
ctrl.dtOptions.displayLength = Number(ctrl.dtOptions.displayLength);
118118
}
119119

120+
_.defaults(ctrl.dtOptions, ctrl.defaultDtOptions);
121+
_.defaults(ctrl.config, ctrl.defaultConfig);
122+
123+
if (ctrl.config.showCheckboxes !== prevShowCheckboxes) {
124+
// adjust column numbers based on whether or not there is a checkbox column
125+
// multi-col order may be used. Ex: [[ 0, 'asc' ], [ 1, 'desc' ]]
126+
_.each(ctrl.dtOptions.order, function (col) {
127+
col[0] = ctrl.config.showCheckboxes ? col[0] + 1 : col[0] - 1;
128+
col[0] = col[0] < 0 ? 0 : col[0]; //no negative col numbers
129+
});
130+
}
131+
120132
// Need to deep watch changes in dtOptions and items
121133
prevDtOptions = angular.copy(ctrl.dtOptions);
122134
prevItems = angular.copy(ctrl.items);
123135
prevPageConfig = angular.copy(ctrl.pageConfig);
124-
125-
// Setting bound variables to new variables loses it's one way binding
126-
// ctrl.dtOptions = pfUtils.merge(ctrl.defaultDtOptions, ctrl.dtOptions);
127-
// ctrl.config = pfUtils.merge(ctrl.defaultConfig, ctrl.config);
128-
129-
// Instead, use _.defaults to update the existing variable
130-
_.defaults(ctrl.dtOptions, ctrl.defaultDtOptions);
131-
_.defaults(ctrl.config, ctrl.defaultConfig);
132-
// may need to use _.defaultsDeep, but not currently available in
133-
// lodash-amd a-pf is using
136+
prevShowCheckboxes = angular.copy(ctrl.config.showCheckboxes);
134137

135138
if (!validSelectionMatchProp()) {
136139
angular.forEach(ctrl.columns, function (col) {

0 commit comments

Comments
 (0)