Tuesday, July 30, 2013

Multiple Checkboxes inside Grid Control in Tapestry5

I am going to show an example on how to display multiple checkbox(<t:checkbox >) with Grid control(<t:grid). For this I am going to use the same Employee master example which I have shown in my previous article.

Page-EmployeeMasterPG

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<html t:type="layout" title="sample Index"
      t:sidebarTitle="Framework Version"
      xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
      xmlns:p="tapestry:parameter">
<form t:type="form" t:id="employeeMasterList">
   <t:grid source="addresses" row ="employee" add="delete,deleteCheck">
      <p:empIdCell>
                <t:pagelink page="CreateEmployeeMasterPG" context="encrypt(employee.id)">${employee.empId}</t:pagelink>
         </p:empIdCell>
          <p:deletecell>
                <t:actionlink t:id="delete" context="employee.id">Delete</t:actionlink>
          </p:deletecell>
          <p:deleteCheckCell>
           <t:checkbox t:id="deleteCheckbox" value ="currentSelected" />
          </p:deleteCheckCell>
         <p:empty>
              <p>There are no users to display; you can <t:pagelink page="createEmployeeMasterPG">add some</t:pagelink>.</p>
          </p:empty>
      </t:grid>
   <br/>
    <t:submit t:id="deleteMultiple" value="Delete"/>    
<a t:type="eventlink" t:event="deleteSelected" href="#">Delete</a>
      <a t:type="eventlink" t:event="CreateEmployee" href="#">Create Employee</a><br/><br/>
   </form>
</html>

Java-EmployeeMasterPG
In the above page we have defined a checkbox , now we will see how to retrieve the values

<t:checkbox t:id="deleteCheckbox" value ="currentSelected" />
The value can be retrieved in the java file with getCurrentSelected and setCurrentSelected accessors

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
package org.my.in.sample.pages;

import org.my.in.sample.Util.AESencrp;
import org.my.in.sample.entities.EmployeeMasterEntityEO;
import org.my.in.sample.entities.EmployeeMasterListVO;
import org.my.in.sample.model.EmployeeMasterAM;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.services.Request;
import org.hibernate.Hibernate;
import org.hibernate.Session;


public class EmployeeMasterPG 
{
  @Inject
     private Session session;
  
  @InjectPage
  private CreateEmployeeMasterPG createEmployeeMaster;
  
  @Property
  private EmployeeMasterListVO employee;
  
  @Persist
  private HashSet<Long> selectedSet; 

  private String id;
  
 
  
  public boolean getCurrentSelected() 
  {
   if(selectedSet != null)
    return selectedSet.contains(employee.getId());
   else 
    selectedSet= new HashSet<Long>();
   return false;
  } 
  

      public void setCurrentSelected(boolean value) 
      { 
       if(selectedSet == null)
        selectedSet =new HashSet<Long>();
             if (value) 
             { 
              selectedSet.add(employee.getId()); 
             } 
             else 
             { 
                 selectedSet.remove(employee.getId()); 
             } 
      } 

  @SuppressWarnings("unchecked")
  public List<EmployeeMasterListVO> getAddresses()
     {
   List<EmployeeMasterListVO> emp = session.createSQLQuery("SELECT id, empId, empName, createdBy, createdDate FROM employeemaster")
      .addEntity(EmployeeMasterListVO.class)
      .list();

   return emp;
     }
  
 
  Object onCreateEmployee()
  {
   System.out.println("onCreateEmployee");
   return createEmployeeMaster;
  }
  
  void onActionFromDelete(long id)
  {
   EmployeeMasterAM.delete(id);
  }
  
  void onDeleteSelected()
  {
   if(selectedSet != null)
   {
    System.out.println(selectedSet.size());
    for (Long id : selectedSet) {
        System.out.println(id);
    }
    Iterator iter = selectedSet.iterator();
    while (iter.hasNext()) {
      System.out.println(iter.next());
    }
   }
  }
  
  void onSelectedFromDeleteMultiple()
  {

  }

  
}

In the EmployeeMasterListPG.java  the @Persist needs to be given to retain the values when the form is submitted. The selected Set is the hash set which will store which checkbox have been checked and unchecked

   @Persist
   private HashSet<Long> selectedSet;

We are getting the checkbox value already persisted using the method below. For the first time the SelectedSet initialization will happen. After that, it will check for the id and if present returns.

public boolean getCurrentSelected()
{
if(selectedSet != null)
return selectedSet.contains(employee.getId());
else
selectedSet= new HashSet<Long>();
return false;
}

Likewise for setting the value the user has checked or unchecked.

 public void setCurrentSelected(boolean value)
     {
    if(selectedSet == null)
    selectedSet =new HashSet<Long>();
             if (value)
             {
            selectedSet.add(employee.getId());
             }
             else
             {
                 selectedSet.remove(employee.getId());
             }
     }


You can retrieve the selected values in two ways

1. for (Long id : selectedSet) {
        System.out.println(id);
    }


2.   Iterator iter = selectedSet.iterator();
    while (iter.hasNext()) {
      System.out.println(iter.next());
    }


Note: Please note that form needs to be submitted to retrieve the values. I tried to call using the event link. But the values are not retained. The tapestry5 understands only if the form where the checkbox is given to be submitted. It can be done either through submit button or through javascript :this.form.submit();

1 comment:

Anonymous said...

Thank you so much. It's exactly what I was looking for.
Tapestry's Checklist object wasn't good for what I was trying to do, because I use a grid, just like you.
So thank you.