Skip to content

Commit

Permalink
fix #123; fix #256
Browse files Browse the repository at this point in the history
  • Loading branch information
samussiah committed Dec 12, 2019
1 parent 351f306 commit 20f71d4
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 63 deletions.
95 changes: 64 additions & 31 deletions build/webcharts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3402,7 +3402,7 @@
);
})
) {
this.data.filtered = this.data.raw;
this.data.filtered = this.data.raw.slice();
this.filters
.filter(function(filter) {
return (
Expand All @@ -3418,7 +3418,7 @@
: filter.val === d[filter.col];
});
});
} else this.data.filtered = this.data.raw;
} else this.data.filtered = this.data.raw.slice();
}

function updateDataObject() {
Expand Down Expand Up @@ -3922,7 +3922,8 @@
key: col
})
.classed('wc-button sort-box', true)
.text(header)
.text(header),
type: this.config.types[col]
};
sortItem.wrap
.append('span')
Expand Down Expand Up @@ -3968,27 +3969,50 @@
this.draw();
}

function _typeof(obj) {
if (typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol') {
_typeof = function(obj) {
return typeof obj;
};
} else {
_typeof = function(obj) {
return obj &&
typeof Symbol === 'function' &&
obj.constructor === Symbol &&
obj !== Symbol.prototype
? 'symbol'
: typeof obj;
};
}

return _typeof(obj);
}

function sortData(data) {
var _this = this;

data = data.sort(function(a, b) {
var order = 0;

_this.sortable.order.forEach(function(item) {
var aCell = a[item.col],
bCell = b[item.col];
var aCell = a[item.col];
var bCell = b[item.col];

if (order === 0) {
if (
(item.direction === 'ascending' && aCell < bCell) ||
(item.direction === 'descending' && aCell > bCell)
)
order = -1;
else if (
(item.direction === 'ascending' && aCell > bCell) ||
(item.direction === 'descending' && aCell < bCell)
)
order = 1;
if (item.type === 'number') {
order = item.direction === 'ascending' ? +aCell - +bCell : +bCell - +aCell;
} else {
if (order === 0) {
if (
(item.direction === 'ascending' && aCell < bCell) ||
(item.direction === 'descending' && aCell > bCell)
)
order = -1;
else if (
(item.direction === 'ascending' && aCell > bCell) ||
(item.direction === 'descending' && aCell < bCell)
)
order = 1;
}
}
});

Expand Down Expand Up @@ -4347,30 +4371,39 @@
}

function setDefaults$1(firstItem) {
//Set data-driven defaults.
if (this.config.cols instanceof Array && this.config.headers instanceof Array) {
if (this.config.cols.length === 0) delete this.config.cols;
if (
this.config.headers.length === 0 ||
this.config.headers.length !== this.config.cols.length
)
delete this.config.headers;
}
var _this = this;

this.config.cols = this.config.cols || d3.keys(firstItem);
this.config.headers = this.config.headers || this.config.cols;
this.config.layout = 'horizontal'; // placeholder setting to align table components vertically or horizontally
//Set all other defaults.
// cols
if (
!Array.isArray(this.config.cols) ||
(Array.isArray(this.config.cols) && this.config.cols.length === 0)
)
this.config.cols = d3.keys(firstItem); // headers

if (
!Array.isArray(this.config.headers) ||
(Array.isArray(this.config.headers) && this.config.headers.length === 0) ||
(Array.isArray(this.config.headers) &&
this.config.headers.length !== this.config.cols.length)
)
this.config.headers = this.config.cols.slice(); // types

if (_typeof(this.config.types) !== 'object') this.config.types = {};
this.config.cols.forEach(function(col) {
if (!['string', 'number'].includes(_this.config.types[col]))
_this.config.types[col] = 'string';
}); // Set all other defaults.

setDefault.call(this, 'searchable');
setDefault.call(this, 'exportable');
setDefault.call(this, 'exports', ['csv']);
setDefault.call(this, 'sortable');
setDefault.call(this, 'pagination');
setDefault.call(this, 'exportable');
setDefault.call(this, 'exports', ['csv']);
setDefault.call(this, 'nRowsPerPage', 10);
setDefault.call(this, 'nPageLinksDisplayed', 5);
setDefault.call(this, 'applyCSS');
setDefault.call(this, 'dynamicPositioning');
setDefault.call(this, 'layout', 'horizontal');
}

function transformData$1(processed_data) {
Expand Down
2 changes: 1 addition & 1 deletion build/webcharts.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/table/draw/applyFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function applyFilters() {
(Array.isArray(filter.val) && filter.val.length < filter.choices.length)
)
) {
this.data.filtered = this.data.raw;
this.data.filtered = this.data.raw.slice();
this.filters
.filter(
filter =>
Expand All @@ -24,5 +24,5 @@ export default function applyFilters() {
: filter.val === d[filter.col]
);
});
} else this.data.filtered = this.data.raw;
} else this.data.filtered = this.data.raw.slice();
}
41 changes: 26 additions & 15 deletions src/table/setDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,39 @@ import { keys } from 'd3';
import setDefault from '../util/setDefault';

