2016-12-02 22 views
0

我有一個p:dataTableselectionMode=multiple結合的rowSelectrowUnselect事件:號碼:數據表selectAllRows API調用不會觸發rowSelect事件

<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> 

選擇行工作正常,myBean.selection更新並調用myBean.onSelect()

現在我想添加按鈕(un)將所有項目選擇到我的工具欄。我創建了兩個<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> 

的選擇似乎工作,我可以看到無論是所有項目(UN)選擇了。但是,更新/調用myBean.selectionmyBean.onSelect()都不是。我必須這樣做才能實現這個目標?

+0

我沒有真正的想法,但是我應該去調試一些東西來找出更多的東西,但是出於好奇,你**調試了什麼? Ajax調用?網絡流量?檢查PrimeFaces源代碼(包括js和java文件)。很多你可以做的事 – Kukeltje

+0

我檢查過,調用'selectAllRows()'時沒有Ajax-Call或網絡流量。我也嘗試在調用'selectAllRows()'之前或之後調用'PF('myDatatable')。fireRowSelectEvent()',但是我找不到正確的參數。 – Bob

+0

如果你使用了'commandButton',例如從頁面觸發一個調用(使用selectAllRows函數後)。例如。就像在'Multiple with Meta and Shift keys'的展示中一樣' – Kukeltje

回答

1

這兩個PrimeFaces javascript api調用不會與任何ajax事件進行交互。這可以在selectAllRows()unselectAllRows()的datatable.js中看到。如果您不使用ajax,而是通過按鈕進行常規「提交」,您會看到PrimeFaces將選擇擴展到服務器端的「全部」。爲此,它將選擇的@all值傳遞給服務器。

您還可以看到來源,已經有一些代碼發送「toggleSelect`AJAX事件,所以我把它的功能擴展的PrimeFaces數據表:

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); 
     } 
    } 
} 

調用後( un)selectAllRows()通過小部件,您可以調用此函數,也可以使用true(select)或false(unselect)值作爲參數。

如果再加入toggleSelect AJAX到你的數據表

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

,並在bean添加一個處理程序:

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

它所有的作品。在PrimeFaces datatable.js和結束(PrimeFaces 6.0,對代碼進行測試展示)

你也可以同時覆蓋(UN)selectAllRows致電fireToggleSelectEvent,因此所有在一個functioncall(或合併兩個單獨的呼叫一個自定義功能)。