Page 1 of 1

How to check whether a DataTable has one row to be selected?

Posted: 17 Nov 2010, 01:36
by keating
When click a button, how to check whether a DataTable has one row selected?

If there is one row selected, show a dialog; else show a message "please select one row".

But now, I write the code which was shown in

http://www.primefaces.org/showcase/ui/d ... Single.jsf (primefaces' showcase)

Code: Select all

        <f:facet name="footer">  
            <p:commandButton value="View" image="ui-icon ui-icon-search"  
                    update="form:display" oncomplete="carDialog.show()"/>  
        </f:facet>  
If there is not a row selected, the dialog will always be shown.

Re: How to check whether a DataTable has one row to be selec

Posted: 17 Nov 2010, 02:14
by callahan
One approach would be to use callback parameters.

Code: Select all

<f:facet name="footer"> 
  <p:commandButton value="View" image="ui-icon ui-icon-search" 
    action="#{theBean.handleViewClicked}" update="form:display" oncomplete="handleViewComplete(xhr, status, args)"/> 
</f:facet>
        
public String handleViewClicked() {
    RequestContext.getCurrentInstance().addCallbackParam("isCarSelected", getSelectedCar() != null);
    return null;
}
    
<script type="text/javascript">
  function handleViewComplete(xhr, status, args) {
    if(args.isCarSelected)
      carDialog.show();
    else
      alert("Please select a car.");
  }
</script>
Another approach would be to define a widgetVar for the dataTable and use the widgetVars undocumented selection array to find out if something is selected in JavaScript. If something is selected call carDialog.show(), if not, tell the user to select something first.The selection array is empty if nothing is selected.