1
+ // Simple DataTables initialization for sortable/searchable tables
2
+ ; ( function ( ) {
3
+ 'use strict'
4
+
5
+ // Initialize Simple DataTables on tables with specific class
6
+ function initializeDataTables ( ) {
7
+ const tables = document . querySelectorAll ( 'table.datatable' )
8
+
9
+ if ( tables . length === 0 ) {
10
+ console . log ( 'No tables with .datatable class found' )
11
+ return
12
+ }
13
+
14
+ // Check if Simple DataTables is available globally
15
+ if ( typeof simpleDatatables === 'undefined' && typeof window . simpleDatatables === 'undefined' ) {
16
+ console . log ( 'Simple DataTables not yet available, waiting...' )
17
+ setTimeout ( initializeDataTables , 100 )
18
+ return
19
+ }
20
+
21
+ // Get the DataTable constructor
22
+ const DataTable = simpleDatatables ?. DataTable || window . simpleDatatables ?. DataTable || window . DataTable
23
+
24
+ if ( ! DataTable ) {
25
+ console . error ( 'DataTable constructor not found!' )
26
+ return
27
+ }
28
+
29
+ console . log ( `Found ${ tables . length } table(s) with .datatable class` )
30
+
31
+ tables . forEach ( ( table , index ) => {
32
+ try {
33
+ console . log ( `Initializing Simple DataTable ${ index + 1 } of ${ tables . length } ` )
34
+
35
+ // Ensure table has a thead for proper functionality
36
+ if ( ! table . querySelector ( 'thead' ) ) {
37
+ console . warn ( `Table ${ index + 1 } does not have a thead element. Simple DataTables may not work properly.` )
38
+ }
39
+
40
+ // Check for CSS classes that control column sorting
41
+ let columnConfig = undefined
42
+
43
+ // Check for specific no-sort classes (e.g., .no-sort-col-3)
44
+ const classList = Array . from ( table . classList )
45
+ const noSortClasses = classList . filter ( cls => cls . startsWith ( 'no-sort-col-' ) )
46
+
47
+ if ( noSortClasses . length > 0 ) {
48
+ const totalColumns = table . querySelector ( 'thead tr' ) . children . length
49
+ const nonSortableColumns = noSortClasses . map ( cls => {
50
+ const match = cls . match ( / n o - s o r t - c o l - ( \d + ) / )
51
+ return match ? parseInt ( match [ 1 ] ) : null
52
+ } ) . filter ( col => col !== null )
53
+
54
+ console . log ( `Non-sortable columns:` , nonSortableColumns )
55
+ console . log ( `Total columns:` , totalColumns )
56
+
57
+ columnConfig = [ ]
58
+ for ( let i = 0 ; i < totalColumns ; i ++ ) {
59
+ const isSortable = ! nonSortableColumns . includes ( i )
60
+ columnConfig . push ( {
61
+ select : i ,
62
+ sortable : isSortable
63
+ } )
64
+ console . log ( `Column ${ i } : sortable = ${ isSortable } ` )
65
+ }
66
+ }
67
+
68
+ // Alternative: Check for .no-sort-description class (makes column 3 non-sortable for pipeline tables)
69
+ else if ( table . classList . contains ( 'no-sort-description' ) ) {
70
+ const totalColumns = table . querySelector ( 'thead tr' ) . children . length
71
+ columnConfig = [ ]
72
+ for ( let i = 0 ; i < totalColumns ; i ++ ) {
73
+ // For pipeline tables, column 3 is typically the description/value column
74
+ const isSortable = i !== 3
75
+ columnConfig . push ( {
76
+ select : i ,
77
+ sortable : isSortable
78
+ } )
79
+ console . log ( `Column ${ i } : sortable = ${ isSortable } (description column rule)` )
80
+ }
81
+ }
82
+
83
+ new DataTable ( table , {
84
+ searchable : true ,
85
+ sortable : true ,
86
+ paging : false ,
87
+ perPage : 25 ,
88
+ perPageSelect : [ 10 , 25 , 50 , 100 ] ,
89
+ labels : {
90
+ placeholder : "Filter records..." ,
91
+ perPage : "Show {select} entries per page" ,
92
+ noRows : "No data available in table" ,
93
+ info : "Showing {start} to {end} of {rows} entries"
94
+ } ,
95
+ columns : columnConfig
96
+ } )
97
+
98
+ console . log ( `Simple DataTable initialized successfully for table ${ index + 1 } ` )
99
+ } catch ( error ) {
100
+ console . error ( `Error initializing Simple DataTable for table ${ index + 1 } :` , error )
101
+ }
102
+ } )
103
+ }
104
+
105
+ // Initialize when DOM is ready
106
+ if ( document . readyState === 'loading' ) {
107
+ document . addEventListener ( 'DOMContentLoaded' , initializeDataTables )
108
+ } else {
109
+ initializeDataTables ( )
110
+ }
111
+
112
+ console . log ( 'Simple DataTables module loaded' )
113
+ } ) ( )
0 commit comments