从ModalWindow Wicket在DataGrid中添加行

时间:2018-07-26 11:41:47

标签: java wicket

我在向表中添加新行时遇到了一些问题。我尝试了3种不同的方法,但没有给出结果。将不胜感激。谢谢

AdminPage

public class AdminPage extends WebPage{
   private DataGrid<IDataSource<User>, User, String> grid;
   public AdminPage(PageParameters parameters){
      super(parameters);
   }

   @Override
   protected void onInitialize() {
      super.onInitialize();
      List<IGridColumn<IDataSource<User>, User, String>> columns = new ArrayList<>();
      columns.add(new PropertyColumn<IDataSource<User>, User, Long, String>(
            new ResourceModel("UserID"), "id", "id"));
      columns.add(new EditablePropertyColumn<IDataSource<User>, User, String, String>(
            new ResourceModel("UserName"), "name", "name") {
        @Override
        protected void addValidators(FormComponent<String> component) {
            component.add(new PropertyValidator<User>());
            component.setRequired(true);
        }
      });
      columns.add(new EditablePropertyColumn<IDataSource<User>, User, String, String>(
            new ResourceModel("UserSurname"), "surname", "surname") {
        @Override
        protected void addValidators(FormComponent<String> component) {
            component.add(new PropertyValidator<User>());
            component.setRequired(true);
        }
      });
      columns.add(new EditablePropertyColumn<IDataSource<User>, User, String, String>(
            new ResourceModel("UserEmail"), "email", "email") {
        @Override
        protected void addValidators(FormComponent<String> component) {
            component.add(new UniqueEmailValidator());
            component.setRequired(true);
        }
      });
      columns.add(new EditablePropertyColumn<IDataSource<User>, User, String, String>(
            new ResourceModel("UserUsername"), "loginInfo.username") {
        @Override
        protected void addValidators(FormComponent<String> component) {
            component.add(new UniqueUsernameValidator<User>());
            component.setRequired(true);
        }
      });
      columns.add(new EditablePropertyColumn<IDataSource<User>, User, String, String>(
            new ResourceModel("UserPassword"), "loginInfo.password") {

        @Override
        protected void addValidators(FormComponent<String> component) {
            component.add(new PropertyValidator<User>());
            component.setRequired(true);
        }
      });
      columns.add(new AddDeleteColumn<IDataSource<User>, User, String>("actions", Model.of("Actions")) {
        @Override
        protected void onDelete(AjaxRequestTarget target, IModel rowModel, WebMarkupContainer rowComponent) {
            User user = (User) rowModel.getObject();
            UserDTO.getInstance().remove(user);
            target.add(getGrid());
        }

        @Override
        protected void onSubmitted(AjaxRequestTarget target, IModel<User> rowModel, WebMarkupContainer rowComponent) {
            User user = rowModel.getObject();
            UserDTO.getInstance().update(user);
            target.add(getGrid());
        }

        @Override
        public AbstractColumn<IDataSource<User>, User, String> setInitialSize(int initialSize) {
            return super.setInitialSize(60);
        }
    });

    grid = new DefaultDataGrid<>("grid", new UserDataSource(){
        @Override
        public void insertRow(long l, User user) {
            grid.insertRow(user);
        }

        @Override
        public void deleteRow(long l, User user) {

        }
    }, columns);
    add(grid);
    add(new NoRecordsToolbar<IDataSource<User>, User, String>(grid));
    grid.setOutputMarkupId(true);
    grid.setAllowSelectMultiple(true);
    grid.setSelectToEdit(false);
    grid.setClickRowToSelect(true);
//---------------------PROBLEM-----------------------------------------
    final ModalWindow modalWindow = new ModalWindow("modal");
    add(modalWindow);
    modalWindow.setCookieName("addNewUser");
    modalWindow.setPageCreator(() -> new AddUserModalPage(AdminPage.this.getPageReference(), modalWindow));
    modalWindow.setWindowClosedCallback(innerTarget -> {
        innerTarget.add(updateGrid());
    });
    modalWindow.setCloseButtonCallback(new ModalWindow.CloseButtonCallback(){
        @Override
        public boolean onCloseButtonClicked(AjaxRequestTarget target) {
            grid = updateGrid();
            return true;
        }
    });
    add(new AjaxLink<Void>("addUser") {
        @Override
        public void onClick(AjaxRequestTarget target) {
            modalWindow.show(target);
        }
    });
    } 
    public DataGrid<IDataSource<User>, User, String> getGrid() {
        return grid;
    }
    public Item<User> insertRow(User rowData){
        return grid.insertRow(rowData);
    }

