Browse Source

[Tables] Expandable table (#2528)

* [Tables] Expandable table

* enhance expandable table

* fix AdminLTE.js export

* fix linting errors

* fix HTML validator errors

Co-authored-by: Shashank Bansal <shashank.bansal@eloquent.studio>
Co-authored-by: REJack <info@rejack.de>
bansalshashank 4 năm trước cách đây
mục cha
commit
f1ce1758ce
5 tập tin đã thay đổi với 285 bổ sung4 xóa
  1. 2 0
      build/js/AdminLTE.js
  2. 126 0
      build/js/ExpandableTable.js
  3. 13 0
      build/scss/_table.scss
  4. 140 0
      pages/tables/simple.html
  5. 4 4
      pages/widgets.html

+ 2 - 0
build/js/AdminLTE.js

@@ -3,6 +3,7 @@ import CardWidget from './CardWidget'
 import ControlSidebar from './ControlSidebar'
 import DirectChat from './DirectChat'
 import Dropdown from './Dropdown'
+import ExpandableTable from './ExpandableTable'
 import Layout from './Layout'
 import PushMenu from './PushMenu'
 import Toasts from './Toasts'
@@ -15,6 +16,7 @@ export {
   ControlSidebar,
   DirectChat,
   Dropdown,
+  ExpandableTable,
   Layout,
   PushMenu,
   Toasts,

+ 126 - 0
build/js/ExpandableTable.js

@@ -0,0 +1,126 @@
+/**
+ * --------------------------------------------
+ * AdminLTE ExpandableTable.js
+ * License MIT
+ * --------------------------------------------
+ */
+
+import $ from 'jquery'
+
+/**
+  * Constants
+  * ====================================================
+  */
+
+const NAME = 'ExpandableTable'
+const DATA_KEY = 'lte.expandableTable'
+const EVENT_KEY = `.${DATA_KEY}`
+const JQUERY_NO_CONFLICT = $.fn[NAME]
+
+const Event = {
+  EXPANDED: `expanded${EVENT_KEY}`,
+  COLLAPSED: `collapsed${EVENT_KEY}`
+}
+
+const ClassName = {
+  HEADER: 'expandable-header'
+}
+
+const Selector = {
+  HEADER: `.${ClassName.HEADER}`,
+  DATA_SELECTOR: 'data-expandable-table',
+  EXPANDED: 'expanded',
+  COLLAPSED: 'collapsed'
+}
+
+/**
+  * Class Definition
+  * ====================================================
+  */
+class ExpandableTable {
+  constructor(element, config) {
+    this._config = config
+    this._element = element
+  }
+
+  // Public
+
+  init() {
+    $(Selector.HEADER).each((_, $header) => {
+      // Next Child to the header will have the same column span as header
+      $($header).next().children().first().attr('colSpan', $($header).children().length)
+
+      // Setting up table design for the first time
+      const $type = $($header).next().attr(Selector.DATA_SELECTOR)
+      const $body = $($header).next().children().first().children()
+      if ($type === Selector.EXPANDED) {
+        $body.show()
+      } else if ($type === Selector.COLLAPSED) {
+        $body.hide()
+        $body.parent().parent().addClass('d-none')
+      }
+    })
+  }
+
+  toggleRow() {
+    const $element = this._element
+    const time = 500
+    const $type = $element.next().attr(Selector.DATA_SELECTOR)
+    const $body = $element.next().children().first().children()
+    $body.stop()
+    if ($type === Selector.EXPANDED) {
+      $body.slideUp(time, () => {
+        $element.next().addClass('d-none')
+      })
+      $element.next().attr(Selector.DATA_SELECTOR, Selector.COLLAPSED)
+      $element.trigger($.Event(Event.COLLAPSED))
+    } else if ($type === Selector.COLLAPSED) {
+      $element.next().removeClass('d-none')
+      $body.slideDown(time)
+      $element.next().attr(Selector.DATA_SELECTOR, Selector.EXPANDED)
+      $element.trigger($.Event(Event.EXPANDED))
+    }
+  }
+
+  // Static
+
+  static _jQueryInterface(config) {
+    return this.each(function () {
+      let data = $(this).data(DATA_KEY)
+      if (!data) {
+        data = new ExpandableTable($(this))
+        $(this).data(DATA_KEY, data)
+      }
+
+      if (config === 'init' || config === 'toggleRow') {
+        data[config]()
+      }
+    })
+  }
+}
+
+/**
+  * Data API
+  * ====================================================
+  */
+$(ClassName.TABLE).ready(function () {
+  ExpandableTable._jQueryInterface.call($(this), 'init')
+})
+
+$(document).on('click', Selector.HEADER, function () {
+  ExpandableTable._jQueryInterface.call($(this), 'toggleRow')
+})
+
+/**
+  * jQuery API
+  * ====================================================
+  */
+
+$.fn[NAME] = ExpandableTable._jQueryInterface
+$.fn[NAME].Constructor = ExpandableTable
+$.fn[NAME].noConflict = function () {
+  $.fn[NAME] = JQUERY_NO_CONFLICT
+  return ExpandableTable._jQueryInterface
+}
+
+export default ExpandableTable

+ 13 - 0
build/scss/_table.scss

@@ -70,3 +70,16 @@
     }
   }
 }
