List
Selectable rows in sections, with search, keyboard navigation, loading and empty states and load-more paging.
Sections and search
ui.vertical(|ui| {
ui.set_width(420.0);
let query = state.companies.query.to_lowercase();
// The rows matching the search, grouped by industry.
let mut groups: Vec<(&str, Vec<(&str, f32)>)> = Vec::new();
for &(industry, name, change) in COMPANIES {
if !name.to_lowercase().contains(&query) {
continue;
}
match groups.iter_mut().find(|(group, _)| *group == industry) {
Some((_, rows)) => rows.push((name, change)),
None => groups.push((industry, vec![(name, change)])),
}
}
let sections = groups
.iter()
.map(|(industry, rows)| {
ListSection::new(*industry, rows.len())
.footer(format!("{} companies", rows.len()))
})
.collect();
let theme = tokens(ui.ctx());
let response = egui::Frame::new()
.stroke(egui::Stroke::new(1.0, theme.border))
.corner_radius(theme.shape.radius)
.show(ui, |ui| {
List::sectioned(&mut state.companies, sections)
.id_salt("companies")
.searchable(true)
.search_placeholder("Search companies...")
.max_height(300.0)
.label(|index| groups[index.section].1[index.row].0.to_owned())
.accessibility_label("Companies")
.show(ui, |ui, row| {
let (name, change) = groups[row.index.section].1[row.index.row];
ui.label(name);
ui.with_layout(
egui::Layout::right_to_left(egui::Align::Center),
|ui| {
let color = if change > 0.0 {
theme.hues.green
} else if change < 0.0 {
theme.hues.red
} else {
theme.muted_foreground
};
ui.label(
egui::RichText::new(format!("{change:+.1}%")).color(color),
);
},
);
})
})
.inner;
let chosen = state
.companies
.selected
.and_then(|index| groups.get(index.section)?.1.get(index.row).map(|row| row.0));
ui.label(match (response.activated, chosen) {
(Some(_), Some(name)) => format!("Opened {name}"),
(None, Some(name)) => format!("Selected {name}"),
_ => "Nothing selected".to_owned(),
});
});
Loading
ui.vertical(|ui| {
ui.set_width(320.0);
ui.add(Toggle::new(&mut state.loading, "Loading"));
List::new(&mut state.loading_list, 3)
.id_salt("loading")
.loading(state.loading)
.show(ui, |ui, row| {
ui.label(format!("Loaded row {}", row.index.row + 1));
});
});
Empty
ui.vertical(|ui| {
ui.set_width(320.0);
List::new(&mut state.empty, 0)
.id_salt("empty")
.show(ui, |_, _| {});
});
Load more
ui.vertical(|ui| {
ui.set_width(320.0);
let response = List::new(&mut state.feed, state.loaded)
.id_salt("feed")
.max_height(200.0)
.has_more(state.loaded < 400)
.show(ui, |ui, row| {
ui.label(format!("Post {}", row.index.row + 1));
});
if response.load_more {
// Fetch the next page; here, twenty more rows at once.
state.loaded += 20;
}
ui.label(format!("{} of 400 loaded", state.loaded));
});
The story above is gallery/src/stories/list.rs, running in your browser. Every snippet on this page is cut from it.