1
votes

Good day! I create some Table:

List<IColumn<User, String>> columns = new ArrayList<>();

columns.add(new AbstractColumn<User, String>(new Model<String>("")) {

        @Override
        public void populateItem(Item<ICellPopulator<User>> cellItem, String componentId, IModel<User> rowModel) {
            cellItem.add(new Link<String>(componentId) {

                @Override
                public void onClick() {
                    System.out.println("editors" + rowModel.getObject().getName());
                    PageParameters parameters = new PageParameters();
                    parameters.add("id", rowModel.getObject().getId());
                    add(new EditPanel("panel", rowModel));

                }

                @Override
                public IMarkupFragment getMarkup() {
                    return Markup.of("<div wicket:id='cell'> edit </div>");
                }
            });

        }

When I click on cell into Table, cell markup "Edit", I create some Panel:

public class EditPanel extends Panel {

public EditPanel(String id, IModel<User> model) {
    super(id, model);

    User user = model.getObject();
    if (user == null) {
        user = new User();
    }

    List<UserRole> list = Arrays.asList(UserRole.values());

    Form<?> form = new Form("form", new CompoundPropertyModel(user));

    TextField<String> userName = new TextField<String>("name");

    };

    add(new FeedbackPanel("feedback"));
    add(form);
    form.add(userName);

}   

}

How can I set value to TextField userName = new TextField("name"); from my model, or if model == null, set any text what I need? Thanks!

2
Since you are using CompoundPropertyModel try to give entity name as text field name .i.e if your user model is username then You should specify new TextField("username")then you don't rely on fields - soorapadman

2 Answers

0
votes

If you use Wicket 7 or older then you may use :

TextField<String> userName = new TextField<String>("name", new PropertyModel(model, "username"));

assuming that username is a property of User.

With Wicket 8.x you can use LambdaModel instead.

0
votes

Heyy, I think the problem is because you are passing the object and not the model.

The right way is passing with CompoundPropertyModel, like you do. It`s better then PropertyModel in this case.

Try doing this:

...
    if (model.getObject() == null) {
        model.setObject(new User());
    }
 Form<?> form = new Form("form", new CompoundPropertyModel(model));
...

Hope help you.