ExpandableTable.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_EXPANDED = `expanded${EVENT_KEY}`
  17. const EVENT_COLLAPSED = `collapsed${EVENT_KEY}`
  18. const CLASS_NAME_HEADER = 'expandable-header'
  19. const SELECTOR_HEADER = `.${CLASS_NAME_HEADER}`
  20. const SELECTOR_DATA_SELECTOR = 'data-expandable-table'
  21. const SELECTOR_EXPANDED = 'expanded'
  22. const SELECTOR_COLLAPSED = 'collapsed'
  23. /**
  24. * Class Definition
  25. * ====================================================
  26. */
  27. class ExpandableTable {
  28. constructor(element, config) {
  29. this._config = config
  30. this._element = element
  31. }
  32. // Public
  33. init() {
  34. $(SELECTOR_HEADER).each((_, $header) => {
  35. // Next Child to the header will have the same column span as header
  36. $($header).next().children().first().attr('colspan', $($header).children().length)
  37. // Setting up table design for the first time
  38. const $type = $($header).next().attr(SELECTOR_DATA_SELECTOR)
  39. const $body = $($header).next().children().first().children()
  40. if ($type === SELECTOR_EXPANDED) {
  41. $body.show()
  42. } else if ($type === SELECTOR_COLLAPSED) {
  43. $body.hide()
  44. $body.parent().parent().addClass('d-none')
  45. }
  46. })
  47. }
  48. toggleRow() {
  49. const $element = this._element
  50. const time = 500
  51. const $type = $element.next().attr(SELECTOR_DATA_SELECTOR)
  52. const $body = $element.next().children().first().children()
  53. $body.stop()
  54. if ($type === SELECTOR_EXPANDED) {
  55. $body.slideUp(time, () => {
  56. $element.next().addClass('d-none')
  57. })
  58. $element.next().attr(SELECTOR_DATA_SELECTOR, SELECTOR_COLLAPSED)
  59. $element.trigger($.Event(EVENT_COLLAPSED))
  60. } else if ($type === SELECTOR_COLLAPSED) {
  61. $element.next().removeClass('d-none')
  62. $body.slideDown(time)
  63. $element.next().attr(SELECTOR_DATA_SELECTOR, SELECTOR_EXPANDED)
  64. $element.trigger($.Event(EVENT_EXPANDED))
  65. }
  66. }
  67. // Static
  68. static _jQueryInterface(config) {
  69. return this.each(function () {
  70. let data = $(this).data(DATA_KEY)
  71. if (!data) {
  72. data = new ExpandableTable($(this))
  73. $(this).data(DATA_KEY, data)
  74. }
  75. if (config === 'init' || config === 'toggleRow') {
  76. data[config]()
  77. }
  78. })
  79. }
  80. }
  81. /**
  82. * Data API
  83. * ====================================================
  84. */
  85. $(CLASS_NAME_TABLE).ready(function () {
  86. ExpandableTable._jQueryInterface.call($(this), 'init')
  87. })
  88. $(document).on('click', SELECTOR_HEADER, function () {
  89. ExpandableTable._jQueryInterface.call($(this), 'toggleRow')
  90. })
  91. /**
  92. * jQuery API
  93. * ====================================================
  94. */
  95. $.fn[NAME] = ExpandableTable._jQueryInterface
  96. $.fn[NAME].Constructor = ExpandableTable
  97. $.fn[NAME].noConflict = function () {
  98. $.fn[NAME] = JQUERY_NO_CONFLICT
  99. return ExpandableTable._jQueryInterface
  100. }
  101. export default ExpandableTable