ExpandableTable.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE ExpandableTable.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'ExpandableTable'
  13. const DATA_KEY = 'lte.expandableTable'
  14. const EVENT_KEY = `.${DATA_KEY}`
  15. const JQUERY_NO_CONFLICT = $.fn[NAME]
  16. const Event = {
  17. EXPANDED: `expanded${EVENT_KEY}`,
  18. COLLAPSED: `collapsed${EVENT_KEY}`
  19. }
  20. const ClassName = {
  21. HEADER: 'expandable-header'
  22. }
  23. const Selector = {
  24. HEADER: `.${ClassName.HEADER}`,
  25. DATA_SELECTOR: 'data-expandable-table',
  26. EXPANDED: 'expanded',
  27. COLLAPSED: 'collapsed'
  28. }
  29. /**
  30. * Class Definition
  31. * ====================================================
  32. */
  33. class ExpandableTable {
  34. constructor(element, config) {
  35. this._config = config
  36. this._element = element
  37. }
  38. // Public
  39. init() {
  40. $(Selector.HEADER).each((_, $header) => {
  41. // Next Child to the header will have the same column span as header
  42. $($header).next().children().first().attr('colSpan', $($header).children().length)
  43. // Setting up table design for the first time
  44. const $type = $($header).next().attr(Selector.DATA_SELECTOR)
  45. const $body = $($header).next().children().first().children()
  46. if ($type === Selector.EXPANDED) {
  47. $body.show()
  48. } else if ($type === Selector.COLLAPSED) {
  49. $body.hide()
  50. $body.parent().parent().addClass('d-none')
  51. }
  52. })
  53. }
  54. toggleRow() {
  55. const $element = this._element
  56. const time = 500
  57. const $type = $element.next().attr(Selector.DATA_SELECTOR)
  58. const $body = $element.next().children().first().children()
  59. $body.stop()
  60. if ($type === Selector.EXPANDED) {
  61. $body.slideUp(time, () => {
  62. $element.next().addClass('d-none')
  63. })
  64. $element.next().attr(Selector.DATA_SELECTOR, Selector.COLLAPSED)
  65. $element.trigger($.Event(Event.COLLAPSED))
  66. } else if ($type === Selector.COLLAPSED) {
  67. $element.next().removeClass('d-none')
  68. $body.slideDown(time)
  69. $element.next().attr(Selector.DATA_SELECTOR, Selector.EXPANDED)
  70. $element.trigger($.Event(Event.EXPANDED))
  71. }
  72. }
  73. // Static
  74. static _jQueryInterface(config) {
  75. return this.each(function () {
  76. let data = $(this).data(DATA_KEY)
  77. if (!data) {
  78. data = new ExpandableTable($(this))
  79. $(this).data(DATA_KEY, data)
  80. }
  81. if (config === 'init' || config === 'toggleRow') {
  82. data[config]()
  83. }
  84. })
  85. }
  86. }
  87. /**
  88. * Data API
  89. * ====================================================
  90. */
  91. $(ClassName.TABLE).ready(function () {
  92. ExpandableTable._jQueryInterface.call($(this), 'init')
  93. })
  94. $(document).on('click', Selector.HEADER, function () {
  95. ExpandableTable._jQueryInterface.call($(this), 'toggleRow')
  96. })
  97. /**
  98. * jQuery API
  99. * ====================================================
  100. */
  101. $.fn[NAME] = ExpandableTable._jQueryInterface
  102. $.fn[NAME].Constructor = ExpandableTable
  103. $.fn[NAME].noConflict = function () {
  104. $.fn[NAME] = JQUERY_NO_CONFLICT
  105. return ExpandableTable._jQueryInterface
  106. }
  107. export default ExpandableTable