Primefaces Datatable RowSelect Event

datatableeventsjsfprimefaces

I have the following in my xhtml

                <h:form id="clientTableForm" prependId="false">
                    <p:dataTable id="clientTable" widgetVar="clientTableVar"
                        var="client" value="#{resendEmailController.lazyDataModel}"
                        paginator="true" rows="15"
                        paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink}  {PageLinks} {NextPageLink} {LastPageLink} {CurrentPageReport}"
                        rowsPerPageTemplate="5,10,15,20,25,50,75,100"
                        paginatorPosition="bottom" pageLinks="5" lazy="true"
                        sortBy="#{client.cclnCode}" sortOrder="ascending"
                        selection="#{resendEmailController.selectedClient}"
                        selectionMode="single" filterDelay="500" scrollable="true"
                        scrollHeight="380">

                        <p:ajax event="rowSelect"
                            listener="#{resendEmailController.changeClient}"
                            update="_accountTableForm_accountTable" />



                        <p:column id="cclnCodeColumn" headerText="Client Code"
                            style="width:25%;" sortBy="#{client.cclnCode}"
                            filterBy="#{client.cclnCode}" filterMaxLength="10">
                            <h:outputText value="#{client.cclnCode}"
                                converter="#{trimStringConverter}" />
                        </p:column>

                        <p:column id="cclnNamenColumn" headerText="Client Name"
                            style="width:75%" sortBy="#{client.cclnName}"
                            filterBy="#{client.cclnName}" filterMaxLength="50">
                            <h:outputText value="#{client.cclnName}"
                                converter="#{trimStringConverter}" />
                        </p:column>
                    </p:dataTable>
                </h:form>
            </p:layoutUnit>

<script type="text/javascript">

        $(document).ready(function()
        {
            autoSelectClient();
        });


        function autoSelectClient()
        {
            if (clientTableVar.isEmpty() == false)
            {
                clientTableVar.selectRow(1, false);
            }
        }

        </script>

And I have this in my backing bean

    public void changeClient(SelectEvent selectEvent)
{
    ResendEmailClient client = (ResendEmailClient) selectEvent.getObject();

    selectedClient = client;

    String cclnCode = client.getCclnCode();

    selectedAccounts = getService().listAccounts(cclnCode);
}

I would just like to ask why the "selectedClient" variable in the backing bean is NULL when executed by the "autoSelectClient();". But if I clicked the rows the "selectedClient" is already set.

As you can see in my backing bean I can get the value I want by getting the object inside the SelectEvent but I just want to know what is the cause of the difference.

Also if possible can any also suggest how to replicate the emulate the second scenario so that the "selectedClient" is already set before the "changeClient()" is invoked.

Using
JSF 2.1
PrimeFaces 3.5
Mojarra 2.1

Best Solution

try to send your datatable id in process of your ajax selection event like this:

<p:ajax event="rowSelect" listener="#{resendEmailController.changeClient}" update="_accountTableForm_accountTable" process="clientTable" />

Because when you call your event the real situation of your selection has no sent.