Browse Source

Fix #13: add 'onAll' event and jQuery events.

zhixin 11 years ago
parent
commit
9782e4f048
2 changed files with 56 additions and 9 deletions
  1. 21 0
      docs/examples.html
  2. 35 9
      src/bootstrap-table.js

+ 21 - 0
docs/examples.html

@@ -513,6 +513,10 @@
                             var $result = $('#events-result');
                             var $result = $('#events-result');
 
 
                             $('#events-table').bootstrapTable({
                             $('#events-table').bootstrapTable({
+                                /*
+                                onAll: function(name, args) {
+                                    console.log('Event: onAll, data: ', args);
+                                }
                                 onClickRow: function(row) {
                                 onClickRow: function(row) {
                                     $result.text('Event: onClickRow, data: ' + JSON.stringify(row));
                                     $result.text('Event: onClickRow, data: ' + JSON.stringify(row));
                                 },
                                 },
@@ -534,6 +538,23 @@
                                 onUncheckAll: function() {
                                 onUncheckAll: function() {
                                     $result.text('Event: onUncheckAll');
                                     $result.text('Event: onUncheckAll');
                                 }
                                 }
+                                */
+                            }).on('all.bs.table', function(e, name, args) {
+                                console.log('Event:', name, ', data:', args);
+                            }).on('click-row.bs.table', function(e, row, $element) {
+                                $result.text('Event: click-row.bs.table, data: ' + JSON.stringify(row));
+                            }).on('dbl-click-row.bs.table', function(e, row, $element) {
+                                $result.text('Event: dbl-click-row.bs.table, data: ' + JSON.stringify(row));
+                            }).on('sort.bs.table', function(e, name, order) {
+                                $result.text('Event: sort.bs.table, data: ' + name + ', ' + order);
+                            }).on('check.bs.table', function(e, row) {
+                                $result.text('Event: check.bs.table, data: ' + JSON.stringify(row));
+                            }).on('uncheck.bs.table', function(e, row) {
+                                $result.text('Event: uncheck.bs.table, data: ' + JSON.stringify(row));
+                            }).on('check-all.bs.table', function(e) {
+                                $result.text('Event: check-all.bs.table');
+                            }).on('uncheck-all.bs.table', function(e) {
+                                $result.text('Event: uncheck-all.bs.table');
                             });
                             });
                         });
                         });
                     </script>
                     </script>

+ 35 - 9
src/bootstrap-table.js

@@ -103,6 +103,7 @@
             return 'No matching records found';
             return 'No matching records found';
         },
         },
 
 
+        onAll: function(name, args) {return false;},
         onClickRow: function(item, $element) {return false;},
         onClickRow: function(item, $element) {return false;},
         onDblClickRow: function(item, $element) {return false;},
         onDblClickRow: function(item, $element) {return false;},
         onSort: function(name, order) {return false;},
         onSort: function(name, order) {return false;},
@@ -130,6 +131,20 @@
         sorter: undefined
         sorter: undefined
     };
     };
 
 
