11import * as List from "list.js" ;
22
3- // function set_width() {
4- // w = $(this)
5- // .children("tr:first")
6- // .children("th, td")
7- // .map(function () {
8- // return $(this)
9- // .css("width");
10- // });
11- // console.log(w);
12-
13- // $(tbody)
14- // .children("tr:first")
15- // .children("th, td")
16- // .each(function (i) {
17- // console.log(i, w[i]);
18- // $(this)
19- // .css("width", w[i]);
20- // });
21- // }
22-
23- // FIXME sort only works on first table
24-
25- var table_cnt = 0 ;
3+ var dummy = new List ( ) ;
4+
5+ function text_sort ( a , b , options ) {
6+ return dummy . utils . naturalSort . caseInsensitive ( $ ( $ . parseHTML ( a . values ( ) [ options . valueName ] ) )
7+ . text ( )
8+ . trim ( )
9+ . replaceAll ( / \w + / g, ' ' ) , $ ( $ . parseHTML ( b . values ( ) [ options . valueName ] ) )
10+ . text ( )
11+ . trim ( )
12+ . replaceAll ( / \w + / g, ' ' ) ) ;
13+ }
2614
2715$ ( document )
2816 . ready ( function ( ) {
2917 $ ( "table.tablesorter" )
3018 . each ( function ( ) {
31- var header_row = $ ( this )
19+ var table = $ ( this ) ;
20+
21+ var header_row = $ ( table )
3222 . find ( "thead > tr:first" ) ;
3323
3424 var fields = $ ( header_row )
@@ -63,7 +53,7 @@ $(document)
6353 </button>
6454 </div>` ) ;
6555
66- $ ( this )
56+ $ ( table )
6757 . before ( searcher ) ;
6858
6959 var search_field = $ ( searcher )
@@ -72,49 +62,35 @@ $(document)
7262 var reset_search = $ ( searcher )
7363 . children ( "button.search-reset" ) ;
7464
75- var instance = [ ] ;
65+ var list_instance = [ ] ;
66+ var internal_table = [ ] ;
7667
77- var first_table ;
78- var last_table ;
79- $ ( this )
68+ $ ( table )
8069 . children ( "tbody" )
8170 . addClass ( "list" )
8271 . each ( function ( ) {
83- var parent ;
84- if ( first_table === undefined ) {
85- first_table = $ ( this )
86- . parent ( ) ;
87- last_table = first_table ;
88- parent = first_table [ 0 ] ;
89- } else {
90- var new_table = $ ( first_table )
91- . clone ( )
92- . empty ( )
93- . removeClass ( "tablesorter" ) ;
94- $ ( last_table )
95- . after ( new_table ) ;
96- var thead = $ ( this )
97- . prev ( "thead" )
98- . detach ( ) ;
99- var tbody = $ ( this )
100- . detach ( ) ;
101- new_table . append ( thead , tbody ) ;
102- parent = $ ( new_table ) [ 0 ] ;
103- last_table = new_table ;
104- }
72+ var thead = $ ( this )
73+ . siblings ( "thead:first" )
74+ . clone ( ) ;
10575
106- $ ( parent )
107- . addClass ( "tablesorter-table-" + table_cnt ) ;
76+ var tbody = $ ( this )
77+ . clone ( ) ;
10878
109- instance . push (
110- new List ( parent , { valueNames : fields } ) ) ;
111- } ) ;
79+ var parent = $ ( table )
80+ . clone ( )
81+ . empty ( )
82+ . removeClass ( "tablesorter" )
83+ . append ( thead , tbody ) ;
11284
113- table_cnt ++ ;
85+ internal_table . push ( parent ) ;
86+
87+ list_instance . push (
88+ new List ( parent [ 0 ] , { valueNames : fields } ) ) ;
89+ } ) ;
11490
11591 reset_search . on ( "click" , function ( ) {
11692 search_field . val ( "" ) ;
117- $ . each ( instance , ( i , e ) => {
93+ $ . each ( list_instance , ( i , e ) => {
11894 e . search ( ) ;
11995 } ) ;
12096 } ) ;
@@ -123,14 +99,49 @@ $(document)
12399 if ( event . key == "Escape" ) {
124100 reset_search . trigger ( "click" ) ;
125101 } else {
126- $ . each ( instance , ( i , e ) => {
102+ $ . each ( list_instance , ( i , e ) => {
127103 e . search ( $ ( this )
128104 . val ( ) ) ;
129105 } ) ;
130106 }
131107 } ) ;
132108
133- $ . each ( instance , ( i , e ) => {
109+ $ ( table )
110+ . find ( ".sort" )
111+ . on ( "click" , function ( ) {
112+ var order = $ ( this )
113+ . hasClass ( "asc" ) ? "desc" : "asc" ;
114+ $ . each ( list_instance , ( i , e ) => {
115+ e . sort ( $ ( this )
116+ . attr ( "data-sort" ) , { order : order , sortFunction : text_sort } ) ;
117+ } ) ;
118+ } ) ;
119+
120+ $ . each ( list_instance , ( i , e ) => {
121+ e . on ( "sortComplete" , function ( ) {
122+ $ ( table )
123+ . children ( "tbody" )
124+ . eq ( i )
125+ . replaceWith ( internal_table [ i ]
126+ . children ( "tbody" )
127+ . clone ( ) ) ;
128+
129+ if ( i == list_instance . length - 1 ) {
130+ $ ( table )
131+ . find ( "thead:first tr" )
132+ . children ( "th, td" )
133+ . each ( ( idx , el ) => {
134+ var cl = internal_table [ i ] . find ( "thead:first tr" )
135+ . children ( "th, td" )
136+ . eq ( idx )
137+ . attr ( "class" ) ;
138+ $ ( el )
139+ . attr ( "class" , cl ) ;
140+
141+ } ) ;
142+ }
143+ } ) ;
144+
134145 e . on ( "searchComplete" , function ( ) {
135146 var last_show_with_children = { } ;
136147 e . items . forEach ( ( item ) => {
@@ -158,7 +169,14 @@ $(document)
158169 item . show ( ) ;
159170 }
160171 } ) ;
172+
161173 e . update ( ) ;
174+ $ ( table )
175+ . children ( "tbody" )
176+ . eq ( i )
177+ . replaceWith ( internal_table [ i ]
178+ . children ( "tbody" )
179+ . clone ( ) ) ;
162180 } ) ;
163181 } ) ;
164182 }
0 commit comments