浏览代码

Merge pull request #532 from macias3/master

New methods to select rows by value
文翼 11 年之前
父节点
当前提交
0362b8fd85
共有 1 个文件被更改,包括 30 次插入1 次删除
  1. 30 1
      src/bootstrap-table.js

+ 30 - 1
src/bootstrap-table.js

@@ -1511,10 +1511,17 @@
     };
 
     BootstrapTable.prototype.checkAll_ = function (checked) {
+        var rows;
+        if(!checked) {
+            rows = this.getSelections();
+        }
         this.$selectItem.filter(':enabled').prop('checked', checked);
         this.updateRows(checked);
         this.updateSelected();
-        this.trigger(checked ? 'check-all' : 'uncheck-all');
+        if(checked) {
+            rows = this.getSelections();
+        }
+        this.trigger(checked ? 'check-all' : 'uncheck-all', rows);
     };
 
     BootstrapTable.prototype.check = function (index) {
@@ -1531,6 +1538,27 @@
         this.updateSelected();
         this.trigger(checked ? 'check' : 'uncheck', this.data[index]);
     };
+    
+    BootstrapTable.prototype.checkBy = function (obj) {
+        this.checkBy_(true, obj);
+    };
+
+    BootstrapTable.prototype.uncheckBy = function (obj) {
+        this.checkBy_(false, obj);
+    };
+    
+    BootstrapTable.prototype.checkBy_ = function (checked, obj) {
+        var that = this;
+        var is_array = $.isArray(obj.value);
+        $.each(this.data, function (index, row) {
+            if(is_array ? $.inArray(row[obj.field], obj.value) != -1 : row[obj.field] == obj.value) {
+                that.$selectItem.filter(sprintf('[data-index="%s"]', index)).prop('checked', checked);
+                row[that.header.stateField] = checked;
+                that.trigger(checked ? 'check' : 'uncheck', row);  
+            }
+        });
+        this.updateSelected();
+    };
 
     BootstrapTable.prototype.destroy = function () {
         this.$el.insertBefore(this.$container);
@@ -1632,6 +1660,7 @@
         'mergeCells',
         'checkAll', 'uncheckAll',
         'check', 'uncheck',
+        'checkBy', 'uncheckBy',
         'refresh',
         'resetView',
         'destroy',