How to get selected row of a p:datatable direclty on selecting the row? - jsf

I want to get the selected row of my dataTable, directly if the user selects a row.
I have added
<p:dataTable id="dataTableID" var="row"
value="#{bean.value}"
rowKey="${row.id}"
selection="#{bean.selectedValue}" selectionMode="single">
It is just possible to get it after clicking on a button.

There are two ajax-events for instant row selection in primefaces dataTable. One for the selection and one for the unselection.
<p:dataTable ..>
<p:ajax event="rowSelect" listener="#{yourBean.someListener}"/>
...
</p:dataTable>
Now you can access the selected item (of class Foo) like this:
public void someListener(SelectEvent event) {
(Foo) event.getObject() // cast "Object" to "Foo"
}
For additional information take a look at the primefaces showcase first: http://www.primefaces.org/showcase/ui/datatableRowSelectionInstant.jsf

Related

get selected row in ace:datatable Icefaces

I am using an in rowselection mode and trying just to get the selected row of the datatable.
I have tried it using the stateMap of IceFaces, but it does not work. The ajax event opens a dialog after selection, where I want to show data of the selected row.
<ace:dataTable id="datatable"
value="#{myBean.myValues()}"
var="myValue" paginator="true" paginatorPosition="bottom"
selectionMode="single" rows="15" rowKey="#{myValue.id}"
doubleClickSelect="true">
<ace:ajax event="select" render="#this" execute="#this"
onStart="ice.ace.instance('#{myDialog.clientId}').show();" />
...
You can add rowSelectListener to the tag <ace:dataTable>
<ace:dataTable rowSelectListener="#{manageBean.rowSelectListener1}" >
And add a function in the code behind to get the row selected.
public void rowSelectListener1(SelectEvent event){
DataType selectedItem = (DataType )event.getObject();
}

Datatable selection - inputTextArea doesn't have up-to-date contents

Use case
I have a dialog that contains a datatable and an inputTextArea. The datatable is filled when the user clicks a button. When some (single) row of the datatable is selected, I want the contents of this row to be inserted at the end of the inputTextArea.
Problem
When using the selection attribute of the datatable, I dont have the most recent contents in the inputTextArea. Scenario: I enter "ABC" in my inputTextArea, then click the button, then make my inputTextArea "FFF" and select a row ("dtText") from the datatable. The new contents of the inputTextArea is "ABC dtText" instead of "FFF dtText".
What I tried
I tried to add a new listener but I cannot figure out how to bring it in properly so that it is being called before the selection happens. Found nothing on google or in the PF User Guide.
I was hoping the blur event updates my ruleText, but it doesn't happen.
inputTextArea
<p:inputTextarea value="#{mrBean.selectedElement.ruleTxt}" id="rt1New" rows="20" cols="100" autoResize="false">
<f:ajax event="blur" update="duoDlgForm2" />
</p:inputTextarea>
datatable
<p:dataTable id="qPdt" var="p" value="#{regelBean.queriedParams}" rowKey="#{p}"
selection="#{mrBean.selectedParam}" selectionMode="single">
<p:ajax event="rowSelect" update="#form"/>
<p:column ...
java
public void setSelectedParam(ParamOrDBParamModel selectedParam) {
if(selectedParam != null) {
selectedElement.setRuleTxt(selectedElement.getRuleTxt() + "\n" + selectedParam.getName().trim());
How can I work with the most recent text (selectedElement.getRuleTxt) when I am inside the setSelectedParam method?
PF 4.0
EDIT: if I try jquery, what do I have to insert into my function
$('#qPdt tr').on('click', function() {
// I reach this point, now what? How to get the contents of a row when I know the type?
$("#rt1New").append( ?? )
});

How to add double click listener to primefaces datatable

What i want to do is; when user clicks to row, it will select the row.When user double clicks to row, it will start cell editing. At Primefaces showcase(http://www.primefaces.org/showcase/ui/d ... nstant.jsf) it says "Instant row selection, dblclick selection and unselection is implemented using ajax behaviors." but i couldnt find where they implemented dblclick selection. Is there a way to start cell editing event with double click event?
<p:ajax event="rowDblselect">
From PrimeFaces Users Guide
Use
<p:ajax event="rowDblselect" />
in your <p:dataTable /> like this:
<p:dataTable
id="yourTableId"
value="#{yourBean.items}"
selectionMode="single"
selection="#{yourBean.selectedItem}"
var="item"
rowKey="#{item.id}">
<p:ajax
event="rowDblselect"
listener="#{yourBean.onRowDoubleClick}"
update="#form:theComponentYouWantToUpdate"
global="false" />
<!-- your columns here -->
</p:dataTable>
In your bean/controller use:
import org.primefaces.event.SelectEvent;
public void onRowDoubleClick(final SelectEvent event) {
YourObject obj = (YourObject) event.getObject();
// rest of your logic
}
Try setting dblClickSelect="true" on your table.
From the documentation:
By default, row based selection is enabled by click event, enable dblClickSelect so that clicking double on a row does the selection.

How to get selected datatable row using icefaces?

I want to get selected row data from ice:datatable ? Here is me code , please give me your suggestion to get selected row data using rowselector.
Backingbean:
public void rowSelectionListener(RowSelectorEvent event) {
System.out.println(event.getRow());
}
jspx code:
<ice:rowSelector id="selected" selectionListener="#{inventoryList.rowSelectionListener}" multiple="false" selectedClass="tableRowSelected" mouseOverClass="tableRowMouseOver" />
<f:facet name="header">
<ice:outputText value="Stock #"/>
</f:facet>
<ice:outputText value="#{item.stock}"/>
</ice:column>
Now I am able to get row index, but I need to get selected row value. How can I do that?
You can get the desired row by binding the table(HtmlDataTable) in backing bean, and on action or some other event you can get the selected row by tableBinding.getRowData() returning the object from the list that was used in table.

JSF datatable row selection

How to select multiple rows in a JSF datatable?
You can either place a checkbox on each row giving it a value of #{currentRowItem.selected} or use <rich:extendedDataTable>
Create a Map<Integer, Boolean> selectMap in the backing bean. (If the key is not model's id and key is a string, create a Map with String, Boolean pair ..)
For every row provide a checkbox #{backingBean.selectMap[rowvar.selectedId]}
You must be able to find the selected values in the map after each selection.
you can use primefaces as follow:
<p:dataTable id="tableId" var="data" value="#{myBean.myList}"
selection="#{myBean.selectedDTOs}"
rowKey="#{data.objectId}">
<p:column selectionMode="multiple" />
</p:dataTable>

Resources