Project

General

Profile

How to refresh() a homepage

Added by Jason Lamar about 15 years ago

Hey, once more :) Wow I seem to have a lot of questions today ;)

If I'm running something like the wthomepage example, and I've added navigated to a page that, say, displays users in a table as well as adds new ones. How do I make it so that when I click on submit(), it refreshes the widget and displays the new user? Right now I have to "log out" and re-enter everything again (create a new session). I've made a call to WApplication::refresh(), but it didn't do anything. I have the reload-is-new-session set to false.

Thanks for the help (again)!

        mainMenu_->addItem
        (tr("administration"), administration(),
         WMenuItem::PreLoading)->setPathComponent("admin/");

WWidget *Home::administration()
{
    WContainerWidget *box = new WContainerWidget();


    box->addWidget(new WText("<h3>Add New User</h3>"));
    new WBreak(box);


    WLabel * newUserLabel = new WLabel("Add new user: ", box);
    newusername = new WLineEdit(box);
    newUserLabel->setBuddy(newusername);

    new Wt::WBreak(box);
    WPushButton *submit = new WPushButton("submit", box);
    submit->clicked().connect(SLOT(this, Home::addUser));
    new WBreak(box);
    new WBreak(box);
    new WBreak(box);

    box->addWidget(new WText("<h3>Users</h3>"));
    new WBreak(box);

    Wt::WTable *userTable = new Wt::WTable(box);
    userTable->setHeaderCount(1);

    userTable->elementAt(0,0)->addWidget(new Wt::WText("Username"));
    userTable->elementAt(0,1)->addWidget(new Wt::WText("Last Login"));
    userTable->elementAt(0,2)->addWidget(new Wt::WText("Delete?"));

    Wt::WSignalMapper<WText *> *MyMap = new Wt::WSignalMapper<Wt::WText *>(this);
    MyMap->mapped().connect(SLOT(this, Home::removeUser));

    WPushButton *deleteButton;


    CppSQLite3Query q = db.execQuery("Select distinct username from users");

    int i=0;
    while(!q.eof())
    {
        i++; // start at row #1
        WText *username_= new WText(q.fieldValue(0));
        deleteButton = new WPushButton("Delete");
        userTable->elementAt(i, 0)->addWidget(username_);
        userTable->elementAt(i, 1)->addWidget(new Wt::WText("March 26th, 2010"));
        userTable->elementAt(i, 2)->addWidget(deleteButton);
        MyMap->mapConnect(deleteButton->clicked(), username_); //call removeuser function with username as parameter
        q.nextRow();

    }
        return box;

}

Replies (5)

RE: How to refresh() a homepage - Added by Jason Lamar about 15 years ago

Still stuck on this one, any suggestions? I mean, I suppose I could delete the entire div and then re-add it every time, but that would create a mess.

Thanks :)

RE: How to refresh() a homepage - Added by Koen Deforche about 15 years ago

Hey Jason,

You can use WApplication::refresh() and reimplement the virtual WWidget::refresh() method to respond to a refresh (do not forget to call the base implementation). That works if you have one general "refresh", not if you need to express exactly what aspects should be refreshed.

Regards,

koen

RE: How to refresh() a homepage - Added by Jason Lamar about 15 years ago

So, how would I say that all of the pages need to be refreshed? Would I delete all of the containers (how?) and then re-add them? Or is there some shortcut of just calling refresh on a widget?

RE: How to refresh() a homepage - Added by Jason Lamar about 15 years ago

Im a bit confused about the structure of how the refresh works, WApplication::refresh() calls refresh on all of the widgets? I would need to reimplement the WWidget::refresh for each widget that I'm using? And then make a call to the base implementation of what? One general refresh would be fine, but I'm somewhat confused as to how I would do this, based on the code above.

RE: How to refresh() a homepage - Added by Wim Dumon about 15 years ago

Hey Jason,

Looking at your example code, I wonder if it wouldn't be better to create a subclass of WContainerWidget to implement the 'administration' functionality. You could write this as a self-contained widget, which updates itself when a user is added. Keep a reference to your WTable where you store the user list (userTable), and call clear() on it when the user list changes, and then repopulate the table. That will 'refresh' the table and show the new user data. You can do this either from within the slot of the 'add user' button, or by specializing refresh() as koen suggests.

Regards,

Wim.

    (1-5/5)