use_pagination

Function use_pagination 

Source
pub fn use_pagination<T, L, Q, M>(
    state: Store<PaginationState>,
    loader: L,
    query: impl Into<Signal<Q>>,
    item_count_per_page: impl Into<Signal<usize>>,
    options: UsePaginationOptions,
) -> ItemWindow<T>
where T: Send + Sync + 'static, L: InternalLoader<M, Item = T, Query = Q> + 'static, L::Error: Send + Sync, Q: Send + Sync + 'static,
Expand description

Hook for the pagination logic.

This handles loading items on-demand from the data source and caching them.

It returns an ItemWindow that is in effect a signal of the items to display.

§Usage

let state = PaginationState::new_store();

pub struct ExampleItem {
    num: usize,
}

// Implement Loader for example data
pub struct ExampleLoader;

impl MemoryLoader for ExampleLoader {
    type Item = ExampleItem;
    type Query = ();

    fn load_items(&self, range: Range<usize>, query: &Self::Query) -> Vec<Self::Item> {
        // Generate example data
        range.map(|i| ExampleItem { num: i }).collect()
    }

    fn item_count(&self, query: &Self::Query) -> usize {
        // Let's say we have 1000 items in total
        1000
    }
}

// See PaginatedFor for how to build a pagination component with the returned window from this hook.
let window = use_pagination(
    state,
    ExampleLoader,
    (),
    20, // items per page
    UsePaginationOptions::default(),
);

// Use this to control the pagination
let pagination_controls = use_pagination_controls(state, UsePaginationControlsOptions::default());

§Paramters

  • state: The pagination state. Used to communicate between the pagination controls and this component.
  • loader: The loader used to load items from the data source.
  • item_count_per_page: The number of items to display per page.
  • options: Additional options for the pagination logic.