浏览代码

Merge pull request #1435 from djhvscf/master

Added ExpandAllRows and CollapseAllRows methods
文翼 10 年之前
父节点
当前提交
8cd9a717a2

+ 4 - 2
docs/_i18n/en/documentation/events.md

@@ -84,19 +84,21 @@
     <tr>
         <td>onCheck</td>
         <td>check.bs.table</td>
-        <td>row</td>
+        <td>row, $element</td>
         <td>
         Fires when user check a row, the parameters contain: <br>
         row: the record corresponding to the clicked row.
+        $element: the DOM element checked.
         </td>
     </tr>
     <tr>
         <td>onUncheck</td>
         <td>uncheck.bs.table</td>
-        <td>row</td>
+        <td>row, $element</td>
         <td>
         Fires when user uncheck a row, the parameters contain: <br>
         row: the record corresponding to the clicked row.
+        $element: the DOM element unchecked.
         </td>
     </tr>
     <tr>

+ 20 - 0
docs/_i18n/en/documentation/methods.md

@@ -283,5 +283,25 @@ The calling method syntax: `$('#table').bootstrapTable('method', parameter);`.
         <td>cookie name</td>
         <td>Delete a cookie created. You must use: 'sortOrder', 'sortName', 'pageNumber' or 'pageList'.</td>
     </tr>
+    <tr>
+        <td>expandRow</td>
+        <td>index</td>
+        <td>Expand the row that has the index passed by parameter if the detail view option is set to True.</td>
+    </tr>
+    <tr>
+        <td>collapseRow</td>
+        <td>index</td>
+        <td>Collapse the row that has the index passed by parameter if the detail view option is set to True.</td>
+    </tr>
+    <tr>
+        <td>expandAllRows</td>
+        <td>is subtable</td>
+        <td>Expand all rows if the detail view option is set to True.</td>
+    </tr>
+    <tr>
+        <td>collapseAllRows</td>
+        <td>is subtable</td>
+        <td>Collapse all rows if the detail view option is set to True.</td>
+    </tr>
     </tbody>
 </table>

+ 2 - 0
docs/_i18n/es/documentation/events.md

@@ -87,6 +87,7 @@
         <td>
         Se ejecuta cuando el usuario chequea una fila, los parámetros contienen: <br>
         row: el registro que corresponde a la fila chequeada.
+        $element: el elemento DOM chequeado.
         </td>
     </tr>
     <tr>
@@ -96,6 +97,7 @@
         <td>
         Se ejecuta cuando el usuario des-chequea una fila, los parámetros contienen: <br>
         row: el registro que corresponde a la fila deschequeada.
+        $element: el elemento DOM deschequeado.
         </td>
     </tr>
     <tr>

+ 20 - 0
docs/_i18n/es/documentation/methods.md

@@ -274,5 +274,25 @@ Sintaxis para llamar a un método: `$('#table').bootstrapTable('method', paramet
         <td>cookie name</td>
         <td>Elimina una cookie creada. Debe usar: 'sortOrder', 'sortName', 'pageNumber' o 'pageList'.</td>
     </tr>
+    <tr>
+        <td>expandRow</td>
+        <td>index</td>
+        <td>Expande la fila que tiene el index pasado por parámetro si la opción detail view está en True.</td>
+    </tr>
+    <tr>
+        <td>collapseRow</td>
+        <td>index</td>
+        <td>Colapsa la fila que tiene el index pasado por parámetro si la opción detail view está en True.</td>
+    </tr>
+    <tr>
+        <td>expandAllRows</td>
+        <td>is subtable</td>
+        <td>Expande todas las filas si la opción detail view está en True..</td>
+    </tr>
+    <tr>
+        <td>collapseAllRows</td>
+        <td>is subtable</td>
+        <td>Colapsa todas las filas si la opción detail view está en True.</td>
+    </tr>
     </tbody>
 </table>

+ 2 - 0
docs/_i18n/zh-cn/documentation/events.md

@@ -88,6 +88,7 @@
         <td>
         Fires when user check a row, the parameters contains: <br>
         row: the record corresponding to the clicked row.
+        $element: the DOM element checked.
         </td>
     </tr>
     <tr>
@@ -97,6 +98,7 @@
         <td>
         Fires when user uncheck a row, the parameters contains: <br>
         row: the record corresponding to the clicked row.
+        $element: the DOM element unchecked.
         </td>
     </tr>
     <tr>

+ 20 - 0
docs/_i18n/zh-cn/documentation/methods.md

@@ -278,5 +278,25 @@ The calling method syntax: `$('#table').bootstrapTable('method', parameter);`.
         <td>cookie name</td>
         <td>Delete a cookie created. You must use: 'sortOrder', 'sortName', 'pageNumber' or 'pageList'.</td>
     </tr>
+    <tr>
+        <td>expandRow</td>
+        <td>index</td>
+        <td>Expand the row that has the index passed by parameter if the detail view option is set to True.</td>
+    </tr>
+    <tr>
+        <td>collapseRow</td>
+        <td>index</td>
+        <td>Collapse the row that has the index passed by parameter if the detail view option is set to True.</td>
+    </tr>
+    <tr>
+        <td>expandAllRows</td>
+        <td>is subtable</td>
+        <td>Expand all rows if the detail view option is set to True.</td>
+    </tr>
+    <tr>
+        <td>collapseAllRows</td>
+        <td>is subtable</td>
+        <td>Collapse all rows if the detail view option is set to True.</td>
+    </tr>
     </tbody>
 </table>

+ 65 - 12
src/bootstrap-table.js

@@ -212,10 +212,12 @@
     };
 
     var getItemField = function (item, field) {
-        var props = field.split('.');
         var value = item;
-        for (var p in props) {
-            value = value[props[p]];
+        if (field !== undefined) {
+            var props = field.split('.');
+            for (var p in props) {
+                value = value[props[p]];
+            }
         }
         return value;
     }
@@ -1596,8 +1598,9 @@
         this.$selectItem.off('click').on('click', function (event) {
             event.stopImmediatePropagation();
 
-            var checked = $(this).prop('checked'),
-                row = that.data[$(this).data('index')];
+            var $this = $(this),
+                checked = $this.prop('checked'),
+                row = that.data[$this.data('index')];
 
             row[that.header.stateField] = checked;
 
@@ -1609,7 +1612,7 @@
             }
 
             that.updateSelected();
-            that.trigger(checked ? 'check' : 'uncheck', row);
+            that.trigger(checked ? 'check' : 'uncheck', row, $this);
         });
 
         $.each(this.header.events, function (i, events) {
@@ -2478,17 +2481,67 @@
         this.onSearch({currentTarget: $search});
     };
 
-    BootstrapTable.prototype.expandRow = function (index) {
+    BootstrapTable.prototype.expandRow_ = function (expand, index) {
         var $tr = this.$body.find(sprintf('> tr[data-index="%s"]', index));
-        if (!$tr.next().is('tr.detail-view')) {
+        if ($tr.next().is('tr.detail-view') === (expand ? false : true)) {
             $tr.find('> td > .detail-icon').click();
         }
     };
 
+    BootstrapTable.prototype.expandRow = function (index) {
+        this.expandRow_(true, index);
+    };
+
     BootstrapTable.prototype.collapseRow = function (index) {
-        var $tr = this.$body.find(sprintf('> tr[data-index="%s"]', index));
-        if ($tr.next().is('tr.detail-view')) {
-            $tr.find('> td > .detail-icon').click();
+        this.expandRow_(false, index);
+    };
+
+    BootstrapTable.prototype.expandAllRows = function (isSubTable) {
+        if (isSubTable) {
+            var $tr = this.$body.find(sprintf('> tr[data-index="%s"]', 0)),
+                that = this,
+                detailIcon = null,
+                executeInterval = false,
+                idInterval = -1;
+
+            if (!$tr.next().is('tr.detail-view')) {
+                $tr.find('> td > .detail-icon').click();
+                executeInterval = true;
+            } else if (!$tr.next().next().is('tr.detail-view')) {
+                $tr.next().find(".detail-icon").click();
+                executeInterval = true;
+            }
+
+            if (executeInterval) {
+                try {
+                    idInterval = setInterval(function () {
+                        detailIcon = that.$body.find("tr.detail-view").last().find(".detail-icon");
+                        if (detailIcon.length > 0) {
+                            detailIcon.click();
+                        } else {
+                            clearInterval(idInterval);
+                        }
+                    }, 1);
+                } catch (ex) {
+                    clearInterval(idInterval);
+                }
+            }
+        } else {
+            var trs = this.$body.children();
+            for (var i = 0; i < trs.length; i++) {
+                this.expandRow_(true, $(trs[i]).data("index"));
+            }
+        }
+    };
+
+    BootstrapTable.prototype.collapseAllRows = function (isSubTable) {
+        if (isSubTable) {
+            this.expandRow_(false, 0);
+        } else {
+            var trs = this.$body.children();
+            for (var i = 0; i < trs.length; i++) {
+                this.expandRow_(false, $(trs[i]).data("index"));
+            }
         }
     };
 
@@ -2519,7 +2572,7 @@
         'toggleView',
         'refreshOptions',
         'resetSearch',
-        'expandRow', 'collapseRow'
+        'expandRow', 'collapseRow', 'expandAllRows', 'collapseAllRows'
     ];
 
     $.fn.bootstrapTable = function (option) {