|
var savedProperties;
document.getElementById('save').addEventListener('click', function (args) {
// Persist data is stored in a global variable
savedProperties = JSON.parse(this.$refs.grid.ej2Instances.getPersistData());
});
document.getElementById('restore').addEventListener('click', function (args) {
// Stored persist data is restored to the grid
this.$refs.grid.ej2Instances.setProperties(savedProperties);
}); |
|
var savedProperties;
var savedColumns;
document.getElementById('save').addEventListener('click', function (args) {
// Persist data and column model are stored in global variable
savedProperties = JSON.parse(this.$refs.grid.ej2Instances.getPersistData());
savedColumns = Object.assign([],this.$refs.grid.ej2Instances.columns);
});
document.getElementById('restore').addEventListener('click', function (args) {
// Stored persist data and column model are restored to the grid
this.$refs.grid.ej2Instances.setProperties(savedProperties);
var restoreCols = Object.assign([], savedColumns);
this.$refs.grid.ej2Instances.columns = restoreCols;
}); |
|
var colValues;
document.getElementById("save").addEventListener("click", args => {
.
.
colObject = Object.assign([], grid.columns);
colObject.filter((col) => colValues.push({ "template": col.template, "headerText": col.headerText }))
savedColumns = JSON.stringify(colObject);
});
document.getElementById("restore").addEventListener("click", args => {
var columnObj = JSON.parse(savedColumns);
var i = 0;
while (i < columnObj.length) {
columnObj[i].template = colValues[i].template;
columnObj[i].headerText = colValues[i].headerText;
i++;
}
grid.setProperties(savedProperties);
grid.columns = columnObj;
}); |
I want to make sure I understand the concept:
1. The user goes to the windows with the grid. The first thing I do is store the columns' headerText, template and ValueAccessor in the localstorage as json.
colObject = Object.assign([], grid.columns);
colObject.filter((col) => colValues.push({ "template": col.template, "headerText": col.headerText }))
colValues should be stored as json into the localstorage
2. The user reorder the columns, sort column X and save this view (name: test) . I should save the persistData() (which is a string) to the db.
3. The user logs out and after a day he logs in.
4. The user go to the window with the grid. When that happens, point 1 above happens again, meaning updating localstorage with the 3 properties of all columns.
5. The user is selecting to show view "test". I take the persistData I stored previously and do .setProperties(...). This will change the grid columns (order/sort) but without headerText, template and valueaccessor.
6. I then should take the json from localStorage (point1) and update the columns:
var i = 0;
while (i < columnObj.length) {
columnObj[i].template = colValues[i].template;
columnObj[i].headerText = colValues[i].headerText;
i++;
}
question 1: Because getPersistData already contains the columns object, in the right order/sort, there is no need to store also the column, right?
question 2: Because the user enters the window with the default view but he can save as many views as he wants, the order of the columns may be different between the views and the columns order from point 1 (localstorage). How can I make sure that the correct column gets the correct template/headerText/ValueAccessor? Maybe based on the field name of the column?
question 3: If later on I will add a new column to the grid that was not saved in getPersistData, how will the grid behave after .setProperties(...) of an object without this new column?
Thanks
|
colObject.filter((col) => colValues.push({ "template": col.template, "headerText": col.headerText, "field": col.field, "valAcc": col.valueAccessor })) |
|
var initialAdd = true;
// Add new columns button click
document.getElementById('add').addEventListener('click', function(args) {
if (initialAdd) {
// New column is added to the Grid
grid.columns.push({ field: 'CustomerID', headerText: 'Customer ID' });
grid.refreshColumns();
initialAdd = false;
}
});
// Merge columns button click
document.getElementById('merge').addEventListener('click', function(args) {
// Initially persisted data
var persistData1 = JSON.parse(persistData);
// Grid column object is converted to array of column object
gridColumns = Object.entries(gridColumns).map(e => e[1]);
persistData1.columns[3].headerText = "Amount";
// Grid column object is extended with persisted columns
var newData = ej.base.extend(gridColumns, persistData1.columns);
// The extended data is assigned to the Grid columns
grid.setProperties({ columns: JSON.parse(JSON.stringify(newData)) })
}); |


|
saveBtnClick: function() {
// Persist data and column model are stored in global variable
this.savedProperties = [];
this.savedColumns = [];
this.savedProperties = Object.assign([], JSON.parse(this.$refs.grid.ej2Instances.getPersistData()));
this.savedColumns = Object.assign([], this.$refs.grid.ej2Instances.columns);
},
restoreBtnClick: function() {
// Stored persist data and column model are restored to the grid
this.$refs.grid.ej2Instances.setProperties(this.savedProperties);
this.$refs.grid.ej2Instances.columns = Object.assign([], this.savedColumns);
} |

Amos with this methodology do you have any issues with capturing the default view and columns if the user is directly navigating to a saved version of the grid first and switching to the default view after?