can't get Wt::WStringListModel->setFilterRegExp() to work
Added by Mark Travis over 1 year ago
I've tried the following:
std::shared_ptr<Wt::WSortFilterProxyModel> filterColumns;
myRegEx = std::make_unique<std::regex>(columnFilterEntry_->text().toUTF8());
filterColumns->invalidate();
filterColumns->setFilterRegExp(std::move(myRegEx));
In the prior example, I also tried to concatenate "\" to the beginning and "\g" to the end as well as "*" in myRegEx.
I've also tried using the output from WRegExpValidator:
validator = std::make_shared<Wt::WRegExpValidator>("[a-zA-Z0-9._*%+-]{1,30}");
filterColumns->invalidate();
filterColumns->setFilterRegExp(validator->regExp());
Nothing seems to work. I've got 830 strings in the WStringListModel with ->setFilterKeyColumn(0). (set to zero) and setFilterRole(Wt::ItemDataRole::Display). I see all 830 rows when the page comes up, but when I type in a couple of characters that I KNOW match at least 20 of the strings, nothing is returned.
Replies (3)
RE: can't get Wt::WStringListModel->setFilterRegExp() to work - Added by Matthias Van Ceulebroeck over 1 year ago
Hi Mark,
keep in mind that the regex required here is a std::regex
. I created a simple example that does correctly filter some values, which happens correctly.
class RegExpServer : public Wt::WApplication {
public:
explicit RegExpServer(const Wt::WEnvironment &env)
: WApplication{env}
{
columnFilterEntry_ = root()->addNew<Wt::WLineEdit>();
auto okBtn = root()->addNew<Wt::WPushButton>("Ok");
auto model = createStringListModel();
filterColumns_ = std::make_shared<Wt::WSortFilterProxyModel>();
filterColumns_->setSourceModel(model);
filterColumns_->setFilterKeyColumn(0);
auto table = root()->addNew<Wt::WTableView>();
table->setModel(filterColumns_);
okBtn->clicked().connect([this] {
auto myRegEx = std::make_unique<std::regex>(columnFilterEntry_->text().toUTF8());
filterColumns_->invalidate();
filterColumns_->setFilterRegExp(std::move(myRegEx));
});
}
private:
Wt::WLineEdit* columnFilterEntry_ = nullptr;
std::shared_ptr<Wt::WSortFilterProxyModel> filterColumns_;
std::shared_ptr<Wt::WAbstractItemModel> createStringListModel()
{
auto vec = std::vector<Wt::WString>({ Wt::WString("this"), Wt::WString("is"), Wt::WString("a"), Wt::WString("test") });
auto model = std::make_shared<Wt::WStringListModel>(vec);
return model;
}
If I input a regex like ^t.*
it filters correctly and shows this
and test
.
RE: can't get Wt::WStringListModel->setFilterRegExp() to work - Added by Mark Travis over 1 year ago
Thanks!!
I changed
auto myRegEx = std::make_unique<std::regex>(columnFilterEntry_->text().toUTF8());
to
auto myRegEx = std::make_unique<std::regex>("^" + columnFilterEntry_->text().toUTF8() + ".*");
And it works!! So simple, but so obscure. I wasn't familiar with regex before now.
RE: can't get Wt::WStringListModel->setFilterRegExp() to work - Added by Matthias Van Ceulebroeck over 1 year ago
Ah yes, regex will always try to fully match. E.g. t
won't match for test
, only for t
.
Only test
itself and things like t.*
, t.+
, \w
, t[est]{3}
will match. This is obviously a limited set of potential match generators, but always shows that a full match is necessary.