Project

General

Profile

Bug #5353 » 0001-Improve-FormModel-behavior-without-JavaScript.patch

Bruce Toll, 10/19/2016 12:25 AM

View differences:

examples/widgetgallery/examples/FormModel.cpp
// setValue() for each field.
setValue(BirthField, Wt::WDate());
setValue(CountryField, std::string());
setValue(ChildrenField, Wt::WString());
}
Wt::WAbstractItemModel *countryModel() {
......
}
void updateCityModel(const std::string& countryCode) {
if (cityModel_->rowCount() && countryCode == cityModelCountryCode_)
return;
cityModel_->clear();
CityMap::const_iterator i = cities.find(countryCode);
......
cityModel_->appendRow(
new Wt::WStandardItem("(Choose Country first)"));
}
cityModelCountryCode_ = countryCode;
}
// Get the user data from the model
......
static const CityMap cities;
static const CountryMap countries;
Wt::WStandardItemModel *countryModel_, *cityModel_;
std::string cityModelCountryCode_;
static const int MAX_LENGTH = 25;
static const int MAX_CHILDREN = 15;
......
model = new UserFormModel(this);
setTemplateText(tr("userForm-template"));
addFunction("id", &WTemplate::Functions::id);
addFunction("block", &WTemplate::Functions::id);
/*
* First Name
......
model->updateCityModel(code);
}));
setFormWidget(UserFormModel::CountryField, countryCB,
[=] () { // updateViewValue()
std::string code = boost::any_cast<std::string>
(model->value(UserFormModel::CountryField));
int row = model->countryModelRow(code);
countryCB->setCurrentIndex(row);
},
[=] () { // updateModelValue()
std::string code = model->countryCode(countryCB->currentIndex());
model->setValue(UserFormModel::CountryField, code);
});
setFormWidget(UserFormModel::CountryField, countryCB);
/*
* City
*/
Wt::WComboBox *cityCB = new Wt::WComboBox();
cityCB->setModel(model->cityModel());
setFormWidget(UserFormModel::CityField, cityCB);
setFormWidget(UserFormModel::CityField, cityCB, 0,
[=] () { // updateModelValue()
std::string countryCode = model->countryCode(countryCB->currentIndex());
model->updateCityModel(countryCode);
model->setValue(UserFormModel::CityField, cityCB->valueText());
});
/*
* Birth Date
*/
Wt::WDateEdit *dateEdit = new Wt::WDateEdit();
setFormWidget(UserFormModel::BirthField, dateEdit,
[=] () { // updateViewValue()
Wt::WDate date = boost::any_cast<Wt::WDate>
(model->value(UserFormModel::BirthField));
dateEdit->setDate(date);
},
[=] () { // updateModelValue()
Wt::WDate date = dateEdit->date();
model->setValue(UserFormModel::BirthField, date);
});
setFormWidget(UserFormModel::BirthField, new Wt::WDateEdit());
/*
* Children
*/
setFormWidget(UserFormModel::ChildrenField, new Wt::WSpinBox());
Wt::WSpinBox *childrenSB = new Wt::WSpinBox();
setFormWidget(UserFormModel::ChildrenField, childrenSB,
[=] () { // updateViewValue()
if (childrenSB->validate() == Wt::WValidator::Valid)
childrenSB->setValueText(boost::any_cast<Wt::WString>
(model->value(UserFormModel::ChildrenField)));
}, 0);
/*
* Remarks
(1-1/4)