From f3d211a109f448dc42a2a8a75ee532a4da46be96 Mon Sep 17 00:00:00 2001 From: amass Date: Fri, 17 Jan 2025 02:55:03 +0800 Subject: [PATCH] add search for record. --- WebApplication/Application.cpp | 1 + WebApplication/TaskPage.cpp | 4 +-- WebApplication/VisitorRecordsPage.cpp | 16 ++++++--- WebApplication/VisitorRecordsPage.h | 4 +-- WebApplication/WebRTCClientPage.cpp | 1 - .../model/VisitorRecordTableModel.cpp | 35 +++++++++++-------- .../model/VisitorRecordTableModel.h | 1 + resources/webrtc.xml | 17 +++++++++ 8 files changed, 54 insertions(+), 25 deletions(-) diff --git a/WebApplication/Application.cpp b/WebApplication/Application.cpp index fdbb1ec..f894945 100644 --- a/WebApplication/Application.cpp +++ b/WebApplication/Application.cpp @@ -32,6 +32,7 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) messageResourceBundle().use(appRoot() + "wt"); messageResourceBundle().use(appRoot() + "auth_strings"); messageResourceBundle().use(appRoot() + "auth_css_theme"); + messageResourceBundle().use(appRoot() + "webrtc"); useStyleSheet("/resources/app.css"); LOG(info) << "app root: " << appRoot(); m_session = Database::session(); diff --git a/WebApplication/TaskPage.cpp b/WebApplication/TaskPage.cpp index d7b919b..21f4a75 100644 --- a/WebApplication/TaskPage.cpp +++ b/WebApplication/TaskPage.cpp @@ -13,11 +13,9 @@ TaskPage::TaskPage() : Wt::WTemplate(tr("Wt.Task.Home")) { using namespace Wt; - WApplication *app = WApplication::instance(); - app->messageResourceBundle().use(app->appRoot() + "webrtc"); addStyleClass("bulma-is-flex-grow-1 bulma-is-flex bulma-is-flex-direction-column"); - auto addButton = bindNew("add-new", "创建"); + auto addButton = bindNew("add-new", "创建"); addButton->clicked().connect(this, [this]() { showAddTaskDialog(-1); }); update(); } diff --git a/WebApplication/VisitorRecordsPage.cpp b/WebApplication/VisitorRecordsPage.cpp index 9810084..b1848d9 100644 --- a/WebApplication/VisitorRecordsPage.cpp +++ b/WebApplication/VisitorRecordsPage.cpp @@ -6,17 +6,23 @@ #include #include #include +#include #include #include #include -VisitorRecordsPage::VisitorRecordsPage(Session &session) : m_model{std::make_shared(session)} { - addNew("访客数据统计"); - addNew(); - auto button = addNew("删除"); +VisitorRecordsPage::VisitorRecordsPage(Session &session) + : Wt::WTemplate(tr("Wt.VisitorRecords.Home")), m_model{std::make_shared(session)} { + + auto button = bindNew("remove-button", "删除"); button->clicked().connect(this, &VisitorRecordsPage::onDeleteButtonClicked); - auto view = addNew(); + auto searchInput = bindNew("search-input"); + searchInput->setPlaceholderText("请输入文章标题"); + auto searchButton = bindNew("search-button", "搜索"); + searchButton->clicked().connect(this, [this, searchInput]() { m_model->filter(searchInput->text().toUTF8()); }); + + auto view = bindNew("records"); view->setColumnWidth(0, Wt::WLength::Auto); view->setColumnWidth(1, Wt::WLength::Auto); view->setHeaderHeight(30); // 设置表头的高度 diff --git a/WebApplication/VisitorRecordsPage.h b/WebApplication/VisitorRecordsPage.h index 010bc72..5225fbe 100644 --- a/WebApplication/VisitorRecordsPage.h +++ b/WebApplication/VisitorRecordsPage.h @@ -1,14 +1,14 @@ #ifndef __VISITORRECORDSPAGE_H__ #define __VISITORRECORDSPAGE_H__ -#include +#include class Session; class VisitorRecordTableModel; // https://infima.dev/ -class VisitorRecordsPage : public Wt::WContainerWidget { +class VisitorRecordsPage : public Wt::WTemplate { public: VisitorRecordsPage(Session &session); diff --git a/WebApplication/WebRTCClientPage.cpp b/WebApplication/WebRTCClientPage.cpp index 94fbb8f..442f44b 100644 --- a/WebApplication/WebRTCClientPage.cpp +++ b/WebApplication/WebRTCClientPage.cpp @@ -15,7 +15,6 @@ WebRTCClientPage::WebRTCClientPage() : Wt::WTemplate(tr("Wt.WebRTC.Home")) { using namespace Wt; WApplication *app = WApplication::instance(); - app->messageResourceBundle().use(app->appRoot() + "webrtc"); LOAD_JAVASCRIPT(app, "js/WebRTCClient.js", "WebRTCClient", wtjs1); addStyleClass("bulma-is-flex-grow-1 bulma-is-flex bulma-is-flex-direction-column"); diff --git a/WebApplication/model/VisitorRecordTableModel.cpp b/WebApplication/model/VisitorRecordTableModel.cpp index b5b79f0..8796f81 100644 --- a/WebApplication/model/VisitorRecordTableModel.cpp +++ b/WebApplication/model/VisitorRecordTableModel.cpp @@ -4,20 +4,7 @@ #include "DateTime.h" VisitorRecordTableModel::VisitorRecordTableModel(Session &session) : m_session(session) { - Wt::Dbo::Transaction transaction(session); - Wt::Dbo::collection> records = session.find(); - int index = 1; - std::unordered_map users; - for (auto &record : records) { - if (record->url.size() <= 1) continue; - if (!users.contains(record->visitorUuid)) { - users.insert({record->visitorUuid, std::format("匿名用户{}", index++)}); - } - - m_records.push_back({*record, false}); - std::get<0>(m_records.back()).visitorUuid = users.at(record->visitorUuid); - std::get<0>(m_records.back()).id = record.id(); - } + filter(""); } int VisitorRecordTableModel::columnCount(const Wt::WModelIndex &parent) const { @@ -154,6 +141,26 @@ Wt::WModelIndexSet VisitorRecordTableModel::selectedIndexes() const { return ret; } +void VisitorRecordTableModel::filter(const std::string &search) { + layoutAboutToBeChanged().emit(); + m_records.clear(); + + Wt::Dbo::Transaction transaction(m_session); + VisitorRecords records = m_session.find().where(std::format("url LIKE '%{}%'", search)); + int index = 1; + std::unordered_map users; + for (auto &record : records) { + if (record->url.size() <= 1) continue; + if (!users.contains(record->visitorUuid)) { + users.insert({record->visitorUuid, std::format("匿名用户{}", index++)}); + } + m_records.push_back({*record, false}); + std::get<0>(m_records.back()).visitorUuid = users.at(record->visitorUuid); + std::get<0>(m_records.back()).id = record.id(); + } + layoutChanged().emit(); +} + bool VisitorRecordTableModel::removeSelectedRows(const Wt::WModelIndexSet &indexes) { Wt::Dbo::Transaction transaction(m_session); layoutAboutToBeChanged().emit(); diff --git a/WebApplication/model/VisitorRecordTableModel.h b/WebApplication/model/VisitorRecordTableModel.h index 348b0c5..9a51678 100644 --- a/WebApplication/model/VisitorRecordTableModel.h +++ b/WebApplication/model/VisitorRecordTableModel.h @@ -21,6 +21,7 @@ public: Wt::ItemDataRole role = Wt::ItemDataRole::Edit) final; void sort(int column, Wt::SortOrder order = Wt::SortOrder::Ascending) final; Wt::WModelIndexSet selectedIndexes() const; + void filter(const std::string &search); bool removeRows(int row, int count, const Wt::WModelIndex &parent = Wt::WModelIndex()) final; bool removeSelectedRows(const Wt::WModelIndexSet &indexes); diff --git a/resources/webrtc.xml b/resources/webrtc.xml index a46c920..33b1626 100644 --- a/resources/webrtc.xml +++ b/resources/webrtc.xml @@ -97,4 +97,21 @@ 内容 备注 + +

访客数据统计

+
+
+
+ ${search-input class="bulma-input"} +
+
+ ${search-button class="bulma-button bulma-is-info"} +
+
+
+ ${remove-button class="bulma-button bulma-is-danger "} +
+
+ ${records} +
\ No newline at end of file