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.table == other.table
48 && self.class == other.class
50 && self.table_class == other.table_class
51 && self.show_pagination == other.show_pagination
52 && self.show_global_filter == other.show_global_filter
53 && self.filter_placeholder == other.filter_placeholder
54 && self.selectable == other.selectable
55 }
56}
57
58#[function_component(DataTable)]
67pub fn data_table<T: Clone + PartialEq + 'static>(props: &DataTableProps<T>) -> Html {
68 let table = props.table.clone();
70
71 let on_global_filter = {
73 let table = table.clone();
74 Callback::from(move |e: InputEvent| {
75 let target = e.target_dyn_into::<web_sys::HtmlInputElement>();
77 if let Some(input) = target {
78 table.set_global_filter(input.value());
80 }
81 })
82 };
83
84 html! {
85 <div class={classes!("datatable-container", props.class.clone())}>
86 {if props.show_global_filter {
87 html! {
88 <div class="datatable-toolbar">
89 <input
90 type="text"
91 class="global-filter"
92 placeholder={props.filter_placeholder.clone()}
93 oninput={on_global_filter}
94 />
95 </div>
96 }
97 } else {
98 html! {}
99 }}
100
101 <table class={classes!("datatable", props.table_class.clone())}>
102 <TableHeader<T> table={table.clone()} />
103 <TableBody<T> table={table.clone()} selectable={props.selectable} />
104 </table>
105
106 {if props.show_pagination {
107 html! {
108 <Pagination<T> table={table.clone()} />
109 }
110 } else {
111 html! {}
112 }}
113 </div>
114 }
115}