From 1a8975fa65c177458c8b07661701c97d8817db73 Mon Sep 17 00:00:00 2001 From: Egor Trutnev Date: Mon, 7 Apr 2025 15:53:40 +0300 Subject: [PATCH 1/6] fix issues/7796 --- src/bootstrap-table.js | 36 ++++++++-------- src/utils/index.js | 94 ++++++++++++++++++++++++------------------ 2 files changed, 74 insertions(+), 56 deletions(-) diff --git a/src/bootstrap-table.js b/src/bootstrap-table.js index 0b95a258a9..47d3a0dd62 100644 --- a/src/bootstrap-table.js +++ b/src/bootstrap-table.js @@ -503,24 +503,26 @@ class BootstrapTable { this.options.sortOrder, this.data ]) + } else if (this.options.groupBy === true && this.options.groupByField !== '') { + const groupedData = {} + + 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).map(groupKey => { + const group = groupedData[groupKey] + Utils.sort(name, order, index, group, this.header, this.options) + return group + }); + + this.data = [].concat(...sortedGroups) } 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]) - - if (value !== undefined) { - if (this.options.sortStable && value === 0) { - return order * (a._position - b._position) - } - return order * value - } - - return Utils.sort(aa, bb, order, this.options, a._position, b._position) - }) + 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..8db2cfa056 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -514,57 +514,73 @@ 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) { + data.sort((a, b) => { + if (header.sortNames[index]) { + name = header.sortNames[index] + } - if (options.sortStable && a === b) { - a = aPosition - b = bPosition - } + const aa = Utils.getItemField(a, name, options.escape) + const bb = Utils.getItemField(b, name, options.escape) + const value = Utils.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 = aPosition + bb = bPosition } - 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 = '') { From 35b42a05f1ce2cc5df86273df09d88aee09fba5a Mon Sep 17 00:00:00 2001 From: Egor Trutnev Date: Mon, 7 Apr 2025 16:39:31 +0300 Subject: [PATCH 2/6] fix --- src/bootstrap-table.js | 31 +++++++++++++------------------ src/utils/index.js | 14 +++++++------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/bootstrap-table.js b/src/bootstrap-table.js index 47d3a0dd62..696dd63e69 100644 --- a/src/bootstrap-table.js +++ b/src/bootstrap-table.js @@ -487,7 +487,6 @@ class BootstrapTable { const order = this.options.sortOrder === 'desc' ? -1 : 1 const index = this.header.fields.indexOf(this.options.sortName) let timeoutId = 0 - if (index !== -1) { if (this.options.sortStable) { this.data.forEach((row, i) => { @@ -496,7 +495,6 @@ class BootstrapTable { } }) } - if (this.options.customSort) { Utils.calculateObjectValue(this.options, this.options.customSort, [ this.options.sortName, @@ -505,22 +503,19 @@ class BootstrapTable { ]) } else if (this.options.groupBy === true && this.options.groupByField !== '') { const groupedData = {} - - 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).map(groupKey => { - const group = groupedData[groupKey] - Utils.sort(name, order, index, group, this.header, this.options) - return group - }); - - this.data = [].concat(...sortedGroups) + 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).map(groupKey => { + const group = groupedData[groupKey] + 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) } diff --git a/src/utils/index.js b/src/utils/index.js index 8db2cfa056..6c21975e45 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -514,15 +514,15 @@ export default { return data }, - sort(name, order, index, data, header, options) { + sort (name, order, index, data, header, options) { data.sort((a, b) => { if (header.sortNames[index]) { name = header.sortNames[index] } - const aa = Utils.getItemField(a, name, options.escape) - const bb = Utils.getItemField(b, name, options.escape) - const value = Utils.calculateObjectValue(header, header.sorters[index], [aa, bb, a, b]) + let aa = getItemField(a, name, options.escape) + let bb = getItemField(b, name, options.escape) + const value = calculateObjectValue(header, header.sorters[index], [aa, bb, a, b]) if (value !== undefined) { if (options.sortStable && value === 0) { @@ -539,8 +539,8 @@ export default { } if (options.sortStable && aa === bb) { - aa = aPosition - bb = bPosition + aa = a._position + bb = b._position } // If both values are numeric, do a numeric comparison @@ -580,7 +580,7 @@ export default { } return order - }); + }) }, getEventName (eventPrefix, id = '') { From 8edc824acfd735cb9b1da73f411ebf7d00850705 Mon Sep 17 00:00:00 2001 From: Egor Trutnev Date: Mon, 7 Apr 2025 16:44:41 +0300 Subject: [PATCH 3/6] fix --- src/bootstrap-table.js | 13 +++++++++---- src/utils/index.js | 6 +++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/bootstrap-table.js b/src/bootstrap-table.js index 696dd63e69..96d5050f45 100644 --- a/src/bootstrap-table.js +++ b/src/bootstrap-table.js @@ -483,10 +483,11 @@ 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 + if (index !== -1) { if (this.options.sortStable) { this.data.forEach((row, i) => { @@ -503,18 +504,22 @@ class BootstrapTable { ]) } else if (this.options.groupBy === true && this.options.groupByField !== '') { const groupedData = {} + this.data.forEach(item => { - const groupKey = Utils.getItemField(item, this.options.groupByField, this.options.escape); + 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).map(groupKey => { const group = groupedData[groupKey] + 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) diff --git a/src/utils/index.js b/src/utils/index.js index 6c21975e45..df8e84cfc5 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -520,9 +520,9 @@ export default { name = header.sortNames[index] } - let aa = getItemField(a, name, options.escape) - let bb = getItemField(b, name, options.escape) - const value = calculateObjectValue(header, header.sorters[index], [aa, bb, a, b]) + 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 (value !== undefined) { if (options.sortStable && value === 0) { From 44f6a7780c141864935ccfc4cd3894926547c05f Mon Sep 17 00:00:00 2001 From: Egor Trutnev <48055220+EgorTrutnev@users.noreply.github.com> Date: Thu, 15 May 2025 10:30:01 +0300 Subject: [PATCH 4/6] Update bootstrap-table.js --- src/bootstrap-table.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap-table.js b/src/bootstrap-table.js index 96d5050f45..cc32a54b84 100644 --- a/src/bootstrap-table.js +++ b/src/bootstrap-table.js @@ -502,7 +502,7 @@ class BootstrapTable { this.options.sortOrder, this.data ]) - } else if (this.options.groupBy === true && this.options.groupByField !== '') { + } else if (this.options.groupBy && this.options.groupByField !== '') { const groupedData = {} this.data.forEach(item => { From 3e5966917e2c0c56a6eb084c779ece23ca58aa2a Mon Sep 17 00:00:00 2001 From: Egor Trutnev <48055220+EgorTrutnev@users.noreply.github.com> Date: Thu, 15 May 2025 10:30:57 +0300 Subject: [PATCH 5/6] Update src/bootstrap-table.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/bootstrap-table.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap-table.js b/src/bootstrap-table.js index cc32a54b84..bbd9209cdc 100644 --- a/src/bootstrap-table.js +++ b/src/bootstrap-table.js @@ -513,7 +513,7 @@ class BootstrapTable { } groupedData[groupKey].push(item) }) - const sortedGroups = Object.keys(groupedData).map(groupKey => { + const sortedGroups = Object.keys(groupedData).sort().map(groupKey => { const group = groupedData[groupKey] Utils.sort(name, order, index, group, this.header, this.options) From 3b57fe11a0d51b4776523bf3a4f4b712653b8952 Mon Sep 17 00:00:00 2001 From: Egor Trutnev <48055220+EgorTrutnev@users.noreply.github.com> Date: Fri, 30 May 2025 14:00:17 +0300 Subject: [PATCH 6/6] Update src/utils/index.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/utils/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index df8e84cfc5..11e9beeb3f 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -515,9 +515,10 @@ export default { }, sort (name, order, index, data, header, options) { + const sortName = header.sortNames[index]; data.sort((a, b) => { - if (header.sortNames[index]) { - name = header.sortNames[index] + if (sortName) { + name = sortName; } let aa = this.getItemField(a, name, options.escape)