Sri Harsha
Sri Harsha

Reputation: 319

select/unselect all elements in one go in primefaces <p:selectCheckboxMenu>?

I have gone through the documentation of primefaces and sadly there is no option to select all the options of the p:selectCheckboxMenu component in one go like you can using Javascript.

Can anyone suggest how it can be done using Javascript or using the Backing Bean?

Upvotes: 1

Views: 3468

Answers (2)

BalusC
BalusC

Reputation: 1109745

Just preset its value with the same value as the available values the usual way (like as you would do for every other JSF input component).

Based on the PrimeFaces showcase example of <p:selectCheckboxMenu>, here's how it should look like:

package org.primefaces.examples.view;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FormBean implements Serializable {

    private List<String> selectedMovies;

    private Map<String,String> movies;

    public FormBean() {
        movies = new HashMap<String, String>();
        movies.put("Scarface", "Scarface");
        movies.put("Goodfellas", "Goodfellas");
        movies.put("Godfather", "Godfather");
        movies.put("Carlito's Way", "Carlito's Way");

        selectedMovies = new ArrayList<String>();
        selectedMovies.add("Scarface");
        selectedMovies.add("Goodfellas");
        selectedMovies.add("Godfather");
        selectedMovies.add("Carlito's Way");
    }

    public List<String> getSelectedMovies() {
        return selectedMovies;
    }
    public void setSelectedMovies(List<String> selectedMovies) {
        this.selectedMovies = selectedMovies;
    }

    public Map<String, String> getMovies() {
        return movies;
    }
}

Note that the selectedMovies is been prepared in bean's constructor. This can of course be done more elegantly based on the Map movies. It's just to give you the idea.

Doing this with JavaScript is unnecessary.

Upvotes: 1

Torrent Lee
Torrent Lee

Reputation: 845

<label><input type="checkbox" name="" id="" class="someclass-selectall"/>choose all</label><br/>

<label><input type="checkbox" name="" id="" class="someclass"/>some text</label><br/>
<label><input type="checkbox" name="" id="" class="someclass"/>some text</label><br/>
<label><input type="checkbox" name="" id="" class="someclass"/>some text</label><br/>
<label><input type="checkbox" name="" id="" class="someclass"/>some text</label><br/>
<label><input type="checkbox" name="" id="" class="someclass"/>some text</label><br/>
<label><input type="checkbox" name="" id="" class="someclass"/>some text</label><br/>

<script type="text/javascript" src="../share/libs/jquery-1.7.min.js"></script>

<script type="text/javascript">
 // use jquery
 $(".someclass-selectall").click(function  (  ) {
    $("input:checkbox.someclass").attr ( "checked", this.checked );
 })
</script>

try this

Upvotes: 0

Related Questions