Browse Source

Fix #6405 - Add `sortBy` method (#6719)

* Fixes #6405 - Add sortBy method
Marcel Overdijk 2 years ago
parent
commit
68f6698d00
4 changed files with 33 additions and 1 deletions
  1. 6 0
      CHANGELOG.md
  2. 14 0
      site/docs/api/methods.md
  3. 12 1
      src/bootstrap-table.js
  4. 1 0
      src/constants/index.js

+ 6 - 0
CHANGELOG.md

@@ -1,6 +1,12 @@
 ChangeLog
 ChangeLog
 ---------
 ---------
 
 
+### 1.22.0
+
+#### Core
+
+- **New:** Added `sortBy` method.
+
 ### 1.21.4
 ### 1.21.4
 
 
 #### Core
 #### Core

+ 14 - 0
site/docs/api/methods.md

@@ -509,6 +509,20 @@ The calling method syntax: `$('#table').bootstrapTable('method', parameter)`.
 
 
 - **Example:** [Show/Hide Row](https://examples.bootstrap-table.com/#methods/show-hide-row.html)
 - **Example:** [Show/Hide Row](https://examples.bootstrap-table.com/#methods/show-hide-row.html)
 
 
+## sortBy
+
+- **Parameter:** `params`
+
+- **Detail:**
+
+  Sorts the table by the specified field. The params must contain at least one of the following properties:
+
+  * `field`: the field name.
+  * `sortOrder`: the sort order, can only be 'asc' or 'desc'.
+
+- **Example:** [Sort By](https://examples.bootstrap-table.com/#methods/sort-by.html)
+
+
 ## toggleDetailView
 ## toggleDetailView
 
 
 - **Parameter:** `index`
 - **Parameter:** `index`

+ 12 - 1
src/bootstrap-table.js

@@ -530,6 +530,12 @@ class BootstrapTable {
     }
     }
   }
   }
 
 
+  sortBy (params) {
+    this.options.sortName = params.field
+    this.options.sortOrder = params.hasOwnProperty('sortOrder') ? params.sortOrder : 'asc'
+    this._sort()
+  }
+
   onSort ({ type, currentTarget }) {
   onSort ({ type, currentTarget }) {
     const $this = type === 'keypress' ? $(currentTarget) : $(currentTarget).parent()
     const $this = type === 'keypress' ? $(currentTarget) : $(currentTarget).parent()
     const $this_ = this.$header.find('th').eq($this.index())
     const $this_ = this.$header.find('th').eq($this.index())
@@ -561,13 +567,18 @@ class BootstrapTable {
           this.columns[this.fieldsColumnsIndex[$this.data('field')]].order
           this.columns[this.fieldsColumnsIndex[$this.data('field')]].order
       }
       }
     }
     }
-    this.trigger('sort', this.options.sortName, this.options.sortOrder)
 
 
     $this.add($this_).data('order', this.options.sortOrder)
     $this.add($this_).data('order', this.options.sortOrder)
 
 
     // Assign the correct sortable arrow
     // Assign the correct sortable arrow
     this.getCaret()
     this.getCaret()
 
 
+    this._sort()
+  }
+
+  _sort () {
+    this.trigger('sort', this.options.sortName, this.options.sortOrder)
+
     if (this.options.sidePagination === 'server' && this.options.serverSort) {
     if (this.options.sidePagination === 'server' && this.options.serverSort) {
       this.options.pageNumber = 1
       this.options.pageNumber = 1
       this.initServer(this.options.silentSort)
       this.initServer(this.options.silentSort)

+ 1 - 0
src/constants/index.js

@@ -478,6 +478,7 @@ const METHODS = [
   'togglePagination', 'toggleFullscreen', 'toggleView',
   'togglePagination', 'toggleFullscreen', 'toggleView',
   'resetSearch',
   'resetSearch',
   'filterBy',
   'filterBy',
+  'sortBy',
   'scrollTo', 'getScrollPosition',
   'scrollTo', 'getScrollPosition',
   'selectPage', 'prevPage', 'nextPage',
   'selectPage', 'prevPage', 'nextPage',
   'toggleDetailView',
   'toggleDetailView',