yew_datatable/components/
data_table.rs1use crate::components::pagination::Pagination;
4use crate::components::table_body::TableBody;
5use crate::components::table_header::TableHeader;
6use crate::hooks::use_table::UseTableHandle;
7use yew::prelude::*;
8
9#[derive(Properties, Clone)]
14pub struct DataTableProps<T: Clone + PartialEq + 'static> {
15 pub table: UseTableHandle<T>,
17
18 #[prop_or_default]
20 pub class: Classes,
21
22 #[prop_or_default]
24 pub table_class: Classes,
25
26 #[prop_or(true)]
28 pub show_pagination: bool,
29
30 #[prop_or(true)]
32 pub show_global_filter: bool,
33
34 #[prop_or_else(|| "Search...".to_string())]
36 pub filter_placeholder: String,
37
38 #[prop_or(true)]
40 pub selectable: bool,
41}
42
43impl<T: Clone + PartialEq + 'static> PartialEq for DataTableProps<T> {
45 fn eq(&self, other: &Self) -> bool {
46 self.class == other.class
48 && self.table_class == other.table_class
49 && self.show_pagination == other.show_pagination
50 && self.show_global_filter == other.show_global_filter
51 && self.filter_placeholder == other.filter_placeholder
52 && self.selectable == other.selectable
53 }
54}
55
56#[function_component(DataTable)]
65pub fn data_table<T: Clone + PartialEq + 'static>(props: &DataTableProps<T>) -> Html {
66 let table = props.table.clone();
68
69 let on_global_filter = {
71 let table = table.clone();
72 Callback::from(move |e: InputEvent| {
73 let target = e.target_dyn_into::<web_sys::HtmlInputElement>();
75 if let Some(input) = target {
76 table.set_global_filter(input.value());
78 }
79 })
80 };
81
82 html! {
83 <div class={classes!("datatable-container", props.class.clone())}>
84 {if props.show_global_filter {
85 html! {
86 <div class="datatable-toolbar">
87 <input
88 type="text"
89 class="global-filter"
90 placeholder={props.filter_placeholder.clone()}
91 oninput={on_global_filter}
92 />
93 </div>
94 }
95 } else {
96 html! {}
97 }}
98
99 <table class={classes!("datatable", props.table_class.clone())}>
100 <TableHeader<T> table={table.clone()} />
101 <TableBody<T> table={table.clone()} selectable={props.selectable} />
102 </table>
103
104 {if props.show_pagination {
105 html! {
106 <Pagination<T> table={table.clone()} />
107 }
108 } else {
109 html! {}
110 }}
111 </div>
112 }
113}