浏览代码

fix(js): fix #750, showRow and hideRow bug

zhixin 9 年之前
父节点
当前提交
2b5eac3ca8
共有 2 个文件被更改,包括 44 次插入24 次删除
  1. 1 0
      CHANGELOG.md
  2. 43 24
      src/bootstrap-table.js

+ 1 - 0
CHANGELOG.md

@@ -6,6 +6,7 @@ ChangeLog
 - fix(js): fix #2439, `filterBy` cannot filter array keys
 - fix(js): fix #2424, from html with checkbox bug
 - fix(js): fix #2385, checkbox render bug with formatter
+- fix(js): fix #750, showRow and hideRow bug
 - feat(js): fix #2414, add `paginationLoop` option
 - feat(cookie extension): fix #2386, add `getCookies` method
 

+ 43 - 24
src/bootstrap-table.js

@@ -570,6 +570,7 @@
         this.initTable();
         this.initHeader();
         this.initData();
+        this.initHiddenRows();
         this.initFooter();
         this.initToolbar();
         this.initPagination();
@@ -1617,6 +1618,10 @@
                 attributes = {},
                 htmlAttributes = [];
 
+            if ($.inArray(item, this.hiddenRows) > -1) {
+                continue;
+            }
+
             style = calculateObjectValue(this.options, this.options.rowStyle, [item, i], style);
 
             if (style && style.css) {
@@ -2053,6 +2058,7 @@
                 row[that.header.stateField] = false;
             }
         });
+        this.initHiddenRows();
     };
 
     BootstrapTable.prototype.trigger = function (name) {
@@ -2269,17 +2275,6 @@
         }
     };
 
-    BootstrapTable.prototype.toggleRow = function (index, uniqueId, visible) {
-        if (index === -1) {
-            return;
-        }
-
-        this.$body.find(typeof index !== 'undefined' ?
-            sprintf('tr[data-index="%s"]', index) :
-            sprintf('tr[data-uniqueid="%s"]', uniqueId))
-            [visible ? 'show' : 'hide']();
-    };
-
     BootstrapTable.prototype.getVisibleFields = function () {
         var that = this,
             visibleFields = [];
@@ -2529,28 +2524,52 @@
         this.initBody(true);
     };
 
+    BootstrapTable.prototype.initHiddenRows = function () {
+        this.hiddenRows = [];
+    };
+
     BootstrapTable.prototype.showRow = function (params) {
-        if (!params.hasOwnProperty('index') && !params.hasOwnProperty('uniqueId')) {
-            return;
-        }
-        this.toggleRow(params.index, params.uniqueId, true);
+        this.toggleRow(params, true);
     };
 
     BootstrapTable.prototype.hideRow = function (params) {
-        if (!params.hasOwnProperty('index') && !params.hasOwnProperty('uniqueId')) {
+        this.toggleRow(params, false);
+    };
+
+    BootstrapTable.prototype.toggleRow = function (params, visible) {
+        var row, index;
+
+        if (params.hasOwnProperty('index')) {
+            row = this.getData()[params.index];
+        } else if (params.hasOwnProperty('uniqueId')) {
+            row = this.getRowByUniqueId(params.uniqueId);
+        }
+
+        if (!row) {
             return;
         }
-        this.toggleRow(params.index, params.uniqueId, false);
+
+        index = $.inArray(row, this.hiddenRows);
+
+        if (!visible && index === -1) {
+            this.hiddenRows.push(row);
+        } else if (visible && index > -1) {
+            this.hiddenRows.splice(index, 1);
+        }
+        this.initBody(true);
     };
 
-    BootstrapTable.prototype.getRowsHidden = function (show) {
-        var rows = $(this.$body[0]).children().filter(':hidden'),
-            i = 0;
-        if (show) {
-            for (; i < rows.length; i++) {
-                $(rows[i]).show();
+    BootstrapTable.prototype.getHiddenRows = function (show) {
+        var that = this,
+            data = this.getData(),
+            rows = [];
+
+        $.each(data, function (i, row) {
+            if ($.inArray(row, that.hiddenRows) > -1) {
+                rows.push(row);
             }
-        }
+        });
+        this.hiddenRows = rows;
         return rows;
     };