Browse Source

Merge pull request #1979 from djhvscf/develop

feat(js): add `showAllColumns` and `hideAllColumns` methods
fix(reorder extension): fix hide/show columns after reorder bug
feat(js): add `customSort` options
feat(js): add `customSearch` options
wenzhixin 9 years ago
parent
commit
10d46370c6

+ 35 - 0
docs/_i18n/en/documentation/table-options.md

@@ -551,6 +551,41 @@ function rowStyle(value, row, index) {
         Support all custom attributes.
         </td>
     </tr>
+    <tr>
+        <td>customSearch</td>
+        <td>data-custom-search</td>
+        <td>Function</td>
+        <td>$.noop</td>
+        <td>
+        The custom search function is executed instead of built-in search function, takes one parameters: <br>
+        text: the search text.<br>
+        Example usage:<br>
+        <pre>
+        function customSearch(text) {
+            //Search logic here. 
+            //You must use `this.data` array in order to filter the data. NO use `this.options.data`.
+        }
+        </pre>
+        </td>
+    </tr>
+    <tr>
+        <td>customSort</td>
+        <td>data-custom-sort</td>
+        <td>Function</td>
+        <td>$.noop</td>
+        <td>
+        The custom sort function is executed instead of built-in sort function, takes two parameters: <br>
+        sortName: the sort name.<br>
+        sortOrder: the sort order.<br>
+        Example usage:<br>
+        <pre>
+        function customSort(sortName, sortOrder) {
+            //Sort logic here. 
+            //You must use `this.data` array in order to sort the data. NO use `this.options.data`.
+        }
+        </pre>
+        </td>
+    </tr>
      <tr>
         <td>locale</td>
         <td>data-locale</td>

+ 35 - 0
docs/_i18n/es/documentation/table-options.md

@@ -487,5 +487,40 @@ Las opciones de la tabla están definidas en `jQuery.fn.bootstrapTable.defaults`
         Soporta cualquier atributo customizable.
         </td>
     </tr>
+    <tr>
+            <td>customSearch</td>
+            <td>data-custom-search</td>
+            <td>Function</td>
+            <td>$.noop</td>
+            <td>
+            The custom search function is executed instead of built-in search function, takes one parameters: <br>
+            text: the search text.<br>
+            Example usage:<br>
+            <pre>
+            function customSearch(text) {
+                //Search logic here. 
+                //You must use `this.data` array in order to filter the data. NO use `this.options.data`.
+            }
+            </pre>
+            </td>
+        </tr>
+        <tr>
+            <td>customSort</td>
+            <td>data-custom-sort</td>
+            <td>Function</td>
+            <td>$.noop</td>
+            <td>
+            The custom sort function is executed instead of built-in sort function, takes two parameters: <br>
+            sortName: the sort name.<br>
+            sortOrder: the sort order.<br>
+            Example usage:<br>
+            <pre>
+            function customSort(sortName, sortOrder) {
+                //Sort logic here. 
+                //You must use `this.data` array in order to sort the data. NO use `this.options.data`.
+            }
+            </pre>
+            </td>
+        </tr>
     </tbody>
 </table>

+ 35 - 0
docs/_i18n/zh-cn/documentation/table-options.md

@@ -506,5 +506,40 @@
         Support all custom attributes.
         </td>
     </tr>
+    <tr>
+            <td>customSearch</td>
+            <td>data-custom-search</td>
+            <td>Function</td>
+            <td>$.noop</td>
+            <td>
+            The custom search function is executed instead of built-in search function, takes one parameters: <br>
+            text: the search text.<br>
+            Example usage:<br>
+            <pre>
+            function customSearch(text) {
+                //Search logic here. 
+                //You must use `this.data` array in order to filter the data. NO use `this.options.data`.
+            }
+            </pre>
+            </td>
+        </tr>
+        <tr>
+            <td>customSort</td>
+            <td>data-custom-sort</td>
+            <td>Function</td>
+            <td>$.noop</td>
+            <td>
+            The custom sort function is executed instead of built-in sort function, takes two parameters: <br>
+            sortName: the sort name.<br>
+            sortOrder: the sort order.<br>
+            Example usage:<br>
+            <pre>
+            function customSort(sortName, sortOrder) {
+                //Sort logic here. 
+                //You must use `this.data` array in order to sort the data. NO use `this.options.data`.
+            }
+            </pre>
+            </td>
+        </tr>
     </tbody>
 </table>

+ 42 - 1
src/bootstrap-table.js

@@ -336,6 +336,10 @@
             detailClose: 'glyphicon-minus icon-minus'
         },
 
+        customSearch: $.noop,
+
+        customSort: $.noop,
+
         rowStyle: function (row, index) {
             return {};
         },
