How to use links with parameters...
Added by Andres Jaimes about 12 years ago
Hello again,
In the PHP world it is very common to have lists of links like the following:
Item 1
Item 2
Item 3
Item 4
Item 5
I'm trying to do something similiar with Wt, however I don't know how. I mean, I know how to generate the list of links, but I don't know how to wire it so when a user clicks on a link something happens depending on the link's "id" parameter. If I'd only have one link I would use the WText::clicked().connect() function (one link to one function relation), however for a list that can potentially grow, how would you manage this?
Thanks
Replies (2)
RE: How to use links with parameters... - Added by Koen Deforche about 12 years ago
Hey,
Using std::bind() (or boost::bind() if you're on backward compatible C), you would have:
for (...) {
std::string id = ...;
widget = ...;
widget->clicked().connect(std::bind(&MyWidget::func, this, id));
}
void MyWidget::func(const std::string& id)
{
// process a click for 'id'
}
RE: How to use links with parameters... - Added by Andres Jaimes about 12 years ago
I take my hat off to you... It worked perfectly. I'm pasting my code so it can work for future references.
Thank you.
for (std::vector<AppModel::EventListItem>::const_iterator it = result->begin(); it != result->end(); it++) {
std::string id = it->get<0>();
WContainerWidget* li = new WContainerWidget(ul);
WText* a = new WText("<a href=\"#\">" + it->get<1>() + "</a>");
a->clicked().connect(boost::bind(&HomeWidget::eventList, this, id));
li->addWidget(a);
}
void HomeWidget::eventList(const std::string& id) {
content->addWidget(new WText(id));
}