Note that there are some explanatory texts on larger screens.

plurals
  1. POWicket SELECT doesn't update it's model
    primarykey
    data
    text
    <p>i've wicket panel with list of ProductViews (as SELECT)<br/> <img src="https://i.stack.imgur.com/ojI2s.png" alt="product list by categories"> <br /> after you choose your ProductView from SELECT, its load Product from database by id of ProductView into details form. You can modify Product entity and you can save it when you finish.<br/><img src="https://i.stack.imgur.com/74350.png" alt="details form"> <br/> After save i try to refresh SELECT list to update its data, but it doesn't work(i mean, for example, SELECT contains an old name of product after rename it, but when i select the same ProductView, it reload an entity into details form again, and of course the new data appears from database) i don't want to reload product list again, i want to solve it from memory. Here is my source: </p> <p>ProductView:</p> <pre><code>@Entity @Table(name = "product") @XmlRootElement public class ProductView implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "id") private Long id; @Column(name = "name") private String name; @Enumerated(EnumType.ORDINAL) @Column(name = "category") private Category category; public ProductView() { } public ProductView(Long id) { this.id = id; } public ProductView(Product product) { this.id = product.getId(); this.name = product.getName(); this.category = product.getCategory(); } // + getters &amp; setters } </code></pre> <p>Product:</p> <pre><code>@Entity @Table(name = "product") @XmlRootElement public class Product implements Serializable { // same as ProductView but more data, objects, connections, etc } </code></pre> <p>And wicket panel with comments</p> <pre><code>private Product product; private ProductView productView; private List&lt;ProductView&gt; productViews; private Form productForm; private Select categorySelectComponent; private WebMarkupContainer contentContainer; public ProductPanel(String id) { super(id); setOutputMarkupId(true); add(contentContainer = new WebMarkupContainer("contentContainer")); // container DIV contentContainer.setOutputMarkupId(true); // refreshable contentContainer.add(productForm = new Form("productForm")); // details FORM contentContainer.add(categorySelectComponent = new Select("categorySelectComponent", new PropertyModel&lt;ProductView&gt;(this, "productView"))); // item SELECT categorySelectComponent.add( new SelectOptions&lt;ProductView&gt;( // first category "oneCategory", new PropertyModel&lt;List&lt;ProductView&gt;&gt;(this, "oneProducts"), // see getOneProducts(); list of productviews new IOptionRenderer&lt;ProductView&gt;() { @Override public String getDisplayValue(ProductView p) { return p.getName(); } @Override public IModel&lt;ProductView&gt; getModel(ProductView p) { return new Model&lt;ProductView&gt;(p); } })); categorySelectComponent.add( new SelectOptions&lt;ProductView&gt;( // second category "twoCategory", new PropertyModel&lt;List&lt;ProductView&gt;&gt;(this, "twoProducts"), // see getTwoProducts(); new IOptionRenderer&lt;ProductView&gt;() { @Override public String getDisplayValue(ProductView p) { return p.getName(); } @Override public IModel&lt;ProductView&gt; getModel(ProductView p) { return new Model&lt;ProductView&gt;(p); } })); categorySelectComponent.add( new SelectOptions&lt;ProductView&gt;( // third category "threeCategory", new PropertyModel&lt;List&lt;ProductView&gt;&gt;(this, "threeProducts"), // see getThreeProducts(); new IOptionRenderer&lt;ProductView&gt;() { @Override public String getDisplayValue(ProductView p) { return p.getName(); } @Override public IModel&lt;ProductView&gt; getModel(ProductView p) { return new Model&lt;ProductView&gt;(p); } })); categorySelectComponent.add(new OnChangeAjaxBehavior() { // update form after choose entity @Override protected void onUpdate(final AjaxRequestTarget art) { product = getProductFacade().find( productView.getId() ); updatePanel(art); } }); productForm.add( // some details component (textfields, radios, links, etc) to edit Product ); productForm.add(new AjaxSubmitLink("formSubmitLink") { // save entity @Override protected void onSubmit(AjaxRequestTarget art, Form&lt;?&gt; form) { super.onSubmit(art, form); // i don't know it is necessary at all getProductFacade().edit( product ); updateProductViewInCategoryMap(art); // important method //art.add(contentContainer); //it is in update method } }); } </code></pre> <p>more methods inside panel</p> <pre><code>private Map&lt;Category, List&lt;ProductView&gt;&gt; categoryMap; // all product by categories public void initCategoryMap() { categoryMap = new EnumMap&lt;Category, List&lt;ProductView&gt;&gt;(ProductView.class); categoryMap.put( Category.ONE, new ArrayList&lt;ProductView&gt;() ); categoryMap.put( Category.TWO, new ArrayList&lt;ProductView&gt;() ); categoryMap.put( Category.THREE, new ArrayList&lt;ProductView&gt;() ); for (ProductView view : getProductViews()) { categoryMap.get(view.getCategory()).add(view); } } //***** Get Products By Categories ******* final public List&lt;ProductView&gt; getOneProducts(){ if (categoryMap == null){ initCategoryMap(); } return categoryMap.get( Category.ONE ); } final public List&lt;ProductView&gt; getTwoCategory(){ if (categoryMap == null){ initCategoryMap(); } return categoryMap.get( Category.TWO ); } final public List&lt;ProductView&gt; getThreeProducts(){ if (categoryMap == null){ initCategoryMap(); } return categoryMap.get( Category.THREE ); } // ************************************** public List&lt;ProductView&gt; getProductViews() { // Get All Product if (productViews == null) { productViews = getProductFacade().findAllProductAsView(); } return productViews; } private void updatePanel(AjaxRequestTarget art) { // refresh panel art.add(ProductPanel.this); } private void updateProductViewInCategoryMap(AjaxRequestTarget art) { // change Product in map after save (call from onSubmit method of AjaxSubmitLink) for(Map.Entry&lt;Category, List&lt;ProductView&gt;&gt; entry : categoryMap.entrySet()){ // search category contains entity if (entry.getValue().contains( productView )){ entry.getValue().remove( productView ); // remove entity from category break; } } productView = new ProductView( product ); // new productview by modified product categoryMap.get( productView.getCategory() ).add( productView ); // add entity to it's category's list art.add(contentContainer); } </code></pre> <p>and HTML:</p> <pre><code>&lt;select class="categorySelect" wicket:id="categorySelectComponent"&gt; &lt;optgroup label="Category One"&gt; &lt;wicket:container wicket:id="oneCategory"&gt; &lt;option wicket:id="option"&gt;&lt;/option&gt; &lt;/wicket:container&gt; &lt;/optgroup&gt; &lt;optgroup label="Category Two"&gt; &lt;wicket:container wicket:id="twoCategory"&gt; &lt;option wicket:id="option"&gt;&lt;/option&gt; &lt;/wicket:container&gt; &lt;/optgroup&gt; &lt;optgroup label="Category Three"&gt; &lt;wicket:container wicket:id="threeCategory"&gt; &lt;option wicket:id="option"&gt;&lt;/option&gt; &lt;/wicket:container&gt; &lt;/optgroup&gt; &lt;/select&gt; </code></pre> <p>Any idea?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload