TableState for Table
It would be good to be able to remember the state of the table to be able to give the user continuity as they use an application. This state would include:
- sorts
- filters
- column widths (if resizeable)
- page number
- expanded state?
- toggled columns?
A nice clean API to get and set this would be great:
# template
<p-datatable (stateChanged)="onStateChanged($event)"
[initialState]="loadInitialState()"...
# component
onStateChanged(event: any) {
localStorage.set('table-state', event.data);
}
loadInitialState() {
return localStorage.get('table-state');
}
Or perhaps even a totally automatic API, like:
<p-datatable persistState="true"...
Would be a great feature. Automatically is a nice to have. Best for me if it exposed functions so can hookin and save to remote storage etc
Just looking at source now and it looks like accessing the sort/filter state is simple enough, just attach to onFilter/onSort events. There appears to be now way to set these values back.
Somewhat related, onSort fires the onFilter event even if filter has not changed, i.e. filter data (get filter event), sort data (get filter event then sort event). I think that filter event is redundant. minor issue though.
Is there any update on this issue. I am using the LazyLoad event to store the sort and filter information. I am using local storage (which isn't ideal - but it works). Now when i navigate away from the page with my datatable i can persist those values. Once i return to that page (onInit) i can get those values back from localstorage and use them in very next query (lazy load). HOWEVER, there seems to be no way to visually update the data table UI to show that those filters are being used or that the sort order on a column has changed.
Is there any way to update the datatable header fields on the UI?
This pull request solves this. It uses localStorage for lazy load. Everything is updated visually. For integration with MultiSelect for toggle columns, event onPersistentStateApplied should be used similar to this:
onPersistentStateApplied(event): void { if (!event.state || !event.state.visibleColumnsState) { return; }
let eventColumns = event.state.visibleColumnsState;
this.columns.splice(0, this.columns.length);
for (let i = 0;i < eventColumns.length;i++) {
let eventColumn = eventColumns[i];
this.columns.push({
field: eventColumn.field,
header: eventColumn.header,
style: eventColumn.style
});
}
let columnsOptionsByLabel = {};
for(let i = 0; i < this.columnOptions.length; i++) {
columnsOptionsByLabel[this.columnOptions[i].label] = this.columnOptions[i];
}
for(let j = 0; j < this.columns.length; j++) {
let columnOption = columnsOptionsByLabel[this.columns[j].header];
columnOption.value = this.columns[j];
}
}
Any news on when we might see this released?
This is on roadmap;
https://github.com/primefaces/primeng/wiki/Roadmap
Closing for now and will create a new ticket with details when we pick it up from roadmap after 1.0. At the moment, we're closing tickets that duplicate the upcoming features in roadmap as a part of issue tracker maintenance.
@cagataycivici thanks for the roadmap link - are there any dates pencilled in for when this feature will be released?
any update on this?
Planned for 2.1 due late march.
Similar to PrimeFaces TableState;
https://www.primefaces.org/showcase/ui/data/datatable/tableState.xhtml
@cagataycivici do you have a new planning for this feature?
Yes, but can't give a specific date right now. Rough estimation is in 2 months.
Love ngPrime but this is a major issue with an otherwise great data table. In the meantime, are there any events we could subscribe to and properties we could set that would allow us to implement this ourselves?
@TimKohler -- I solved this manually using something like this:
public gridOptions = {
first: 0,
rows: 10,
sortField: 'title',
sortOrder: 1
};
onSort(e: { field: string, order: number }) {
this.gridOptions.sortField = e.field;
this.gridOptions.sortOrder = e.order;
this.gridOptions.first = 0;
this.localStorageService.set('my-grid-options', this.gridOptions);
}
onPage(e: { first: number, rows: number }) {
this.gridOptions.rows = e.rows;
this.gridOptions.first = e.first;
this.localStorageService.set('my-grid-options', this.gridOptions);
}
ngOnInit() {
// after loading the table data then read the options and
// if options exist set them wrapped in setTimeout():
const opt = this.localStorageService.get('my-grid-options');
if (opt) {
setTimeout(() => {
this.gridOptions = opt;
this.dataTable.sortField = this.gridOptions.sortField;
this.dataTable.sortOrder = this.gridOptions.sortOrder;
this.dataTable.sortSingle();
this.dataTable.paginate(this.gridOptions);
this.blocked = false;
}, 0);
return;
}
}
The table looks like:
<p-dataTable #dt [value]="sets" (onRowClick)="select($event.data)" [paginator]="true"
paginatorPosition="both" [pageLinks]="10" [rows]="10" [rowsPerPageOptions]="[5,10,20,30,40]"
(onPage)="onPage($event)" (onSort)="onSort($event)">
@jamesgroat Thank you! This is a great workaround. Got it working well.
Yes, there are events to use until this is built-in.
I cannot get pagination to stick with the newest version of PrimeNG, since paginate() no longer accepts any arguments.
Does anyone have a solution? Here is my current code:
onPage(e: { first: number, rows: number }) {
console.log(this.dataTable.first);
this.gridOptions.rows = e.rows;
this.gridOptions.first = e.first;
localStorage.setItem(this.optionsKey, JSON.stringify(this.gridOptions));
}
const opt = localStorage.getItem(this.optionsKey);
const filters = localStorage.getItem("filters");
if (opt) {
let go = JSON.parse(opt);
setTimeout(() => {
this.gridOptions = go;
this.dataTable.sortField = this.gridOptions.sortField;
this.dataTable.sortOrder = this.gridOptions.sortOrder;
this.dataTable.sortSingle();
this.dataTable.first = this.gridOptions.first;
this.dataTable.rows = this.gridOptions.rows;
this.dataTable.paginate();
//this.dataTable.paginate(this.gridOptions);
if (filters) {
console.log(JSON.parse(filters));
this.dataTable.filters = JSON.parse(filters);
}
}, 0);
@blgrnboy Try this:
this.gridOptions = opt;
this.dataTable.sortField = this.gridOptions.sortField;
this.dataTable.sortOrder = this.gridOptions.sortOrder;
this.dataTable.sortSingle();
this.dataTable.onPageChange(this.gridOptions);
I don't think you need these:
this.dataTable.first = this.gridOptions.first;
this.dataTable.rows = this.gridOptions.rows;
@jamesgroat I tried that, with no success.
@blgrnboy make sure you are calling that code after you have loaded your data?