1

I need to get the p:dataTable object. But seems like I am not able to get the p:dataTable object using the below.

<script type="text/javascript">    
    var table2 = document.getElementById('test2_datatable_staffs_2');
</script>

<h:form id="form1" prependId="false">
    <p:dataTable id="test2_datatable_staffs_2">

3 Answers 3

1

You can use widgetVar instead of id.

<p:dataTable widgetVar="test2_datatable_staffs_2">

You can access PrimeFaces elements in JavaScript the following way:

<script type="text/javascript">    
   var table2 = PF('test2_datatable_staffs_2');
</script>

Give this a try and tell me if it works.

0

The id of the h:form is being prepended to your p:dataTable

You can use the following code

    var table2 = document.getElementById('form1:test2_datatable_staffs_2');

Or just set the prependId=false for the h:form and use your current code

4
  • 1
    I have added prependId="false". But it returns me a div instead of the dataTable object.
    – bittersour
    May 27, 2014 at 8:06
  • That means that my answer is right :) , since the JSF <p:dataTable element is actually a HTML div with table inside.
    – Daniel
    May 27, 2014 at 8:20
  • the table tag inside the div won't have a id? I saw its only role=grid. Cause I need to get that table id. Not too sure if that is even possible.
    – bittersour
    May 27, 2014 at 8:52
  • It looks like you need rephrase a new question in which you should ask how to achieve your goal in general
    – Daniel
    May 27, 2014 at 9:04
0

that cause JSF put the form name before the table id

you should use

<script type="text/javascript">    
    var table2 = document.getElementById('form1\\:test2_datatable_staffs_2');
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.