We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

State manual load and save

Hello,

How can I manually get and load state object for grid? My main goal is to save state object within SQL anda afterwards load it from there, but I don't see any documentation for that, neither properties which would help me to accomplish this. On top of that, I'm not able to use cookies ir localStorage as at the same time I might have multiple grids open with different states.

So again, how can I retrieve state object from Grid and how can I load it without cookies or localStorage?

13 Replies

ME Me June 7, 2017 09:42 AM UTC

Moreover, seems that column width is not saving / saved - always sames the same, when I check my test localStorage state object. Looks like grouping is not saving too, I'm using: 15.1.0.41


ME Me June 7, 2017 09:44 AM UTC

Another questions: Looks like there are no events where I could subscribe to catch state changes. What can you offer for that? (Timer with check isn't an option)


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team June 8, 2017 12:27 PM UTC

Hi Customer,  
 
Query #1: My main goal is to save state object within SQL  
 
We have already discussed about this “Saving/applying state from the SQL Table to the Grid” in the following KB.  
 
 
In this, we have discussed about saving the Grid model values to the SQL table and later it has been retrieved to populate the saved state back to the Grid. This approach complete independent of the browser cache and local storage or cookies.  
 
Query #2: Moreover, seems that column width is not saving / saved 
 
Using the above described method, you have save the values of the column width and apply them back to the Grid.  
 
Query #3: like there are no events where I could subscribe to catch state changes 
 
We have discussed about the event of the Grid in the following API Reference section.  
 
 
Explain your scenario in which are expecting the events. This would be helpful for us to provide a solution.  
 
Regards,  
Seeni Sakthi Kumar S. 



ME Me June 9, 2017 06:03 AM UTC

As I said, I need event to catch state changes. I want state automatically to be saved in SQL every time it changes.


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team June 12, 2017 12:51 PM UTC

Hi Customer,  
 
In the KB, Grid model where saved to the SQL Table in the DropDown change event. Can you please explain the scenario that you would like to save the state of the Grid Model to the Table? It would be helpful for us to provide you solution on this.  
 
Regards,  
Seeni Sakthi Kumar S.  



ME Me replied to Seeni Sakthi Kumar Seeni Raj June 14, 2017 06:59 AM UTC

Hi Customer,  
 
In the KB, Grid model where saved to the SQL Table in the DropDown change event. Can you please explain the scenario that you would like to save the state of the Grid Model to the Table? It would be helpful for us to provide you solution on this.  
 
Regards,  
Seeni Sakthi Kumar S.  


Ok, lets try again. I don't want to have specific buttons which will save state when user clicks on 'save'. What I need is the event which is called every time something changes in grid. For instance: You changed column width: state save event called, you moved column to different place : state save event called, you added new sorting : state event called, filter / search added: state save event called and so on. My state changes saving must be automatic and instant after anything changes within grid


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team June 15, 2017 12:33 PM UTC

Hi Customer,  
 
We have prepared a sample with the Angular2 Grid Saving its model to the SQL server on each action of the Grid. The sample can be downloaded from the following location.  
 
 
The sample is two parts, one is server-end with SQL server (in MVC application) which is handling the client-request another part includes the Client-end with the Angular2 Grid.  
 
We have collected the Grid state in the actionComplete and resizeEnd events of the Grid. Therefore, these events will collect and trigger the server actions which will save the state of the Grid in the SQL table.  
 
<ej-grid [allowPaging]="true" id="Grid" [allowSorting]="true" [allowFiltering]="true" (resizeEnd) ="onResizeEnd($event)" (actionComplete)="onactionComplete($event)" [allowGrouping]="true" [allowResizing]="true" [allowReordering]="true" [dataSource]="gridData"> 
    . . .  
           . . . 
</ej-grid> 
 
//Server end 
 
        public string Query(string gridObj) 
        { 
            con.Open(); 
            var time = DateTime.Now.TimeOfDay.ToString(); 
            //Inserting Grid object into StateStore Table with respective time 
            //States and CurrentTime are two columns in table 
            //States have the corresponding object whereas the CurrentTime as time 
            SqlCommand insert = new SqlCommand("INSERT INTO SaveModel (States,CurrentTime ) VALUES('" + gridObj + "','" + time + "')", con); 
            insert.ExecuteNonQuery(); 
            con.Close(); 
            return time;//can be saved in ejDropDownList 
        } 
 
export class GridComponent { 
    public gridData: any; 
    onactionComplete(e) { 
        var gridObj = $("#Grid").ejGrid("instance") 
        this.stateChanges(e, gridObj); 
    }; 
    onResizeEnd(e) { 
        var gridObj = $("#Grid").ejGrid("instance") 
        this.stateChanges(e, gridObj); 
    } 
    public stateChanges(args, proxy) { 
    if (args.requestType != "refresh") { 
        var columns = proxy.model.columns.slice(); 
        if (args.type == "resizeEnd") { 
           . . .  
               . . .  
            columns[inx].width = args.newWidth; 
        } 
        var state = { 
            cols: columns, 
            groupedCol: proxy.model.groupSettings.groupedColumns.slice(), 
            sortedCol: proxy.model.sortSettings.sortedColumns.slice(), 
            filteredCol: proxy.model.filterSettings.filteredColumns.slice() 
        } 
        var object = JSON.stringify(state);//converting object to string 
 
        $.ajax({ 
            type: "POST", 
            url: "http://localhost:50498/Home/Query", 
            data: { "gridObj": object },//posting the grid object as string 
            success: function (data, status, xhr) { 
                //On Success save the data which is the time 
           . . . . 
            }, 
        }); 
    } 
} 
} 
 
 
The localhost:50498 is the MVC Application’s PORT number. So, it is recommended to the run the Angular2 application after running the MVC application.  
 
Regards,  
Seeni Sakthi Kumar S. 



ME Me June 16, 2017 02:52 PM UTC

resizeEnd and actionComplete was what I was looking for, thanks!

Another question: Is there is any property which would prevent automatic data load after dataSource is set for ejGrid?
I know I could not set dataSource in the constructor, but in that case there are some errors as ejGrid model without dataSource is not created. What I would like to do is to have an ability to prevent auto data load into a grid if not needed. It's also useful if I want to load state first, set it and only then load grid data.


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team June 19, 2017 12:01 PM UTC

Hi Customer,  
 
We cannot prevent the dataSource from binding to the Grid but we can bind an empty dataSource to the Grid. Later, in any of the instance, we can update the Grid dataSource using the dataSource method. Refer to the following API Reference.  
 
 
appComponent.html 
 
<ej-grid id="OrdersGrid" [dataSource]="gridData"[allowPaging]="true [editSettings]= "edit"  [toolbarSettings]= "tool"> 
 
</ej-grid> 
 
appComponent.ts 

export class AppComponent { 
 
   public gridData = [] 
 
click(args) { 
            var obj = $("OrdersGrid").ejGrid("instance"); 
            obj.dataSource(ej.DataManager({ 
                url : "http://localhost:49339/Home/DataSource", 
                adaptor : "UrlAdaptor" 
            })) 
        } 
} 
 
 
 
Regards,  
Seeni Sakthi Kumar S. 



ME Me October 7, 2017 05:11 AM UTC

Hey,

With latest version not able anymore directly update grid state in this manner:

                    @ViewChild('myGrid') grid: EJComponents;

            grid.widget.model.groupSettings.groupedColumns = stateData.groupedColumns.slice(), 
            grid.widget.model.sortSettings.sortedColumns = stateData.sorterdColumns.slice(), 
            grid.widget.model.filterSettings.filteredColumns = stateData.filteredColumns.slice()

If you load more filters (for example 3) and then less (for example 2), on grid refresh it will load data with 3 filters. Also, then you load 2 filters and try to change third filter it gets buggy. Any solution?




VN Vignesh Natarajan Syncfusion Team October 9, 2017 04:07 PM UTC

Hi Customer, 

We have analyzed your query and we are not able to reproduce the mentioned issue. The sample provided in last update will store the Grid state for every action(sorting,filtering and grouping) in a dropdown and when clicking on ApplyState will reflect in the grid. We have checked with latest build and it was working fine. 

So Kindly Share the following details to help you better 
         
1.       Share the full Grid Codes  
2.       Share the issue reproducing sample (if possible) 
3.       Essential studio version  
 
 
Regards, 
Vignesh Natarajan 



ME Me October 10, 2017 12:49 PM UTC

Hello,

I prepared sample which is attached. Follow those steps:

  1. Set filter on CustomerId to be equal to 'ALFKI'.
  2. From DropDownList select 'State 1' and press Save
  3. On CustomerId column press 'Clear filter'
  4. Set 'Ship country' to be equal to 'Argentina' and then 'EmployeeId' to '2'.
  5. From DropDownList select 'State 1' and press 'Load'

No results will be loaded, because ejGrid shows only one filter set, but in reality - there are three. You can see that by checking ejGrid new get request :

Possible solution is to clear every filter before setting new state, but that is not an solution for large grid, as ejGrid will try to reload data after every filter reset - performance overload.

Any solutions?

P.S. After 'npm install' I updated Angular version to 'latest'.


Attachment: angular2_test_grid_filters_ed87327.zip


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team October 11, 2017 07:00 PM UTC

Hi Customer,  
 
We can reproduce the problem at our end.  
 
Since, the filter values were explicitly defined in the Grid, its sub control (Excel filter) values were not redefined completely which is the cause of the problem. So, we suggest to clear the excel filter values before refreshing the content. Refer to the following code example.  
 
    private onLoadStateClicked(args)  
    { 
        // Getting grid widget 
        var grid = this.myGrid.widget; 
 
        // Creating state object 
        var state = JSON.parse(this.getStateString()); 
         // Setting filters 
         grid.model.filterSettings.filteredColumns = state.filteredColumns.slice(); 
      . . . 
            . .  
         grid._excelFilter._predicates = []; 
         // Refreshing columns 
         grid.columns(state.columns.slice()); 
 
        console.log('State loaded with id: ' + this.dropDownListSelectedValue + ', state: ', state);         
    } 
 
Regards,  
Seeni Sakthi Kumar S. 


Loader.
Up arrow icon