p:datatable selectAllRows api calls don't trigger rowSelect Event

大憨熊 提交于 2019-12-13 08:50:42

问题


I have a p:dataTable with selectionMode=multiple which binds the rowSelect and rowUnselect event:

<p:dataTable 
    widgetVar="myDatatable" 
    selectionMode="multiple" 
    selection="#{myBean.selection}">
    <p:ajax event="rowSelect" listener="#{myBean.onSelect}" />
    <p:ajax event="rowUnselect" listener="#{myBean.onUnselect}" />
... (columns)
</p:dataTable>

Selecting rows works fine, myBean.selection is updated and myBean.onSelect() is invoked.

Now I wanted to add buttons to (un)select all items to my Toolbar. I created two <p:commandLink>s:

<p:commandLink onclick="PF('myDatatable').selectAllRows();" 
            update="actionbarForm">select all</p:commandLink>
<p:commandLink onclick="PF('myDatatable').unselectAllRows();" 
            update="actionbarForm">unselect all</p:commandLink>

The selection seems to work, I can see that either all items are (un)selected. However, neither myBean.selection nor myBean.onSelect() are updated/invoked. What do I have to do that to enable this?


回答1:


These two PrimeFaces javascript api calls do in no way interact with any of the ajax events. This can be seen in the datatable.js in selectAllRows() and unselectAllRows() If you do not use ajax but a normal 'submit' via a button, you'll see that PrimeFaces expands the selection to 'all' on the server side. For this it passes the @all value of the selection to the server.

You'll also see in the source that there already is some code to send a 'toggleSelect` ajax event, so I put it in a function extending the PrimeFaces datatable:

PrimeFaces.widget.DataTable.prototype.fireToggleSelectEvent = function(checked) {
    //fire toggleSelect event
    if(this.cfg.behaviors) {
        var toggleSelectBehavior = this.cfg.behaviors['toggleSelect'];

        if(toggleSelectBehavior) {
            var ext = {
                    params: [{name: this.id + '_checked', value: checked}
                ]
            };

            toggleSelectBehavior.call(this, ext);
        }
    }
}

After calling the (un)selectAllRows() via the widget, you can call this function too, with either a true (select) or false (unselect) value as the parameter.

If you then add the toggleSelect ajax to your datatable

<p:ajax event="toggleSelect" listener="#{dtSelectionView.onToggleSelect}" update=":form:msgs" />

and in your bean add a handler:

public void onToggleSelect(ToggleSelectEvent event) {
    FacesMessage msg = new FacesMessage(event.isSelected() ? "All Selected" : "All Unselected");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

It all works. (PrimeFaces 6.0, code tested against showcase)

You could also override both the (un)selectAllRows in the PrimeFaces datatable.js and at the end call the fireToggleSelectEvent, so all is in one functioncall (or combine both separate calls in one custom function).



来源:https://stackoverflow.com/questions/40934313/pdatatable-selectallrows-api-calls-dont-trigger-rowselect-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!