lucasmerlin / hello_egui

A collection of useful crates for egui
https://lucasmerlin.github.io/hello_egui/
MIT License
272 stars 24 forks source link

How to get the item index in show_vec? #10

Closed imxood closed 1 year ago

imxood commented 1 year ago

How to get the item index in show_vec?

lucasmerlin commented 1 year ago

It was not possible before. I've now added an index property to the state struct. Can you try and see if this works for you?

imxood commented 1 year ago

Thanks~ It's good, but it is the show's sorted item index, and also should add a index(raw data item)

when I want to delete the choosed item, I need the index(raw data item).

image

egui-pnp测试

lucasmerlin commented 1 year ago

This is working for me:

use eframe::egui;
use egui::CentralPanel;
use egui_dnd::dnd;

pub fn main() -> eframe::Result<()> {
    let mut items = vec!["alfred", "bernhard", "christian"];

    let mut delete_index = None;

    eframe::run_simple_native(
        "DnD Simple Example",
        Default::default(),
        move |ctx, _frame| {
            CentralPanel::default().show(ctx, |ui| {
                dnd(ui, "dnd_example").show_vec(&mut items, |ui, item, handle, state| {
                    handle.ui(ui, |ui| {
                        if state.dragged {
                            ui.label("dragging");
                        } else {
                            ui.label("drag");
                        }
                    });
                    ui.label(*item);
                    if ui.button("delete").clicked() {
                        delete_index = Some(state.index);
                    }
                });
            });

            if let Some(index) = delete_index {
                items.remove(index);
                delete_index = None;
            }
        },
    )
}

With raw data item, do you mean the items original index, before the sort was changed? If you need that, you should add it as a field to your item type. I don't think it makes sense for egui_dnd to keep track of that.

imxood commented 1 year ago

It is good, thanks very much~