0

I want to set dynamic id to many check box in <p:dataTable>. how can i give it please help.

<p:column id="sipyes">  
      <center>
      <h:selectBooleanCheckbox  rendered="#{var1[5]}" id="#{var1[4]}">
      <p:ajax listener="#{pmmbean.checkitem(var1[2],var1[0],var1[3])}"/>
      </h:selectBooleanCheckbox><br/>
      <h:outputText value="#{var1[4]}" rendered="#{var1[5]}"/>
      </center>
</p:column>

Here I can get value of var1[4] but it can not set in the id attribute.

Here I got id selectBooleanCheckbox id is empty.

and got this type of error.

How can I resolve it?

Got this type of Error :

HTTP Status 500 - Empty id attribute is not allowed.

Tiny
  • 27,221
  • 105
  • 339
  • 599
devsandipk
  • 3
  • 1
  • 5
  • The HTML `
    ` tag was deprecated a long ago. Make use of CSS instead.
    – Tiny Apr 10 '14 at 14:53
  • Do you want to have checkboxes like [this](http://www.primefaces.org/showcase/ui/datatableRowSelectionRadioCheckbox.jsf) for row selection? You can use `` as shown on the showcase. Checkboxes will be available for multiple row selection. – Tiny Apr 10 '14 at 15:00
  • No Tiny i have many checkboxes in all column and particular checkbox in the cell redered from the database. so i want to give id particular that checkbox. – devsandipk Apr 10 '14 at 15:05
  • Are you sure `var1` is initialized correctly in the corresponding managed bean? It is difficult to tell without looking into the corresponding JSF managed bean. – Tiny Apr 10 '14 at 15:15
  • Here var1 is not define in the JSF managed bean. var1 is the variable. – devsandipk Apr 10 '14 at 15:45

2 Answers2

1

By default all elements inside a dataTable take dynamic IDs, for example:

<h:form id="myForm">
    <p:dataTable id="myTable" value="..." var="...">
        <p:column>
            <h:selectBooleanCheckbox id="firstCheckbox" value="..." />
        </p:column>
        <p:column>
            <h:selectBooleanCheckbox id="secondCheckbox" value="..." />
        </p:column>
    </p:dataTable>
</h:form>

Your checkboxes IDs will be:

  • For the first row myForm:myTable:0:firstCheckbox and myForm:myTable:0:secondCheckbox
  • For the second row myForm:myTable:1:firstCheckbox and myForm:myTable:1:secondCheckbox

and so on.

Unless you want your custom id format, you can always create your own facelets taglib functions. Based on this tutorial or this answer you can create your own id generator method, it would be something like this:

public static String generateId(String value, Integer index) {
    return value + "_" + index;
}

Then use it in your page:

xmlns:fnc="http://yournamespace.com/fnc"
...
<h:form id="myForm">
    <p:dataTable id="myTable" value="..." var="item" rowIndexVar="index">
        <p:column>
            <h:selectBooleanCheckbox id="#{fnc:generateId(item.name, index)}" value="..." />
        </p:column>
    </p:dataTable>
</h:form>

Just make sure you avoid duplicate IDs, by setting a unique value for each one, like I've used the loop index in my example.

Community
  • 1
  • 1
Qussay Najjar
  • 561
  • 3
  • 7
-1

I've just put an random name before the dynamic id! Like this :

<h:selectBooleanCheckbox  rendered="#{var1[5]}" id="random#{var1[4]}">

And then set something that you might use like :

$('#random#{var1[4]}').something();

Hope this works for you!