+
+// Expandable Table
+.expandable-body {
+  td {
+    padding: 0;
+    width: 100%;
+
+    > div,
+    > p {
+      padding: $table-cell-padding;
+    }
+  }
+}

+ 140 - 0
pages/tables/simple.html

@@ -1157,6 +1157,146 @@
           </div>
         </div>
         <!-- /.row -->
+        <div class="row">
+          <div class="col-12">
+            <div class="card">
+              <div class="card-header">
+                <h3 class="card-title">Expandable Table</h3>
+              </div>
+              <!-- ./card-header -->
+              <div class="card-body">
+                <table class="table expandable-table table-bordered">
+                  <thead>
+                    <tr>
+                      <th>#</th>
+                      <th>User</th>
+                      <th>Date</th>
+                      <th>Status</th>
+                      <th>Reason</th>
+                    </tr>
+                  </thead>
+                  <tbody>
+                    <tr class="expandable-header">
+                      <td>183</td>
+                      <td>John Doe</td>
+                      <td>11-7-2014</td>
+                      <td>Approved</td>
+                      <td>Bacon ipsum dolor sit amet salami venison chicken flank fatback doner.</td>
+                    </tr>
+                    <tr class="expandable-body" data-expandable-table='collapsed'>
+                      <td>
+                        <p>
+                          Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
+                        </p>
+                      </td>
+                    </tr>
+                    <tr class="expandable-header">
+                      <td>219</td>
+                      <td>Alexander Pierce</td>
+                      <td>11-7-2014</td>
+                      <td>Pending</td>
+                      <td>Bacon ipsum dolor sit amet salami venison chicken flank fatback doner.</td>
+                    </tr>
+                    <tr class="expandable-body" data-expandable-table='expanded'>
+                      <td>
+                        <p>
+                          Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
+                        </p>
+                      </td>
+                    </tr>
+                    <tr class="expandable-header">
+                      <td>657</td>
+                      <td>Alexander Pierce</td>
+                      <td>11-7-2014</td>
+                      <td>Approved</td>
+                      <td>Bacon ipsum dolor sit amet salami venison chicken flank fatback doner.</td>
+                    </tr>
+                    <tr class="expandable-body" data-expandable-table='expanded'>
+                      <td>
+                        <p>
+                          Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
+                        </p>
+                      </td>
+                    </tr>
+                    <tr class="expandable-header">
+                      <td>175</td>
+                      <td>Mike Doe</td>
+                      <td>11-7-2014</td>
+                      <td>Denied</td>
+                      <td>Bacon ipsum dolor sit amet salami venison chicken flank fatback doner.</td>
+                    </tr>
+                    <tr class="expandable-body" data-expandable-table='collapsed'>
+                      <td>
+                        <p>
+                          Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
+                        </p>
+                      </td>
+                    </tr>
+                    <tr class="expandable-header">
+                      <td>134</td>
+                      <td>Jim Doe</td>
+                      <td>11-7-2014</td>
+                      <td>Approved</td>
+                      <td>Bacon ipsum dolor sit amet salami venison chicken flank fatback doner.</td>
+                    </tr>
+                    <tr class="expandable-body" data-expandable-table='collapsed'>
+                      <td>
+                        <p>
+                          Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
+                        </p>
+                      </td>
+                    </tr>
+                    <tr class="expandable-header">
+                      <td>494</td>
+                      <td>Victoria Doe</td>
+                      <td>11-7-2014</td>
+                      <td>Pending</td>
+                      <td>Bacon ipsum dolor sit amet salami venison chicken flank fatback doner.</td>
+                    </tr>
+                    <tr class="expandable-body" data-expandable-table='collapsed'>
+                      <td>
+                        <p>
+                          Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
+                        </p>
+                      </td>
+                    </tr>
+                    <tr class="expandable-header">
+                      <td>832</td>
+                      <td>Michael Doe</td>
+                      <td>11-7-2014</td>
+                      <td>Approved</td>
+                      <td>Bacon ipsum dolor sit amet salami venison chicken flank fatback doner.</td>
+                    </tr>
+                    <tr class="expandable-body" data-expandable-table='collapsed'>
+                      <td>
+                        <p>
+                          Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
+                        </p>
+                      </td>
+                    </tr>
+                    <tr class="expandable-header">
+                      <td>982</td>
+                      <td>Rocky Doe</td>
+                      <td>11-7-2014</td>
+                      <td>Denied</td>
+                      <td>Bacon ipsum dolor sit amet salami venison chicken flank fatback doner.</td>
+                    </tr>
+                    <tr class="expandable-body" data-expandable-table='collapsed'>
+                      <td>
+                        <p>
+                          Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
+                        </p>
+                      </td>
+                    </tr>
+                  </tbody>
+                </table>
+              </div>
+              <!-- /.card-body -->
+            </div>
+            <!-- /.card -->
+          </div> 
+        </div>
+        <!--  -->
       </div><!-- /.container-fluid -->
     </section>
     <!-- /.content -->

