aspose file tools*
The moose likes JSF and the fly likes Select All and Clear All implementation in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Select All and Clear All implementation in JSF" Watch "Select All and Clear All implementation in JSF" New topic
Author

Select All and Clear All implementation in JSF

srikanth koppisetty
Greenhorn

Joined: Feb 14, 2005
Posts: 20
I have a data table and there are check boxes of each row and I would like to implement the Select All and Clear All links at the bottom of Data Table.

And as expected it should select all or clear all, all the checkboxes in data table.

Please let me, how to do this.

Thank You,
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Can you explain at what point are you stucked?


- Varun
srikanth koppisetty
Greenhorn

Joined: Feb 14, 2005
Posts: 20
I would like to implement, but i do not know the way, since if i use the java script, how to call the JSF component.

So, if you have implemented this, please let me know and if possible please send me sample code of jsf file.

Thanks in advance.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Do it on server side. If the user clicks "Select all", go to backing bean's listener method and set the boolean property mapped to your checkbox to "true" for all objects in collection.
Similar approach will work for uncheck option too.
Prakash Jebaraj
Greenhorn

Joined: Mar 14, 2005
Posts: 5
Here is the client side code.

In the page bean, have a property to hold the count of rows displayed in the datatable and add inputHidden component in the form

<h:inputHidden id="countId" value="#{%pageBean%.%count%}"></h:inputHidden>


Add the below Javascript function

function selectAll(source) {
alert("Entered selectAll");

form = document.forms["%formId%"]; //replace %formId% with ur formId

var count = form["%formId%:countId"].value;

alert("count:"+count); //This gives count of rows

for(i=0; i < count; i++){
form["%formId%:%dataTableId%:" +i + ":%checkBoxId%"].checked = true;
}
}

function clearAll(source) {
alert("Entered clearAll");

form = document.forms["%formId%"]; //replace %formId% with ur formId

var count = form["%formId%:%checkBoxId%"].value; //replace %checkBoxId% with ur checkBoxId

alert("count:"+count); //This gives count of rows

for(i=0; i < count; i++){
form["%formId%:%dataTableId%:" +i + ":%checkBoxId%"].checked = false;
}
}
srikanth koppisetty
Greenhorn

Joined: Feb 14, 2005
Posts: 20
Thank you friend for detailed reply , I will try this. Have a nice day
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Varun Khanna:
Do it on server side. If the user clicks "Select all", go to backing bean's listener method and set the boolean property mapped to your checkbox to "true" for all objects in collection.
Similar approach will work for uncheck option too.


I'd second that for a couple of reasons.

  • With JSF you need to just break down and think Server Side. Javascript is good but what happens when someone visits your site that doesn't have Javascript turned on. At the very least have it work both ways. I typically just don't bother with JS unless I have to
  • Page stay much cleaner if you do all the work in the backing bean.


  • This is not JSP/Servlet programming. This is JSF. A component oriented framework. Think components, think actions, think GUI's. Stop thinking HTML.


    GenRocket - Experts at Building Test Data
    Prakash Jebaraj
    Greenhorn

    Joined: Mar 14, 2005
    Posts: 5
    I agree with Greg and Varun
    Jimmy Ing
    Greenhorn

    Joined: Nov 14, 2008
    Posts: 1
    I�m trying the JSF approach, but the problem is that I use some of the check box components appear on other pages. So after doing a select all on one page, I go to another page, those same boxes would render as checked. They would need to be reset somehow, but I'm not sure how I would approach it. Any suggestions? Thanks!
    NaviNice Naveen
    Greenhorn

    Joined: Aug 04, 2010
    Posts: 2
    Hi,
    Every One I am New in JSF,,
    I have to select all checkboxes on click of a button,,,,,,,,,,,,,,,
    I am not using javascript ,,,
    Please can you tell me , How Can handle this with actionListner ???
    As Mr. Varun Khanna Suggest...
    NaviNice Naveen
    Greenhorn

    Joined: Aug 04, 2010
    Posts: 2
    I have done select all checkboxes on click of a button in jsf on server side.
    But o server it taking 5-6 sec...
    it is taking too much time ,
    i want to ask that,
    What is the good way to this work.
    1 . Do In server Side
    2. Do in client Side..........


    Suggest me.....
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Select All and Clear All implementation in JSF