diff --git a/src/bootstrap-table.js b/src/bootstrap-table.js index 0b95a258a9..bbd9209cdc 100644 --- a/src/bootstrap-table.js +++ b/src/bootstrap-table.js @@ -483,7 +483,7 @@ class BootstrapTable { } initSort () { - let name = this.options.sortName + const name = this.options.sortName const order = this.options.sortOrder === 'desc' ? -1 : 1 const index = this.header.fields.indexOf(this.options.sortName) let timeoutId = 0 @@ -496,31 +496,33 @@ class BootstrapTable { } }) } - if (this.options.customSort) { Utils.calculateObjectValue(this.options, this.options.customSort, [ this.options.sortName, this.options.sortOrder, this.data ]) - } else { - this.data.sort((a, b) => { - if (this.header.sortNames[index]) { - name = this.header.sortNames[index] - } - const aa = Utils.getItemField(a, name, this.options.escape) - const bb = Utils.getItemField(b, name, this.options.escape) - const value = Utils.calculateObjectValue(this.header, this.header.sorters[index], [aa, bb, a, b]) + } else if (this.options.groupBy && this.options.groupByField !== '') { + const groupedData = {} - if (value !== undefined) { - if (this.options.sortStable && value === 0) { - return order * (a._position - b._position) - } - return order * value + this.data.forEach(item => { + const groupKey = Utils.getItemField(item, this.options.groupByField, this.options.escape) + + if (!groupedData[groupKey]) { + groupedData[groupKey] = [] } + groupedData[groupKey].push(item) + }) + const sortedGroups = Object.keys(groupedData).sort().map(groupKey => { + const group = groupedData[groupKey] - return Utils.sort(aa, bb, order, this.options, a._position, b._position) + Utils.sort(name, order, index, group, this.header, this.options) + return group }) + + this.data = [].concat(...sortedGroups) + } else { + Utils.sort(name, order, index, this.data, this.header, this.options) } if (this.options.sortClass !== undefined) { diff --git a/src/utils/index.js b/src/utils/index.js index 71924fab3a..11e9beeb3f 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -514,57 +514,74 @@ export default { return data }, - sort (a, b, order, options, aPosition, bPosition) { - if (a === undefined || a === null) { - a = '' - } - if (b === undefined || b === null) { - b = '' - } + sort (name, order, index, data, header, options) { + const sortName = header.sortNames[index]; + data.sort((a, b) => { + if (sortName) { + name = sortName; + } - if (options.sortStable && a === b) { - a = aPosition - b = bPosition - } + let aa = this.getItemField(a, name, options.escape) + let bb = this.getItemField(b, name, options.escape) + const value = this.calculateObjectValue(header, header.sorters[index], [aa, bb, a, b]) - // If both values are numeric, do a numeric comparison - if (this.isNumeric(a) && this.isNumeric(b)) { - // Convert numerical values form string to float. - a = parseFloat(a) - b = parseFloat(b) - if (a < b) { - return order * -1 + if (value !== undefined) { + if (options.sortStable && value === 0) { + return order * (a._position - b._position) + } + return order * value } - if (a > b) { - return order + + if (aa === undefined || aa === null) { + aa = '' + } + if (bb === undefined || bb === null) { + bb = '' } - return 0 - } - if (options.sortEmptyLast) { - if (a === '') { - return 1 + if (options.sortStable && aa === bb) { + aa = a._position + bb = b._position } - if (b === '') { - return -1 + // If both values are numeric, do a numeric comparison + if (this.isNumeric(aa) && this.isNumeric(bb)) { + // Convert numerical values form string to float. + aa = parseFloat(aa) + bb = parseFloat(bb) + if (aa < bb) { + return order * -1 + } + if (aa > bb) { + return order + } + return 0 } - } - if (a === b) { - return 0 - } + if (options.sortEmptyLast) { + if (aa === '') { + return 1 + } + if (bb === '') { + return -1 + } + } - // If value is not a string, convert to string - if (typeof a !== 'string') { - a = a.toString() - } + if (aa === bb) { + return 0 + } - if (a.localeCompare(b) === -1) { - return order * -1 - } + // If value is not a string, convert to string + if (typeof aa !== 'string') { + aa = aa.toString() + } + + if (aa.localeCompare(bb) === -1) { + return order * -1 + } - return order + return order + }) }, getEventName (eventPrefix, id = '') {