ExpandableTable.js 3.2 KB

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