Hi experts,
I'm studying grid's feature persist . http://igniteui.com/grid/feature-persistence
I see we can persist all features on page. But how can we persist features between postbacks? Is there a way to save the feature to database, and restore it next time we open the same page? Thanks in advance.
Hello Ming,
Thank you for posting in our community.
What I can suggest for achieving your requirement is getting the state of the features that you would like to persist using every features columnSettings option. This could be done on the relevant event for each feature such as dataFiltered, columnSorted etc. In these events you could store the current columnSettings in a localStrage varable. With local storage web applications can store data locally within the users browser. For example:
$(document).ready(function () { if (localStorage) { loadUserSettings(); } function loadUserSettings() { var tempSortCol = localStorage.getItem('igGridSortedColumns'); var tempFilterData = localStorage.getItem('igGridFilterData'); var tempCurrPage = localStorage.getItem('igGridCurrPage'); if (tempSortCol) { igGridSortedColumns = JSON.parse(tempSortCol); } if (tempFilterData) { igGridFilterData = JSON.parse(tempFilterData); } if (tempCurrPage) { igGridCurrPage = tempCurrPage; } } $('#grid1').igGrid({ height: '550px', dataSource: employees, features: [ { name: 'Sorting', type: 'local', mode: 'single', columnSorted: function (evt, ui) { var columnSettings = $('#grid1').igGridSorting('option', 'columnSettings'); localStorage.setItem('igGridSortedColumns', JSON.stringify(columnSettings)); } }, { name: 'Filtering', dataFiltered: function (evt, ui) { var filteringSettings = $('#grid1').data('igGrid').dataSource.settings.filtering.expressions; localStorage.setItem('igGridFilterData', JSON.stringify(filteringSettings)); } }, { name: 'Paging', type: 'local', pageSize: 10, pageIndexChanged: function (evt, ui) { localStorage.setItem('igGridCurrPage', ui.pageIndex); } } ] }); if (igGridSortedColumns.length > 0) { for (var i = 0; i < igGridSortedColumns.length; i++) { if (igGridSortedColumns[i].currentSortDirection) { $('#grid1').igGridSorting('sortColumn', igGridSortedColumns[i].columnIndex, igGridSortedColumns[i].currentSortDirection); } } } if (igGridFilterData.length > 0) { $('#grid1').igGridFiltering('filter', igGridFilterData, true); } else { if (igGridCurrPage >= 0) { $('#grid1').igGridPaging('pageIndex', parseInt(igGridCurrPage)); } } });
$(document).ready(function () { if (localStorage) { loadUserSettings(); }
function loadUserSettings() { var tempSortCol = localStorage.getItem('igGridSortedColumns'); var tempFilterData = localStorage.getItem('igGridFilterData'); var tempCurrPage = localStorage.getItem('igGridCurrPage');
if (tempSortCol) { igGridSortedColumns = JSON.parse(tempSortCol); }
if (tempFilterData) { igGridFilterData = JSON.parse(tempFilterData); }
if (tempCurrPage) { igGridCurrPage = tempCurrPage; } }
$('#grid1').igGrid({ height: '550px', dataSource: employees, features: [ { name: 'Sorting', type: 'local', mode: 'single', columnSorted: function (evt, ui) { var columnSettings = $('#grid1').igGridSorting('option', 'columnSettings'); localStorage.setItem('igGridSortedColumns', JSON.stringify(columnSettings)); } }, { name: 'Filtering', dataFiltered: function (evt, ui) { var filteringSettings = $('#grid1').data('igGrid').dataSource.settings.filtering.expressions; localStorage.setItem('igGridFilterData', JSON.stringify(filteringSettings)); } }, { name: 'Paging', type: 'local', pageSize: 10, pageIndexChanged: function (evt, ui) { localStorage.setItem('igGridCurrPage', ui.pageIndex); } } ] }); if (igGridSortedColumns.length > 0) { for (var i = 0; i < igGridSortedColumns.length; i++) { if (igGridSortedColumns[i].currentSortDirection) { $('#grid1').igGridSorting('sortColumn', igGridSortedColumns[i].columnIndex, igGridSortedColumns[i].currentSortDirection); } } } if (igGridFilterData.length > 0) { $('#grid1').igGridFiltering('filter', igGridFilterData, true); } else { if (igGridCurrPage >= 0) { $('#grid1').igGridPaging('pageIndex', parseInt(igGridCurrPage)); } } });
Additionally, for Hiding,Resizing and ColumnMoving you could take information from columns option of igGrid.
I am also attaching a sample illustrating my suggestion for your reference.
Please let me know if you need any further assistance with thi smatter.
One way i figure out is using $("#grid").igGridColumnMoving to sort my columns, is there a better way to do this? Sorry for bothering you twice~
Thank you for getting back to me.
Since you are setting the autoGenerate option to false this means that you will have to provide column descriptions when initializing the grid. Alternatively, you could set this option to true and grid will initially generate it`s columns automatically. Afterwards, when you make some changes to column`s order, size etc. these changes will be reflected in the columns collection. Corresponding events should be handled in order to modify columns collection in local storage as well. If the variable storing columns in the grid exists in the localStorage(which means that some changes are made) you could set it to the columns option of the grid.
Regarding your second question I believe the ColumnMoving feature of the grid is what you are looking for If (as I assumed) you would like to change columns positions. If this is not exactly what you are trying to achieve please provide me some more details and I will investigate further.
I hope you find this information helpful.
Please let me know if you need any further assistance with this matter.
Hi Vasya,
Thank you for your reply. So based on your experience, if I set autoGenerate option to false, the only way I can use to restore column position is 1) providing a dummy column description when initialing the grid, and then 2) calling igGridColumnMoving to restore previous column positions?
And please forgive my interruption, can you please help me at this one as well... http://es.infragistics.com/community/forums/p/96178/475147.aspx#475147
You assumption is correct, if you set autoGenerateColumns to false you will have to initially provide columns for the grid. When column is moved igGrid columns collection is changed accordingly. Having this in mind, what I can suggest for restoring column order is handling columnMoved event. In this event you could save the current columns collection in the local storage and when loading the grid to set this new collection to the columns property of the grid. For example:
<script type="text/javascript"> var employees = []; for (var i = 0; i < 30; i++) { employees[i] = { "ID": i, "Name": "Name " + i, "Department": i % 8, "OnSite": i % 2 == 0 }; } var initColumns = [ { headerText: "ID", key: "ID", dataType: "number" }, { headerText: "Name", key: "Name", dataType: "string" }, { headerText: "Department", key: "Department", dataType: "number" }, { headerText: "OnSite", key: "OnSite", dataType: "bool" } ]; var igGridColumns = initColumns; $(document).ready(function () { if (localStorage) { var tempColumns = localStorage.getItem('igGridColumns'); if (tempColumns) { igGridColumns = JSON.parse(tempColumns); } } $('#grid1').igGrid({ height: '550px', dataSource: employees, autoGenerateColumns: false, columns: igGridColumns, features: [ { name: "MultiColumnHeaders" }, { name: "ColumnMoving", columnMoved: function (evt, args) { var movingColumnSettings = $("#grid1").igGrid('option', 'columns'); localStorage.setItem('igGridColumns', JSON.stringify(movingColumnSettings)); } } ] }); }); </script>
<script type="text/javascript"> var employees = []; for (var i = 0; i < 30; i++) { employees[i] = { "ID": i, "Name": "Name " + i, "Department": i % 8, "OnSite": i % 2 == 0 }; }
var initColumns = [ { headerText: "ID", key: "ID", dataType: "number" }, { headerText: "Name", key: "Name", dataType: "string" }, { headerText: "Department", key: "Department", dataType: "number" }, { headerText: "OnSite", key: "OnSite", dataType: "bool" } ];
var igGridColumns = initColumns; $(document).ready(function () { if (localStorage) { var tempColumns = localStorage.getItem('igGridColumns');
if (tempColumns) { igGridColumns = JSON.parse(tempColumns);
} }
$('#grid1').igGrid({ height: '550px', dataSource: employees, autoGenerateColumns: false, columns: igGridColumns, features: [ { name: "MultiColumnHeaders" }, { name: "ColumnMoving", columnMoved: function (evt, args) {
var movingColumnSettings = $("#grid1").igGrid('option', 'columns'); localStorage.setItem('igGridColumns', JSON.stringify(movingColumnSettings));
} } ] });
});
</script>
I made a small sample illustrating my suggestion and I am attaching it for your reference.
Regarding the thread that you mention my colleague Tsanna is working on this matter for you and she will get back to you soon with more information or questions fro you.
I got it. Thank you very much!
I am glad that you find my suggestion helpful.