Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Client-sided solution (Javascript)</h2> <p>You could save an Ajax call and gain responsiveness by coding that behavior client-side. In the simplest of the scenarios, if you can give those checkboxes a fixed "id" attribute, you could use <code>Component.setMarkupId()</code> to force an <code>id</code> value for the checks (make sure it is unique in the DOM): </p> <p><strong>Java</strong></p> <pre><code>add(new CheckBox("1").setOutputMarkupId(true).setMarkupId("check1"); add(new CheckBox("2").setOutputMarkupId(true).setMarkupId("check2"); add(new CheckBox("3").setOutputMarkupId(true).setMarkupId("check3"); </code></pre> <p><strong>HTML</strong></p> <pre><code>&lt;input type="checkbox" wicket:id="1" onclick="uncheck(this,1)"/&gt; &lt;input type="checkbox" wicket:id="2" onclick="uncheck(this,2)"/&gt; &lt;input type="checkbox" wicket:id="3" onclick="uncheck(this,3)"/&gt; </code></pre> <p><strong>Javascript</strong></p> <pre><code>function uncheck(comp, idx){ if (comp.checked) { if (idx == 1){ document.getElementById("check2").checked = false; document.getElementById("check3").checked = false; } else if (idx == 2 || idx == 3){ document.getElementById("check1").checked = false; } } } ​ </code></pre> <hr> <h2>Java only solution (Ajax)</h2> <p>If you really want to do this server-side, it would make sense to also propagate the values server side (instead of relying on which component was last focused), for instance with <code>AjaxFormComponentUpdatingBehavior</code>.</p> <pre><code> final CheckBox c1 = new CheckBox("on"); final CheckBox c2 = new CheckBox("off"); c1.setOutputMarkupId(true); c2.setOutputMarkupId(true); c1.add(new AjaxFormComponentUpdatingBehavior("onclick") { private static final long serialVersionUID = 1L; protected void onUpdate(final AjaxRequestTarget target) { if (Boolean.TRUE.equals(c1.getModelObject())) { c2.setModelObject(Boolean.FALSE); target.add(c2); } } }); c2.add(new AjaxFormComponentUpdatingBehavior("onclick") { private static final long serialVersionUID = 1L; protected void onUpdate(final AjaxRequestTarget target) { if (Boolean.TRUE.equals(c2.getModelObject())) { c1.setModelObject(Boolean.FALSE); target.add(c1); } } }); add(c1); add(c2); </code></pre> <p>If this constraint is important for the integrity of your data model, you should also implement it in a <code>FormValidator</code> so that invalid input never reaches the <code>Form</code>'s <code>Model</code>s. Users without JS enabled or just fiddling with the DOM could bypass it (the same would apply if using an Ajax Behavior).</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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