+    BootstrapTable.EVENTS = {
+        'all.bs.table': 'onAll',
+        'click-row.bs.table': 'onClickRow',
+        'dbl-click-row.bs.table': 'onDblClickRow',
+        'sort.bs.table': 'onSort',
+        'check.bs.table': 'onCheck',
+        'uncheck.bs.table': 'onUncheck',
+        'check-all.bs.table': 'onCheckAll',
+        'uncheck-all.bs.table': 'onUncheckAll',
+        'load-success.bs.table': 'onLoadSuccess',
+        'load-error.bs.table': 'onLoadError',
+        'before-load.bs.table': 'onBeforeLoad'
+    };
+
     BootstrapTable.prototype.init = function() {
     BootstrapTable.prototype.init = function() {
         this.initContainer();
         this.initContainer();
         this.initHeader();
         this.initHeader();
@@ -302,7 +317,7 @@
         this.$header.find('span.order').remove();
         this.$header.find('span.order').remove();
         this.options.sortName = $this.data('field');
         this.options.sortName = $this.data('field');
         this.options.sortOrder = $this.data('order') === 'asc' ? 'desc' : 'asc';
         this.options.sortOrder = $this.data('order') === 'asc' ? 'desc' : 'asc';
-        this.options.onSort(this.options.sortName, this.options.sortOrder);
+        this.trigger('sort', this.options.sortName, this.options.sortOrder);
 
 
         $this.data('order', this.options.sortOrder);
         $this.data('order', this.options.sortOrder);
         $this.find('.th-inner').append(this.getCaretHtml());
         $this.find('.th-inner').append(this.getCaretHtml());
@@ -676,13 +691,13 @@
         this.$container.find('.fixed-table-body').scrollTop(0);
         this.$container.find('.fixed-table-body').scrollTop(0);
 
 
         this.$body.find('tr').off('click').on('click', function() {
         this.$body.find('tr').off('click').on('click', function() {
-            that.options.onClickRow(that.data[$(this).data('index')], $(this));
+            that.trigger('click-row', that.data[$(this).data('index')], $(this));
             if (that.options.clickToSelect) {
             if (that.options.clickToSelect) {
                 $(this).find(sprintf('[name="%s"]', that.options.selectItemName)).trigger('click');
                 $(this).find(sprintf('[name="%s"]', that.options.selectItemName)).trigger('click');
             }
             }
         });
         });
         this.$body.find('tr').off('dblclick').on('dblclick', function() {
         this.$body.find('tr').off('dblclick').on('dblclick', function() {
-            that.options.onDblClickRow(that.data[$(this).data('index')], $(this));
+            that.trigger('dbl-click-row', that.data[$(this).data('index')], $(this));
         });
         });
 
 
         this.$selectItem = this.$body.find(sprintf('[name="%s"]', this.options.selectItemName));
         this.$selectItem = this.$body.find(sprintf('[name="%s"]', this.options.selectItemName));
@@ -694,7 +709,7 @@
 
 
             that.$selectAll.prop('checked', checkAll);
             that.$selectAll.prop('checked', checkAll);
             row[that.header.stateField] = checked;
             row[that.header.stateField] = checked;
-            that.options[checked ? 'onCheck' : 'onUncheck'](row);
+            that.trigger(checked ? 'check' : 'uncheck', row);
 
 
             if (that.options.singleSelect) {
             if (that.options.singleSelect) {
                 that.$selectItem.filter(':checked').not(this).each(function() {
                 that.$selectItem.filter(':checked').not(this).each(function() {
@@ -745,17 +760,17 @@
             success: function(res) {
             success: function(res) {
                 var data = res;
                 var data = res;
 
 
-                res = that.options.onBeforeLoad(res);
+                res = that.trigger('before-load', res);
 
 
                 if (that.options.sidePagination === 'server') {
                 if (that.options.sidePagination === 'server') {
                     that.options.totalRows = res.total;
                     that.options.totalRows = res.total;
                     data = res.rows;
                     data = res.rows;
                 }
                 }
                 that.load(data);
                 that.load(data);
-                that.options.onLoadSuccess(data);
+                that.trigger('load-success', data);
             },
             },
             error: function(res) {
             error: function(res) {
-                that.options.onLoadError(res.status);
+                that.trigger('load-error', res.status);
             },
             },
             complete: function() {
             complete: function() {
                 that.$loading.hide();
                 that.$loading.hide();
@@ -787,6 +802,17 @@
         });
         });
     };
     };
 
 
+    BootstrapTable.prototype.trigger = function(name) {
+        var args = Array.prototype.slice.call(arguments, 1);
+
+        name += '.bs.table';
+        this.options[BootstrapTable.EVENTS[name]].apply(this.options, args);
+        this.$el.trigger($.Event(name), args);
+
+        this.options.onAll(name, args);
+        this.$el.trigger($.Event('all.bs.table'), [name, args]);
+    };
+
     // PUBLIC FUNCTION DEFINITION
     // PUBLIC FUNCTION DEFINITION
     // =======================
     // =======================
 
 
@@ -864,14 +890,14 @@
         this.$selectAll.prop('checked', true);
         this.$selectAll.prop('checked', true);
         this.$selectItem.prop('checked', true);
         this.$selectItem.prop('checked', true);
         this.updateRows(true);
         this.updateRows(true);
-        this.options.onCheckAll();
+        this.trigger('check-all');
     };
     };
 
 
     BootstrapTable.prototype.uncheckAll = function() {
     BootstrapTable.prototype.uncheckAll = function() {
         this.$selectAll.prop('checked', false);
         this.$selectAll.prop('checked', false);
         this.$selectItem.prop('checked', false);
         this.$selectItem.prop('checked', false);
         this.updateRows(false);
         this.updateRows(false);
-        this.options.onUncheckAll();
+        this.trigger('uncheck-all');
     };
     };
 
 
     BootstrapTable.prototype.destroy = function() {
     BootstrapTable.prototype.destroy = function() {