export default function setDefaults(firstItem) {
//Set data-driven defaults.
if (this.config.cols instanceof Array && this.config.headers instanceof Array) {
if (this.config.cols.length === 0) delete this.config.cols;
if (
this.config.headers.length === 0 ||
this.config.headers.length !== this.config.cols.length
)
delete this.config.headers;
}
// cols
if (
!Array.isArray(this.config.cols) ||
(Array.isArray(this.config.cols) && this.config.cols.length === 0)
)
this.config.cols = keys(firstItem);

this.config.cols = this.config.cols || keys(firstItem);
this.config.headers = this.config.headers || this.config.cols;
this.config.layout = 'horizontal'; // placeholder setting to align table components vertically or horizontally
// headers
if (
!Array.isArray(this.config.headers) ||
(Array.isArray(this.config.headers) && this.config.headers.length === 0) ||
(Array.isArray(this.config.headers) &&
this.config.headers.length !== this.config.cols.length)
)
this.config.headers = this.config.cols.slice();

//Set all other defaults.
// types
if (typeof this.config.types !== 'object') this.config.types = {};

this.config.cols.forEach(col => {
if (!['string', 'number'].includes(this.config.types[col]))
this.config.types[col] = 'string';
});

// Set all other defaults.
setDefault.call(this, 'searchable');
setDefault.call(this, 'exportable');
setDefault.call(this, 'exports', ['csv']);
setDefault.call(this, 'sortable');
setDefault.call(this, 'pagination');
setDefault.call(this, 'exportable');
setDefault.call(this, 'exports', ['csv']);
setDefault.call(this, 'nRowsPerPage', 10);
setDefault.call(this, 'nPageLinksDisplayed', 5);
setDefault.call(this, 'applyCSS');
setDefault.call(this, 'dynamicPositioning');
setDefault.call(this, 'layout', 'horizontal');
}
3 changes: 2 additions & 1 deletion src/table/sortable/onClick.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default function onClick(th, header) {
.append('div')
.datum({ key: col })
.classed('wc-button sort-box', true)
.text(header)
.text(header),
type: this.config.types[col]
};
sortItem.wrap
.append('span')
Expand Down
30 changes: 17 additions & 13 deletions src/table/sortable/sortData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ export default function sortData(data) {
let order = 0;

this.sortable.order.forEach(item => {
const aCell = a[item.col],
bCell = b[item.col];
const aCell = a[item.col];
const bCell = b[item.col];

if (order === 0) {
if (
(item.direction === 'ascending' && aCell < bCell) ||
(item.direction === 'descending' && aCell > bCell)
)
order = -1;
else if (
(item.direction === 'ascending' && aCell > bCell) ||
(item.direction === 'descending' && aCell < bCell)
)
order = 1;
if (item.type === 'number') {
order = item.direction === 'ascending' ? +aCell - +bCell : +bCell - +aCell;
} else {
if (order === 0) {
if (
(item.direction === 'ascending' && aCell < bCell) ||
(item.direction === 'descending' && aCell > bCell)
)
order = -1;
else if (
(item.direction === 'ascending' && aCell > bCell) ||
(item.direction === 'descending' && aCell < bCell)
)
order = 1;
}
}
});

Expand Down
19 changes: 19 additions & 0 deletions test-page/createTable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ d3.csv(
return d;
},
function(data) {
table.config.types = Object.keys(data[0])
.map(col => {
let type = 'string';

const vector = data
.map(d => d[col]).filter(d => d !== '');

if (vector.length > 0 && vector.every(d => !isNaN(parseFloat(d))))
type = 'number';

return [col, type];
})
.reduce(
(acc,cur) => {
acc[cur[0]] = cur[1];
return acc;
},
{}
);
table.init(data);

//Update settings.
Expand Down

0 comments on commit 20f71d4

Please sign in to comment.