From 858c327a8f23fa78fed9b061481d248ba75e34df Mon Sep 17 00:00:00 2001 From: Lukas Jung Date: Mon, 14 Mar 2022 09:59:10 +0100 Subject: [PATCH] implement update and delete for crud example --- examples/7guis/crud/crud.slint | 14 ++++++++++-- examples/7guis/crud/main.rs | 41 +++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/examples/7guis/crud/crud.slint b/examples/7guis/crud/crud.slint index 37c49200c..a4f5c2a44 100644 --- a/examples/7guis/crud/crud.slint +++ b/examples/7guis/crud/crud.slint @@ -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 current-item: list.current-item; + property name; property surname; + property prefix; + + callback prefexEdited(); callback createClicked(); callback updateClicked(); callback deleteClicked(); diff --git a/examples/7guis/crud/main.rs b/examples/7guis/crud/main.rs index 771087c33..38ee17007 100644 --- a/examples/7guis/crud/main.rs +++ b/examples/7guis/crud/main.rs @@ -1,5 +1,6 @@ // Copyright © SixtyFPS GmbH // 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(); }