From 51bfddd40f6d652ba0a90ca41963bbcba1e234b3 Mon Sep 17 00:00:00 2001 From: amass Date: Thu, 28 Nov 2024 23:39:15 +0800 Subject: [PATCH] add checkbox for view. --- WebApplication/VisitorRecordTableModel.cpp | 41 ++++++++++++++++++++-- WebApplication/VisitorRecordTableModel.h | 4 +++ WebApplication/VisitorRecordsPage.cpp | 10 +++--- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/WebApplication/VisitorRecordTableModel.cpp b/WebApplication/VisitorRecordTableModel.cpp index 4622208..dbaeadc 100644 --- a/WebApplication/VisitorRecordTableModel.cpp +++ b/WebApplication/VisitorRecordTableModel.cpp @@ -9,10 +9,45 @@ int VisitorRecordTableModel::rowCount(const Wt::WModelIndex &parent) const { return 50; } -Wt::cpp17::any VisitorRecordTableModel::data(const Wt::WModelIndex &index, Wt::ItemDataRole role) const { - LOG(info) << "VisitorRecordTableModel: " << role.value(); - if (role == Wt::ItemDataRole::Checked && index.column() == 0) { // Assuming first column for checkbox +Wt::WFlags VisitorRecordTableModel::flags(const Wt::WModelIndex &index) const { + if (index.column() == 0) { + return Wt::ItemFlag::UserCheckable | Wt::ItemFlag::Selectable; + } else { + return Wt::ItemFlag::Selectable; + } +} + +Wt::WFlags VisitorRecordTableModel::headerFlags(int section, Wt::Orientation orientation) const { + if (section == 0 && orientation== Wt::Orientation::Horizontal) { + return Wt::HeaderFlag::UserCheckable|Wt::HeaderFlag::XHTMLText; + } else { + return Wt::HeaderFlag::XHTMLText; + } +} + +Wt::cpp17::any VisitorRecordTableModel::headerData(int section, Wt::Orientation orientation, Wt::ItemDataRole role) const { + if ((role == Wt::ItemDataRole::Checked || role == Wt::ItemDataRole::Edit) && + section == 0) { // Assuming first column for checkbox + return true; + } else if ((role == Wt::ItemDataRole::Display || role == Wt::ItemDataRole::Edit) && + section == 0) { // Assuming first column for checkbox + return Wt::cpp17::any{}; + } else { + return Wt::cpp17::any{}; + } +} + +Wt::cpp17::any VisitorRecordTableModel::data(const Wt::WModelIndex &index, Wt::ItemDataRole role) const { + LOG(info) << "VisitorRecordTableModel: " << role.value(); + if ((role == Wt::ItemDataRole::Checked || role == Wt::ItemDataRole::Edit) && + index.column() == 0) { // Assuming first column for checkbox + + return true; + } else if ((role == Wt::ItemDataRole::Checked || role == Wt::ItemDataRole::Edit) && + index.column() == 1) { // Assuming first column for checkbox + + return false; } else { return Wt::cpp17::any{}; } diff --git a/WebApplication/VisitorRecordTableModel.h b/WebApplication/VisitorRecordTableModel.h index 65523f0..5626040 100644 --- a/WebApplication/VisitorRecordTableModel.h +++ b/WebApplication/VisitorRecordTableModel.h @@ -8,5 +8,9 @@ public: Wt::cpp17::any data(const Wt::WModelIndex &index, Wt::ItemDataRole role = Wt::ItemDataRole::Display) const final; int columnCount(const Wt::WModelIndex &parent = Wt::WModelIndex()) const final; int rowCount(const Wt::WModelIndex &parent = Wt::WModelIndex()) const final; + Wt::WFlags flags(const Wt::WModelIndex &index) const final; + Wt::WFlags headerFlags(int section, Wt::Orientation orientation = Wt::Orientation::Horizontal) const final; + Wt::cpp17::any headerData(int section, Wt::Orientation orientation = Wt::Orientation::Horizontal, + Wt::ItemDataRole role = Wt::ItemDataRole::Display) const final; }; #endif // __VISITORRECORDTABLEMODEL_H__ \ No newline at end of file diff --git a/WebApplication/VisitorRecordsPage.cpp b/WebApplication/VisitorRecordsPage.cpp index 11ebaf5..b4da81a 100644 --- a/WebApplication/VisitorRecordsPage.cpp +++ b/WebApplication/VisitorRecordsPage.cpp @@ -1,11 +1,11 @@ #include "VisitorRecordsPage.h" #include "BoostLog.h" #include "Database/Session.h" +#include "VisitorRecordTableModel.h" #include #include #include #include -#include "VisitorRecordTableModel.h" class CheckBoxDelegate : public Wt::WAbstractItemDelegate { public: @@ -36,8 +36,10 @@ VisitorRecordsPage::VisitorRecordsPage(Session &session) { view->setColumnResizeEnabled(true); view->setAlternatingRowColors(true); view->setRowHeight(50); - // view->setSelectable(true); - view->setSelectionMode(Wt::SelectionMode::Extended); + view->setSelectionMode(Wt::SelectionMode::None); + view->setColumnAlignment(0, Wt::AlignmentFlag::Center); + view->setHeaderAlignment(0,Wt::AlignmentFlag::Center); + view->setSortingEnabled(0,false); // view->setEditTriggers(Wt::EditTrigger::None); // view->setItemDelegateForColumn(0, std::make_shared()); // view->setHeaderItemDelegate( std::make_shared()); @@ -51,8 +53,6 @@ VisitorRecordsPage::VisitorRecordsPage(Session &session) { auto model = std::make_shared(); view->setModel(model); - - /* * Configure column widths and matching table width */