implement update and delete for crud example

This commit is contained in:
Lukas Jung 2022-03-14 09:59:10 +01:00 committed by Lukas Jung
parent 2417869aa4
commit 858c327a8f
2 changed files with 47 additions and 8 deletions

View file

@ -7,9 +7,12 @@ MainWindow := Window {
GridBox {
Text { text: "Filter prefix:"; }
LineEdit {}
LineEdit {
text <=> root.prefix;
edited => { root.prefexEdited() }
}
StandardListView {
list := StandardListView {
row: 1;
rowspan: 3;
colspan: 2;
@ -30,19 +33,26 @@ MainWindow := Window {
}
Button {
text: "Update";
enabled: list.current-item != -1;
clicked => { root.updateClicked() }
}
Button {
text: "Delete";
enabled: list.current-item != -1;
clicked => { root.deleteClicked() }
}
}
}
property <[StandardListViewItem]> names-list;
property <int> current-item: list.current-item;
property <string> name;
property <string> surname;
property <string> prefix;
callback prefexEdited();
callback createClicked();
callback updateClicked();
callback deleteClicked();

View file

@ -1,5 +1,6 @@
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
use slint::Model;
use std::rc::Rc;
slint::slint!(import { MainWindow } from "crud.slint";);
@ -14,11 +15,39 @@ pub fn main() {
]));
main_window.set_names_list(model.clone().into());
let main_window_weak = main_window.as_weak();
main_window.on_createClicked(move || {
let main_window = main_window_weak.unwrap();
let new_entry = main_window.get_surname() + ", " + main_window.get_name().as_str();
model.push(new_entry.into());
});
{
let main_window_weak = main_window.as_weak();
let model = model.clone();
main_window.on_createClicked(move || {
let main_window = main_window_weak.unwrap();
let new_entry = main_window.get_surname() + ", " + main_window.get_name().as_str();
model.push(new_entry.into());
});
}
{
let main_window_weak = main_window.as_weak();
let model = model.clone();
main_window.on_updateClicked(move || {
let main_window = main_window_weak.unwrap();
let index = main_window.get_current_item() as usize;
let entry = main_window.get_surname() + ", " + main_window.get_name().as_str();
model.set_row_data(index, entry.into());
});
}
{
let main_window_weak = main_window.as_weak();
let model = model.clone();
main_window.on_deleteClicked(move || {
let main_window = main_window_weak.unwrap();
let index = main_window.get_current_item() as usize;
model.remove(index);
});
}
main_window.run();
}