@@ -228,7 +228,6 @@ fn convert_string(input: &str) -> String {
228228 result
229229}
230230
231-
232231#[ function_component( Home ) ]
233232pub fn home ( ) -> Html {
234233 // Mock data holder
@@ -239,15 +238,14 @@ pub fn home() -> Html {
239238 let search_term = use_state ( || None :: < String > ) ;
240239 let search = ( * search_term) . as_ref ( ) . cloned ( ) ;
241240
242- let page=use_state ( ||0usize ) ;
243- let current_page=( * page) . clone ( ) ;
241+ // Pagination state
242+ let page = use_state ( || 0usize ) ;
243+ let current_page = ( * page) . clone ( ) ;
244244
245- // Sum data
245+ // Selected indexes for summing
246246 let selected_indexes = use_set ( HashSet :: < usize > :: new ( ) ) ;
247-
248247 let sum = selected_indexes. current ( ) . len ( ) ;
249248
250-
251249 // Column definition
252250 let columns = vec ! [
253251 ColumnBuilder :: new( "select" ) . orderable( true ) . short_name( "Select" ) . data_property( "select" ) . header_class( "user-select-none" ) . build( ) ,
@@ -256,7 +254,6 @@ pub fn home() -> Html {
256254 ColumnBuilder :: new( "value" ) . orderable( true ) . short_name( "Value" ) . data_property( "value" ) . header_class( "user-select-none" ) . build( ) ,
257255 ] ;
258256
259-
260257 // Table options
261258 let options = Options {
262259 unordered_class : Some ( "fa-sort" . to_string ( ) ) ,
@@ -265,8 +262,6 @@ pub fn home() -> Html {
265262 orderable_classes : vec ! [ "mx-1" . to_string( ) , "fa-solid" . to_string( ) ] ,
266263 } ;
267264
268-
269-
270265 // Handle sum
271266 let callback_sum = {
272267 let selected_indexes = selected_indexes. clone ( ) ;
@@ -277,24 +272,51 @@ pub fn home() -> Html {
277272 } )
278273 } ;
279274
280-
281-
282-
283- // Fill the table data structure with actual data
284- let mut table_data = Vec :: new ( ) ;
285- for ( index, ( id, name, value) ) in mock_data. data . iter ( ) . enumerate ( ) {
286- table_data. push ( TableLine {
275+ // Filter the full dataset based on the search term
276+ let filtered_data: Vec < TableLine > = mock_data. data
277+ . iter ( )
278+ . enumerate ( )
279+ . filter ( |( _, ( _, name, _) ) | {
280+ match search {
281+ Some ( ref term) => name. to_lowercase ( ) . contains ( & term. to_lowercase ( ) ) ,
282+ None => true , // If no search term, include all data
283+ }
284+ } )
285+ . map ( |( index, ( id, name, value) ) | TableLine {
287286 original_index : index,
288287 id : * id,
289288 name : name. clone ( ) ,
290289 value : * value,
291290 checked : selected_indexes. current ( ) . contains ( & index) ,
292291 sum_callback : callback_sum. clone ( ) ,
293292 } )
294- }
293+ . collect ( ) ;
294+
295+ // Pagination logic
296+ let limit = 10 ; // Number of items per page
297+
298+ // Reset current_page to 0 if filtered_data is empty
299+ let current_page = if filtered_data. is_empty ( ) {
300+ 0 // Reset to the first page if no data is found
301+ } else {
302+ current_page. min ( ( filtered_data. len ( ) - 1 ) / limit) // Ensure current_page is within bounds
303+ } ;
304+
305+ let start_index = current_page * limit;
306+ let end_index = ( start_index + limit) . min ( filtered_data. len ( ) ) ; // Ensure end_index does not exceed filtered_data.len()
307+
308+ let paginated_data = if filtered_data. is_empty ( ) {
309+ Vec :: new ( ) // Return an empty vector if no data is found
310+ } else {
311+ filtered_data[ start_index..end_index] . to_vec ( ) // Slice the data
312+ } ;
313+
314+ // Ensure total is at least 1 for the Pagination component
315+ let total = filtered_data. len ( ) . max ( 1 ) ;
295316
296317 // Handle search input
297318 let oninput_search = {
319+ let search_term = search_term. clone ( ) ;
298320 Callback :: from ( move |e : InputEvent | {
299321 let input: HtmlInputElement = e. target_unchecked_into ( ) ;
300322 if input. value ( ) . is_empty ( ) {
@@ -305,6 +327,7 @@ pub fn home() -> Html {
305327 } )
306328 } ;
307329
330+ // Pagination options
308331 let pagination_options = yew_custom_components:: pagination:: Options :: default ( )
309332 . show_prev_next ( true )
310333 . show_first_last ( true )
@@ -317,8 +340,8 @@ pub fn home() -> Html {
317340 // Handle changing page
318341 let handle_page = {
319342 let page = page. clone ( ) ;
320- Callback :: from ( move |id : usize | {
321- page. set ( id ) ;
343+ Callback :: from ( move |new_page : usize | {
344+ page. set ( new_page ) ;
322345 } )
323346 } ;
324347
@@ -331,8 +354,23 @@ pub fn home() -> Html {
331354 </span>
332355 <input class="form-control" type ="text" id="search" placeholder="Search" oninput={ oninput_search} />
333356 </div>
334- <Table <TableLine > options={ options. clone( ) } limit={ Some ( 10 ) } page={ current_page} search={ search. clone( ) } classes={ classes!( "table" , "table-hover" ) } columns={ columns. clone( ) } data={ table_data. clone( ) } orderable={ true } />
335- <Pagination total={ table_data. len( ) } limit={ 10 } max_pages={ 6 } options={ pagination_options} on_page={ Some ( handle_page) } />
357+ <Table <TableLine >
358+ options={ options. clone( ) }
359+ limit={ Some ( limit) }
360+ page={ current_page}
361+ search={ search. clone( ) }
362+ classes={ classes!( "table" , "table-hover" ) }
363+ columns={ columns. clone( ) }
364+ data={ paginated_data}
365+ orderable={ true }
366+ />
367+ <Pagination
368+ total={ total} // Ensure total is at least 1
369+ limit={ limit}
370+ max_pages={ 6 }
371+ options={ pagination_options}
372+ on_page={ Some ( handle_page) }
373+ />
336374 <h5>{ "Number selected" } <span class="badge text-bg-secondary" >{ sum} </span></h5>
337375 <App selected_indexes={ ( * selected_indexes. current( ) ) . clone( ) } />
338376 </>
0 commit comments