Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I initially hide columns in a p:dataTable with p:columnToggler

I'm using PrimeFaces v.5 with this version a new component is released that ColumnToggler, when view is rendered, refreshed all checkbox are checked as a default operation.

What I need to do is;

  • to uncheck some columns when I initialize the view,
  • make p:columnToggler remember checked and unchecked options when a refresh operation occurs on p:dataTable
like image 481
Ismail Sahin Avatar asked Aug 25 '14 13:08

Ismail Sahin


People also ask

How to hide column visibility in DataTable?

DataTables and show and hide columns dynamically through use of this option and the column(). visible() / columns(). visible() methods. This option can be used to get the initial visibility state of the column, with the API methods used to alter that state at a later time.

How to hide column in DataTable javascript?

$(document). ready(function () { $('#example'). DataTable({ columnDefs: [ { target: 2, visible: false, searchable: false, }, { target: 3, visible: false, }, ], }); });


1 Answers

In Primefaces 5.2 you can set the p:column visible attribute to false

<p:column ... visible="false">

You can ofcourse use EL in the visible attribute by colum index (reordering becomes more difficult)

<p:column ... visible="#{visibilityModel.visibleList[1]}">

It hides the column at the beginning depending on the return value and you can show/hide the column through the columnToggler checkbox

By using the ajax toggle event

<p:ajax event="toggle" listener="#{viewBean.onToggle}" />

You can update the state of the visibilityModel server side

public void onToggle(ToggleEvent e) {
    list.set((Integer) e.getData(), e.getVisibility() == Visibility.VISIBLE);
}

See this PrimeFaces blog entry for full example to actually keep/store the state of the visibility server side so it can be reused later

like image 182
John Alexander Betts Avatar answered Oct 12 '22 12:10

John Alexander Betts