浏览代码

Add export extension.

zhixin 11 年之前
父节点
当前提交
296874fa6d
共有 2 个文件被更改,包括 111 次插入0 次删除
  1. 40 0
      src/extensions/export/README.md
  2. 71 0
      src/extensions/export/bootstrap-table-export.js

+ 40 - 0
src/extensions/export/README.md

@@ -0,0 +1,40 @@
+# Table Export
+
+Use Plugin: [tableExport.jquery.plugin](https://github.com/kayalshri/tableExport.jquery.plugin)
+
+## Usage
+
+```html
+<script src="extensions/export/bootstrap-table-export.js"></script>
+```
+
+## Options
+
+* showExport: set `true` to show export button, default: `true`.
+
+* exportTypes: export types, default: `['json', 'xml', 'csv', 'txt', 'sql', 'excel']`, support types: 'json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'powerpoint', 'pdf'
+
+## Examples
+
+```html
+<script src="assets/table-export/tableExport.js"></script>
+<script src="assets/table-export/jquery.base64.js"></script>
+<script src="../src/bootstrap-table.js"></script>
+<script src="../src/extensions/export/bootstrap-table-export.js"></script>
+<table data-toggle="table"
+       data-url="data1.json"
+       data-show-columns="true"
+       data-search="true"
+       data-show-refresh="true"
+       data-show-toggle="true"
+       data-pagination="true"
+       data-height="500">
+    <thead>
+    <tr>
+        <th data-field="id">ID</th>
+        <th data-field="name">Item Name</th>
+        <th data-field="price">Item Price</th>
+    </tr>
+    </thead>
+</table>
+```

+ 71 - 0
src/extensions/export/bootstrap-table-export.js

@@ -0,0 +1,71 @@
+/**
+ * @author zhixin wen <wenzhixin2010@gmail.com>
+ * extensions: https://github.com/kayalshri/tableExport.jquery.plugin
+ */
+
+(function ($) {
+    'use strict';
+
+    var TYPE_NAME = {
+        json: 'JSON',
+        xml: 'XML',
+        png: 'PNG',
+        csv: 'CSV',
+        txt: 'TXT',
+        sql: 'SQL',
+        doc: 'MS-Word',
+        excel: 'Ms-Excel',
+        powerpoint: 'Ms-Powerpoint',
+        pdf: 'PDF'
+    };
+
+    $.extend($.fn.bootstrapTable.defaults, {
+        showExport: true,
+        // 'json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'powerpoint', 'pdf'
+        exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel']
+    });
+
+    var BootstrapTable = $.fn.bootstrapTable.Constructor,
+        _initToolbar = BootstrapTable.prototype.initToolbar;
+
+    BootstrapTable.prototype.initToolbar = function () {
+        _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
+
+        if (this.options.showExport) {
+            var that = this,
+                $btnGroup = this.$toolbar.find('>.btn-group'),
+                $export = $btnGroup.find('div.export');
+
+            if (!$export.length) {
+                $export = $([
+                    '<div class="export btn-group">',
+                        '<button class="btn btn-default dropdown-toggle" ' +
+                            'data-toggle="dropdown" type="button">',
+                            '<i class="glyphicon glyphicon-export icon-export"></i> ',
+                            '<span class="caret"></span>',
+                        '</button>',
+                        '<ul class="dropdown-menu" role="menu">',
+                        '</ul>',
+                    '</div>'].join('')).appendTo($btnGroup);
+
+                var $menu = $export.find('.dropdown-menu');
+                $.each(this.options.exportTypes, function (i, type) {
+                    if (TYPE_NAME.hasOwnProperty(type)) {
+                        $menu.append(['<li data-type="' + type + '">',
+                                '<a href="javascript:void(0)">',
+                                    TYPE_NAME[type],
+                                '</a>',
+                            '</li>'].join(''));
+                    }
+                });
+
+                $menu.find('li').click(function () {
+                    that.$el.tableExport({
+                        type: $(this).data('type'),
+                        escape: false
+                    });
+                });
+            }
+        }
+    };
+})(jQuery);