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

UI Components for JSF
Post Reply
keating
Posts: 21
Joined: 08 Nov 2010, 15:18

17 Nov 2010, 01:36

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.

callahan
Posts: 768
Joined: 27 May 2010, 22:52

17 Nov 2010, 02:14

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.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 7 guests