+ 4 - 4
pages/widgets.html

@@ -2152,7 +2152,7 @@
                   <ul class="contacts-list">
                     <li>
                       <a href="#">
-                        <img class="contacts-list-img" src="../dist/img/user1-128x128.jpg">
+                        <img class="contacts-list-img" src="../dist/img/user1-128x128.jpg" alt="User Avatar">
 
                         <div class="contacts-list-info">
                           <span class="contacts-list-name">
@@ -2247,7 +2247,7 @@
                   <ul class="contacts-list">
                     <li>
                       <a href="#">
-                        <img class="contacts-list-img" src="../dist/img/user1-128x128.jpg">
+                        <img class="contacts-list-img" src="../dist/img/user1-128x128.jpg" alt="User Avatar">
 
                         <div class="contacts-list-info">
                           <span class="contacts-list-name">
@@ -2342,7 +2342,7 @@
                   <ul class="contacts-list">
                     <li>
                       <a href="#">
-                        <img class="contacts-list-img" src="../dist/img/user1-128x128.jpg">
+                        <img class="contacts-list-img" src="../dist/img/user1-128x128.jpg" alt="User Avatar">
 
                         <div class="contacts-list-info">
                           <span class="contacts-list-name">
@@ -2437,7 +2437,7 @@
                   <ul class="contacts-list">
                     <li>
                       <a href="#">
-                        <img class="contacts-list-img" src="../dist/img/user1-128x128.jpg">
+                        <img class="contacts-list-img" src="../dist/img/user1-128x128.jpg" alt="User Avatar">
 
                         <div class="contacts-list-info">
                           <span class="contacts-list-name">