bootstrap-table-template.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. !function ($) {
  2. 'use strict';
  3. $.extend($.fn.bootstrapTable.defaults, {
  4. templateView: false,
  5. templateFormatter: "itemtpl",
  6. templateParentClass: "row row-flex",
  7. templateTableClass: "table-template",
  8. });
  9. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  10. _initContainer = BootstrapTable.prototype.initContainer,
  11. _initBody = BootstrapTable.prototype.initBody,
  12. _initRow = BootstrapTable.prototype.initRow;
  13. BootstrapTable.prototype.initContainer = function () {
  14. _initContainer.apply(this, Array.prototype.slice.apply(arguments));
  15. var that = this;
  16. if (!that.options.templateView) {
  17. return;
  18. }
  19. };
  20. BootstrapTable.prototype.initBody = function () {
  21. var that = this;
  22. $.extend(that.options, {
  23. showHeader: !that.options.templateView ? $.fn.bootstrapTable.defaults.showHeader : false,
  24. showFooter: !that.options.templateView ? $.fn.bootstrapTable.defaults.showFooter : false,
  25. });
  26. $(that.$el).toggleClass(that.options.templateTableClass, that.options.templateView);
  27. _initBody.apply(this, Array.prototype.slice.apply(arguments));
  28. if (!that.options.templateView) {
  29. return;
  30. } else {
  31. //由于Bootstrap是基于Table的,添加一个父类容器
  32. $("> *", that.$body).wrapAll($("<div />").addClass(that.options.templateParentClass));
  33. }
  34. };
  35. BootstrapTable.prototype.initRow = function (item, i, data, parentDom) {
  36. var that = this;
  37. //如果未启用则使用原生的initRow方法
  38. if (!that.options.templateView) {
  39. return _initRow.apply(that, Array.prototype.slice.apply(arguments));
  40. }
  41. var $content = '';
  42. if (typeof that.options.templateFormatter === 'function') {
  43. $content = that.options.templateFormatter.call(that, item, i, data);
  44. } else {
  45. var Template = require('template');
  46. $content = Template(that.options.templateFormatter, {item: item, i: i, data: data});
  47. }
  48. return $content;
  49. };
  50. }(jQuery);