    public DataGrid<IDataSource<User>, User, String> updateGrid(){
        grid.markAllItemsDirty();
        grid.update();
        return grid;
    }
}

AddUserModalWindow

public class AddUserModalPage extends WebPage {
    private static final long serialVersionUID = 1L;
    private String username;
    private String name;
    private String surname;
    private String email;
    private String password;
    private String confirmPassword;
    private User user = new User();

    @SpringBean
    private UserDao userDao;

    public AddUserModalPage(final PageReference reference, final ModalWindow window) {
        Form<Void> form = new Form<Void>("addUserForm");
        form.setDefaultModel(new CompoundPropertyModel<>(user));
        form.setOutputMarkupId(true);
        FeedbackPanel panel = new FeedbackPanel("feedback",
            new ExactLevelFeedbackMessageFilter(FeedbackMessage.ERROR));
        panel.setOutputMarkupId(true);
        form.add(panel);
        form.add(new RequiredTextField<>("name",
            new PropertyModel<>(user, "name"))
            .add(new PropertyValidator<User>()));
        form.add(new RequiredTextField<>("surname",
            new PropertyModel<>(user, "surname"))
            .add(new PropertyValidator<User>()));
        form.add(new EmailTextField("email",
            new PropertyModel<>(user, "email"), new UniqueEmailValidator())
            .setRequired(true));
        form.add(new RequiredTextField<>("username",
            new PropertyModel<>(user, "loginInfo.username"))
            .add(new UniqueUsernameValidator<User>()));
        PasswordTextField passwordTextField, confirmPassword;
        form.add(passwordTextField = new PasswordTextField("password",
            new PropertyModel<>(user, "loginInfo.password")));
        form.add(confirmPassword = new PasswordTextField("confirmPassword",
            new PropertyModel<>(user, "loginInfo.password")));
        passwordTextField.add(new PropertyValidator<User>());
        confirmPassword.add(new PropertyValidator<User>());
        form.add(new EqualPasswordInputValidator(passwordTextField, confirmPassword));
//-------------------------PROBLEM-----------------------------------------
        form.add(new AjaxButton("Create") {
            @Override
            protected void onSubmit(AjaxRequestTarget target) {
                userDao.createUser(user);
   // 1 way   HERE I get java.lang.IllegalArgumentException: Cannot update component 
because its page is not the same as the one this handler has been created for. 
Component: [DefaultDataGrid [Component id = grid]]
   //         target.add(((AdminPage)reference.getPage()).updateGrid());
   // 2 way   HERE I get WicketRuntimeException("Error BAD Data Source type")        
   //         (((AdminPage)reference.getPage()).getGrid()).insertRow(user);
   // 3 way   HERE insertRow(user) is hang and window.close(target) doesn't work
   //         ((AdminPage)reference.getPage()).insertRow(user);
              window.close(target);
        }
    });
    form.add(new AjaxButton("Cancel"){
        @Override
        protected void onSubmit(AjaxRequestTarget target) {
            window.close(target);
        }
    });
//-------------------------------------------------------------
    form.add(new AjaxFormValidatingBehavior("keydown", Duration.ONE_SECOND));
    add(form);
}

0 个答案:

没有答案