Karim Oukara Karim Oukara - 2 months ago 57
Ajax Question

how to get the selected cell in <p:dataTable primefaces?

I want to get, if possible, the selected cell or colomne (id) of primefaces

<p:dataTable


my table is like :

<p:dataTable id="table" var="list" value="#{bean.list}" rowKey="#{list}" selectionMode="single" >
<p:ajax event="rowSelect" listener="#{bean.onRowSelect}" />
<p:column headerText="Date" >
<h:outputText value="#{list.SDate}" />
</p:column>
<p:column headerText="Name" >
<h:outputText value="#{list.IName}" />
</p:column>
</p:dataTable>


with this method I can get the row selected (line) using
<p:ajax event="rowSelect" listener="#{bean.onRowSelect}" />

but I can't get the selected colomn "
Date
" or "
Name
"

onrowSelecte method is like :

public void onRowSelect(SelectEvent event)
myObject obj = (myObject)event.getObject();
//.......
}

SRy SRy
Answer

You can do something like this to get the value of the specific column

<p:dataTable id="firsttable" var="list" value="#{bean.list}" rowKey="#{list}" selectionMode="single" >
     <p:column headerText="Date" >
        <h:outputText  value="#{list.SDate}" />
    </p:column>
    <p:column headerText="Name" >
        <h:outputText value="#{list.IName}" />
    </p:column>
</p:dataTable>

// This is capture the value of selected column
<h:inputText id="selectedId" value="#{bean.selectedColumn}" style="display:none">
       <f:ajax listener="#{bean.onRowSelect}"></f:ajax>
</h:inputText>

This script captures the values of the selected row and sets the inputHidden

 jQuery.noConflict();
    $(window).load(function () {
         $(document).delegate("#firsttable td", "click", function (event) {
             var columnNumber = jQuery(this).index();//get index of clicked row
         var colval=jQuery(this).find('div span').text()); // get the column value
         $("#selectedId").val(colval); //set value in the inputtext
         $("#selectedId").change(); //this will trigger the ajax listener
       });
    });

And In the bean define property to get the input text value

 String selectedColumn;

 public void onRowSelect(AjaxBehaviorEvent event) {
         String value=getSelectedColumn();
         System.out.println(value);
}