11/**
2- * A table with functions for create, read, update, and delete.
2+ * A rest table control extending the base Control class with column reordering functionality and visual indicators.
3+ * The following events are triggered:
4+ * - webexpress.webui.Event.TABLE_SORT_EVENT
5+ * - webexpress.webui.Event.COLUMN_REORDER_EVENT
36 */
4- webexpress . webapp . tableCtrl = class extends webexpress . webui . tableCtrl {
7+ webexpress . webapp . TableCtrl = class extends webexpress . webui . TableCtrl {
58 _restUri = "" ;
6- _searchCtrl = null ;
9+ _titleDiv = $ ( "<h3>" ) . addClass ( "me-auto" ) ;
10+ _progressDiv = $ ( "<div role='status' style='height: 0.5em'>" )
11+ . addClass ( "progress" )
12+ . append ( $ ( "<div class='progress-bar progress-bar-striped progress-bar-animated' style='width: 100%'>" ) ) ;
13+ _filterDiv = $ ( "<div class='col-3'>" ) ;
14+ _filterCtrl = null ;
15+ _statusDiv = $ ( "<span>" ) ;
16+ _paginationDiv = $ ( "<div class='justify-content-end'>" ) ;
717 _paginationCtrl = null ;
818 _filter = null ;
919 _page = null ;
20+ _previewColumns = [
21+ { label : "<span class='placeholder col-6 placeholder-lg'></span>" } ,
22+ { label : "<span class='placeholder col-6 placeholder-lg'></span>" } ,
23+ { label : "<span class='placeholder col-6 placeholder-lg'></span>" } ] ;
24+ _previewBody = [
25+ { cells : [ { text : "<span class='placeholder col-7'></span>" } , { text : "<span class='placeholder col-5'></span>" } , { text : "<span class='placeholder col-6'></span>" } ] } ,
26+ { cells : [ { text : "<span class='placeholder col-6'></span>" } , { text : "<span class='placeholder col-7'></span>" } , { text : "<span class='placeholder col-5'></span>" } ] } ,
27+ { cells : [ { text : "<span class='placeholder col-6'></span>" } , { text : "<span class='placeholder col-6'></span>" } , { text : "<span class='placeholder col-7'></span>" } ] } ] ;
1028
1129 /**
1230 * Constructor
13- * @param settings Options for styling the control:
14- * - id Sets the id of the control.
15- * - css The css class used to design the control.
16- * - placeholder The placeholder text.
17- * - resturi The uri of the rest api interface that collects the data.
31+ * @param {HTMLElement } element - The DOM element associated with the modal control.
1832 */
19- constructor ( settings ) {
20- super ( settings ) ;
33+ constructor ( element ) {
34+ super ( element ) ;
2135
22- this . _restUri = settings . resturi ;
36+ this . _restUri = $ ( element ) . data ( "uri" ) ?? "" ; // Retrieve the URI for loading content
37+
38+ // Cleanup the DOM element
39+ $ ( this . _element )
40+ . removeAttr ( "data-uri" ) ;
2341
24- $ . ajax ( { type : "GET" , url : this . _restUri + "?columns=true" , dataType : 'json' , } ) . then ( function ( response ) {
25- var columns = response . Columns ;
26- this . columns = columns ;
27- } . bind ( this ) ) ;
28-
29- this . _searchCtrl = new webexpress . webui . searchCtrl ( { Id : settings . id + "-search" } ) ;
30- this . _searchCtrl . on ( 'webexpress.webui.change.filter' , function ( key ) { this . _filter = key ; this . receiveData ( ) ; } . bind ( this ) ) ;
31- this . _paginationCtrl = new webexpress . webui . paginationCtrl ( { Id : settings . Id + "-pagination" } ) ;
32- this . _paginationCtrl . on ( 'webexpress.webui.change.page' , function ( page ) { this . _page = page ; this . receiveData ( ) ; } . bind ( this ) ) ;
42+ // Show placeholder while loading content
43+ $ ( this . _element ) . prepend ( $ ( "<div>" ) . addClass ( "wx-table-toolbar" ) . append ( this . _titleDiv , this . _filterDiv ) , this . _progressDiv ) ;
44+ $ ( this . _element ) . append ( $ ( "<div>" ) . addClass ( "wx-table-statusbar" ) . append ( this . _statusDiv , this . _paginationDiv ) ) ;
45+
46+ this . _columns = this . _previewColumns ;
47+ this . _rows = this . _previewBody ;
48+
49+ this . _table . addClass ( "placeholder-glow" ) ;
50+
51+ this . render ( ) ;
52+
53+ this . _filterCtrl = new webexpress . webui . SearchCtrl ( this . _filterDiv [ 0 ] ) ;
54+ $ ( document ) . on ( webexpress . webui . Event . CHANGE_FILTER_EVENT , ( event , data ) => {
55+ this . _filter = data . value ;
56+ this . _receiveData ( ) ;
57+ } ) ;
58+
59+ this . _paginationCtrl = new webexpress . webui . PaginationCtrl ( this . _paginationDiv [ 0 ] ) ;
60+ //this._paginationCtrl = webexpress.webui.Controller.getInstanceByElement(this._paginationDiv);
61+ $ ( document ) . on ( webexpress . webui . Event . CHANGE_PAGE_EVENT , ( event , data ) => {
62+ this . _page = data . page ;
63+ this . _receiveData ( ) ;
64+ } ) ;
65+
66+ this . _receiveData ( ) ;
3367 }
3468
3569 /**
3670 * Retrieve data from rest api.
3771 */
38- receiveData ( ) {
39- if ( this . _filter === undefined || this . _filter == null ) { this . _filter = "" ; }
40- if ( this . _page === undefined || this . _page == null ) { this . _page = 0 ; }
41- $ . ajax ( { type : "GET" , url : this . _restUri + "?search=" + this . _filter + "&page=" + this . _page , dataType : 'json' , } ) . then ( function ( response ) {
42- var data = response . data ;
43- this . clear ( ) ;
44- this . addRange ( data ) ;
45- this . trigger ( 'webexpress.webui.receive.complete' ) ;
46- var pagination = response . pagination ;
47- this . _paginationCtrl . page ( pagination . pagenumber , pagination . totalpage ) ;
48- } . bind ( this ) ) ;
72+ _receiveData ( ) {
73+ this . _progressDiv . css ( "visibility" , "visible" ) ;
74+
75+ $ . get ( `${ this . _restUri } ?filter=${ this . _filter } &page=${ this . _page } ` )
76+ . done ( ( response ) => {
77+
78+ this . _columns = response . columns ;
79+ this . _rows = response . rows ;
80+ this . _titleDiv . text ( response . title ) ;
81+ this . _statusDiv . text ( `${ response . page * response . pageitems } - ${ ( response . page * response . pagecount ) + response . pagecount - 1 } / ${ response . total } ` ) ;
82+
83+ // Trigger event when data has successfully arrived
84+ $ ( this . _element ) . trigger ( webexpress . webui . Event . DATA_ARRIVED_EVENT , {
85+ id : $ ( this . _element ) . attr ( "id" ) ,
86+ response : response
87+ } ) ;
88+ this . _paginationCtrl . pagecount = response . pagecount ;
89+ this . _paginationCtrl . page = response . page ;
90+
91+ this . _table . removeClass ( "placeholder-glow" ) ;
92+
93+ this . _hasOptions = this . _rows . some ( row => row . options ?. length > 0 ) ;
94+
95+ if ( this . _options ?. length > 0 ) {
96+ this . _hasOptions = true ;
97+ }
98+
99+ this . render ( ) ;
100+
101+ this . _progressDiv . css ( "visibility" , "hidden" ) ;
102+ } )
103+ . fail ( ( error ) => {
104+ console . error ( "The request could not be completed successfully:" , error ) ;
105+ } ) ;
49106 }
107+ }
50108
51- /**
52- * Returns the control.
53- */
54- get getCtrl ( ) {
55- let div = $ ( "<div/>" )
56- div . append ( this . _searchCtrl . getCtrl ) ;
57- div . append ( this . _table ) ;
58- div . append ( this . _paginationCtrl . getCtrl ) ;
59- return div ;
60- }
61- }
109+ // Register the class in the controller
110+ webexpress . webui . Controller . registerClass ( "wx-webapp-table" , webexpress . webapp . TableCtrl ) ;
0 commit comments