@@ -860,6 +864,11 @@
             order = this.options.sortOrder === 'desc' ? -1 : 1,
             index = $.inArray(this.options.sortName, this.header.fields);
 
+        if (this.options.customSort !== $.noop) {
+            this.options.customSort.apply(this, [this.options.sortName, this.options.sortOrder]);
+            return;
+        }
+
         if (index !== -1) {
             this.data.sort(function (a, b) {
                 if (that.header.sortNames[index]) {
@@ -1125,6 +1134,10 @@
         var that = this;
 
         if (this.options.sidePagination !== 'server') {
+            if (this.options.customSearch !== $.noop) {
+                this.options.customSearch.apply(this, [this.searchText]);
+                return;
+            }
             var s = this.searchText && this.searchText.toLowerCase();
             var f = $.isEmptyObject(this.filterColumns) ? null : this.filterColumns;
 
@@ -2588,6 +2601,32 @@
         });
     };
 
+    BootstrapTable.prototype.toggleAllColumns = function (visible) {
+        $.each(this.columns, function (i, column) {
+            this.columns[i].visible = visible;
+        });
+
+        this.initHeader();
+        this.initSearch();
+        this.initPagination();
+        this.initBody();
+        if (this.options.showColumns) {
+            var $items = this.$toolbar.find('.keep-open input').prop('disabled', false);
+
+            if ($items.filter(':checked').length <= this.options.minimumCountColumns) {
+                $items.filter(':checked').prop('disabled', true);
+            }
+        }
+    };
+
+    BootstrapTable.prototype.showAllColumns = function () {
+        this.toggleAllColumns(true);
+    };
+
+    BootstrapTable.prototype.hideAllColumns = function () {
+        this.toggleAllColumns(false);
+    };
+
     BootstrapTable.prototype.filterBy = function (columns) {
         this.filterColumns = $.isEmptyObject(columns) ? {} : columns;
         this.options.pageNumber = 1;
@@ -2756,6 +2795,7 @@
         'destroy',
         'showLoading', 'hideLoading',
         'showColumn', 'hideColumn', 'getHiddenColumns',
+        'showAllColumns', 'hideAllColumns',
         'filterBy',
         'scrollTo',
         'getScrollPosition',
@@ -2811,7 +2851,8 @@
         sprintf: sprintf,
         getFieldIndex: getFieldIndex,
         compareObjects: compareObjects,
-        calculateObjectValue: calculateObjectValue
+        calculateObjectValue: calculateObjectValue,
+        getItemField: getItemField
     };
 
     // BOOTSTRAP TABLE INIT

+ 57 - 1
src/extensions/reorder-columns/bootstrap-table-reorder-columns.js

@@ -8,6 +8,44 @@
 
     'use strict';
 
+    //From MDN site, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
+    var filterFn = function () {
+        if (!Array.prototype.filter) {
+            Array.prototype.filter = function(fun/*, thisArg*/) {
+                'use strict';
+
+                if (this === void 0 || this === null) {
+                    throw new TypeError();
+                }
+
+                var t = Object(this);
+                var len = t.length >>> 0;
+                if (typeof fun !== 'function') {
+                    throw new TypeError();
+                }
+
+                var res = [];
+                var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
+                for (var i = 0; i < len; i++) {
+                    if (i in t) {
+                        var val = t[i];
+
+                        // NOTE: Technically this should Object.defineProperty at
+                        //       the next index, as push can be affected by
+                        //       properties on Object.prototype and Array.prototype.
+                        //       But that method's new, and collisions should be
+                        //       rare, so use the more-compatible alternative.
+                        if (fun.call(thisArg, val, i, t)) {
+                            res.push(val);
+                        }
+                    }
+                }
+
+                return res;
+            };
+        }
+    };
+
     $.extend($.fn.bootstrapTable.defaults, {
         reorderableColumns: false,
         maxMovingRows: 10,
@@ -85,7 +123,8 @@
                     formatters = [],
                     columns = [],
                     columnsHidden = [],
-                    columnIndex = -1;
+                    columnIndex = -1,
+                    optionsColumns = [];
                 that.$header.find('th').each(function (i) {
                     ths.push($(this).data('field'));
                     formatters.push($(this).data('formatter'));
@@ -111,6 +150,23 @@
                 }
 
                 that.columns = that.columns.concat(columns);
+
+                filterFn(); //Support <IE9
+                $.each(that.columns, function(i, column) {
+                    var found = false,
+                        field = column.field;
+                    that.options.columns[0].filter(function(item) {
+                        if(!found && item["field"] == field) {
+                            optionsColumns.push(item);
+                            found = true;
+                            return false;
+                        } else
+                            return true;
+                    })
+                });
+
+                that.options.columns[0] = optionsColumns;
+
                 that.header.fields = ths;
                 that.header.formatters = formatters;
                 that.resetView();