@(Html.EJ().Grid<OrdersView>("Grid")
.AllowReordering()
.ClientSideEvents(e=>e.ActionBegin("actionBegin").Load("load"))
.EnablePersistence()
.Columns(col =>
{
. . .
})
)
<script type="text/javascript">
//Load event
function load(args) {
//add the columns property to ignoreOnExport method
this.ignoreOnPersist("columns");
}
</script> |
Hi Venkatesh,
Thanks for the response. For number 1 that isn't an option, the users want column ordering to be persistent until a reset button is clicked to explicitly reset the column ordering. Additionally disabling a feature isn't a better way to accomplish something.
As for number two, I have tried that and it throws the exact same error. The code I'm trying to get to work is as follows:
var grid = $("#" + gridId);
grid.ejWaitingPopup("show");
//Get the grid object
var gridInstance = grid.data("ejGrid");
//Get the original columns from the unobtrusive settings
var originalColumns = ej.parseJSON(grid.data("ej-columns"));
The error I get, whether I use JSON.parse or ej.parseJSON is "JSON.parse: unexpected character at line 1 column 251 of the JSON data". At character 251 is some non-standard JSON, the call to isJSON. The JSON in question looks like this:
"dataSource":ej.isJSON([{"Id":5,"Name":"EPSI"}])
As you can see, that isn't standard JSON. I need a way to parse this so that I can properly reset the column order while maintaining persistence.
@Grid
@(Html.EJ().Grid<object>("FlatGrid")
.Datasource((IEnumerable<object>)ViewBag.datasource)
. . .
.AllowResizing()
.AllowReordering()
.EnablePersistence()
.PageSettings(p=>p.PageSize(5))
. . .
.ClientSideEvents(eve => eve.Create("create").Load("onLoad"))
.Columns(col =>
{
. . .
})
)
@load event
function onLoad(args) {
if (window.localStorage.getItem("Columns") == null)
window.localStorage.setItem("Columns", JSON.stringify(this.model.columns));//store the grid columns in local storage variable
}
@button click event
$("#restore").ejButton({
click: function () {
var gridobj = $("#FlatGrid").ejGrid("instance"); // Create a instance for Grid
gridobj.deleteState();// Delete the persistence State
gridobj.destroy();// Destroy the grid
var gridModel = JSON.parse(window.localStorage.getItem("GridModel"));
gridModel.columns = JSON.parse(window.localStorage.getItem("Columns")); // get the initial grid columns from local storage and assign into gridmodel which is previously stored in the create event
$("#FlatGrid").ejGrid(JSON.parse(window.localStorage.getItem("GridModel")));//rerender the grid which is intially loaded
}
}) |
function buttonClick(args) {
var grid = $("#Grid");
grid.ejWaitingPopup("show");
//Get the grid object
var gridInstance = grid.data("ejGrid");
//Get the original columns from the unobtrusive settings
var originalColumns = grid.data("ej-columns");
gridInstance.columns(originalColumns);//bound the original columns through grid columns method
} |
Thanks for the answers, unfortunately I've tried both of these before and had issues with them.
For option one, if I reorder the columns then return to the page and check the columns array returned by 'this.model.columns' the columns are not in the original order but rather in the last ordered state.
For example: If my set of columns is [Column_1, Column_2, Column_3] and I reorder them to be [Column_2, Column_1, Column_3] and I reload the page, the array I get in the load event is [Column_2, Column_1, Column_3].
For the second option, I tried this on a page that didn't have the strange non-standard JSON and sometimes I ended up causing the grid to render incorrectly and lose columns. Not sure why that happened. When I tried this method with the non-standard JSON it would just dump the whole string into the column header.
Just so you can see exactly what I'm looking at I've attached a zip file with the screenshot of the column header being populated with the JSON and a dump of the string that I get back from 'var originalColumns = grid.data("ej-columns");'
Any ideas on how I can get this working would be greatly appreciated, this issue is holding up releasing the persistence feature.
I took a look at the ej.unobtrusive.js file and finally figured out how it parses this non-standard JSON. It turns out that it just runs `exec` on the string. Kind of dirty but it works. I got my code working by simply adding:
if(typeof(originalColumns) === "string")
originalColumns = exec(originalColumns);
Again, this seems really dirty and asking for someone to inject malicious code but it gets the job done.
<input type="button" id="restore" value="ResetColumns" />
<input type="button" value="unobtrusive" onclick="uClick()" />
@(Html.EJ().Grid<OrdersView>("FlatGrid")
.Datasource((IEnumerable<OrdersView>)ViewBag.datasource)
.AllowReordering()
.AllowPaging()
.EnablePersistence()
.ClientSideEvents(e=>e.Load("onLoad"))
.Columns(col =>
{
. . .
})
)
function onLoad(args) {
if (window.localStorage.getItem("Columns") == null)
window.localStorage.setItem("Columns", JSON.stringify(this.model.columns));//store the grid Original ordered columns in local storage variable while initial rendering
} |
$("#restore").ejButton({
click: function () {
var gridModel = JSON.parse(window.localStorage.$ej$ejGridFlatGrid); //get the grid Model
gridModel.columns = JSON.parse(window.localStorage.getItem("Columns")); // get the initial grid columns from local storage and assign into gridmodel which is previously stored in the create event
$("#FlatGrid").ejGrid(gridModel);//rerender the grid which is intially loaded columns
}
}) |
<input type="button" value="unobtrusive" onclick="uClick()" />
@(Html.EJ().Grid<OrdersView>("FlatGrid")
//.Datasource(ds => ds.URL(@Url.Action("DataSource"))
//.Adaptor(AdaptorType.UrlAdaptor))
.Datasource((IEnumerable<OrdersView>)ViewBag.datasource)
.AllowReordering()
.AllowPaging()
.EnablePersistence()
.Columns(col =>
{
. . .
})
)
<script type="text/javascript">
. . .
function uClick() {
var grid = $("#FlatGrid"); //GridID
var gridObj = grid.ejGrid("instance");//Grid Instance
var originalColumns = grid.data("ej-columns");
var gridModel= JSON.parse(window.localStorage.$ej$ejGridFlatGrid)
gridModel.columns = originalColumns;
$("#FlatGrid").ejGrid(gridModel);
}
|
<script type="text/javascript">
ej.persistStateVersion = "test";//any string value can be set
</script> |