bootstrap-table-custom-view.js 2.8 KB

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