bootstrap-table-custom-view.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @author: Dustin Utecht
  3. * @github: https://github.com/UtechtDustin
  4. */
  5. const Utils = $.fn.bootstrapTable.utils
  6. Object.assign($.fn.bootstrapTable.defaults, {
  7. customView: false,
  8. showCustomView: false,
  9. customViewDefaultView: false
  10. })
  11. Object.assign($.fn.bootstrapTable.defaults.icons, {
  12. customViewOn: {
  13. bootstrap3: 'glyphicon glyphicon-list',
  14. bootstrap5: 'bi-list',
  15. bootstrap4: 'fa fa-list',
  16. semantic: 'fa fa-list',
  17. foundation: 'fa fa-list',
  18. bulma: 'fa fa-list',
  19. materialize: 'list'
  20. }[$.fn.bootstrapTable.theme] || 'fa-list',
  21. customViewOff: {
  22. bootstrap3: 'glyphicon glyphicon-thumbnails',
  23. bootstrap5: 'bi-grid',
  24. bootstrap4: 'fa fa-th',
  25. semantic: 'fa fa-th',
  26. foundation: 'fa fa-th',
  27. bulma: 'fa fa-th',
  28. materialize: 'grid_on'
  29. }[$.fn.bootstrapTable.theme] || 'fa-th'
  30. })
  31. Object.assign($.fn.bootstrapTable.defaults, {
  32. onCustomViewPostBody () {
  33. return false
  34. },
  35. onCustomViewPreBody () {
  36. return false
  37. },
  38. onToggleCustomView () {
  39. return false
  40. }
  41. })
  42. Object.assign($.fn.bootstrapTable.locales, {
  43. formatToggleCustomViewOn () {
  44. return 'Show custom view'
  45. },
  46. formatToggleCustomViewOff () {
  47. return 'Hide custom view'
  48. }
  49. })
  50. Object.assign($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
  51. $.fn.bootstrapTable.methods.push('toggleCustomView')
  52. Object.assign($.fn.bootstrapTable.events, {
  53. 'custom-view-post-body.bs.table': 'onCustomViewPostBody',
  54. 'custom-view-pre-body.bs.table': 'onCustomViewPreBody',
  55. 'toggle-custom-view.bs.table': 'onToggleCustomView'
  56. })
  57. $.BootstrapTable = class extends $.BootstrapTable {
  58. init () {
  59. this.customViewDefaultView = this.options.customViewDefaultView
  60. super.init()
  61. }
  62. initToolbar (...args) {
  63. if (this.options.customView && this.options.showCustomView) {
  64. this.buttons = Object.assign(this.buttons, {
  65. customView: {
  66. text: this.options.customViewDefaultView ? this.options.formatToggleCustomViewOff() : this.options.formatToggleCustomViewOn(),
  67. icon: this.options.customViewDefaultView ? this.options.icons.customViewOn : this.options.icons.customViewOff,
  68. event: this.toggleCustomView,
  69. attributes: {
  70. 'aria-label': this.options.customViewDefaultView ? this.options.formatToggleCustomViewOff() : this.options.formatToggleCustomViewOn(),
  71. title: this.options.customViewDefaultView ? this.options.formatToggleCustomViewOff() : this.options.formatToggleCustomViewOn()
  72. }
  73. }
  74. })
  75. }
  76. super.initToolbar(...args)
  77. }
  78. initBody () {
  79. super.initBody()
  80. if (!this.options.customView) {
  81. return
  82. }
  83. const $table = this.$el
  84. const $customViewContainer = this.$container.find('.fixed-table-custom-view')
  85. $table.hide()
  86. $customViewContainer.hide()
  87. if (!this.options.customView || !this.customViewDefaultView) {
  88. $table.show()
  89. return
  90. }
  91. const data = this.getData().slice(this.pageFrom - 1, this.pageTo)
  92. const value = Utils.calculateObjectValue(this, this.options.customView, [data], '')
  93. this.trigger('custom-view-pre-body', data, value)
  94. if ($customViewContainer.length === 1) {
  95. $customViewContainer.show().html(value)
  96. } else {
  97. this.$tableBody.after(`<div class="fixed-table-custom-view">${value}</div>`)
  98. }
  99. this.trigger('custom-view-post-body', data, value)
  100. }
  101. toggleCustomView () {
  102. this.customViewDefaultView = !this.customViewDefaultView
  103. const icon = this.options.showButtonIcons ? this.customViewDefaultView ? this.options.icons.customViewOn : this.options.icons.customViewOff : ''
  104. const text = this.options.showButtonText ? this.customViewDefaultView ? this.options.formatToggleCustomViewOff() : this.options.formatToggleCustomViewOn() : ''
  105. this.$toolbar.find('button[name="customView"]')
  106. .html(`${Utils.sprintf(this.constants.html.icon, this.options.iconsPrefix, icon)} ${text}`)
  107. .attr('aria-label', text)
  108. .attr('title', text)
  109. this.initBody()
  110. this.trigger('toggle-custom-view', this.customViewDefaultView)
  111. }
  112. }