bootstrap-table-export.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * extensions: https://github.com/kayalshri/tableExport.jquery.plugin
  4. */
  5. (function ($) {
  6. 'use strict';
  7. var TYPE_NAME = {
  8. json: 'JSON',
  9. xml: 'XML',
  10. png: 'PNG',
  11. csv: 'CSV',
  12. txt: 'TXT',
  13. sql: 'SQL',
  14. doc: 'MS-Word',
  15. excel: 'Ms-Excel',
  16. powerpoint: 'Ms-Powerpoint',
  17. pdf: 'PDF'
  18. };
  19. $.extend($.fn.bootstrapTable.defaults, {
  20. showExport: false,
  21. // 'json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'powerpoint', 'pdf'
  22. exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel']
  23. });
  24. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  25. _initToolbar = BootstrapTable.prototype.initToolbar;
  26. BootstrapTable.prototype.initToolbar = function () {
  27. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  28. if (this.options.showExport) {
  29. var that = this,
  30. $btnGroup = this.$toolbar.find('>.btn-group'),
  31. $export = $btnGroup.find('div.export');
  32. if (!$export.length) {
  33. $export = $([
  34. '<div class="export btn-group">',
  35. '<button class="btn btn-default dropdown-toggle" ' +
  36. 'data-toggle="dropdown" type="button">',
  37. '<i class="glyphicon glyphicon-export icon-share"></i> ',
  38. '<span class="caret"></span>',
  39. '</button>',
  40. '<ul class="dropdown-menu" role="menu">',
  41. '</ul>',
  42. '</div>'].join('')).appendTo($btnGroup);
  43. var $menu = $export.find('.dropdown-menu'),
  44. exportTypes = this.options.exportTypes;
  45. if (typeof this.options.exportTypes === 'string') {
  46. var types = this.options.exportTypes.slice(1, -1).replace(/ /g, '').split(',');
  47. exportTypes = [];
  48. $.each(types, function (i, value) {
  49. exportTypes.push(value.slice(1, -1));
  50. });
  51. }
  52. $.each(exportTypes, function (i, type) {
  53. if (TYPE_NAME.hasOwnProperty(type)) {
  54. $menu.append(['<li data-type="' + type + '">',
  55. '<a href="javascript:void(0)">',
  56. TYPE_NAME[type],
  57. '</a>',
  58. '</li>'].join(''));
  59. }
  60. });
  61. $menu.find('li').click(function () {
  62. that.$el.tableExport({
  63. type: $(this).data('type'),
  64. escape: false
  65. });
  66. });
  67. }
  68. }
  69. };
  70. })(jQuery);