DataStateChange and DataSourceChanged not triggered

Dear support,


I have read all documentations :

https://ej2.syncfusion.com/vue/documentation/grid/data-binding/?_ga=2.5868531.1969696766.1625244187-2053838237.1623261141#custom-binding 

but I am pretty desperate.

I am unable to make the DataStateChange and DataSourceChanged work/get triggered.

1/ I don't understand how the "dataSourceChanged" work. In the example of the documentation it is not declared on the grid's props, neither on the mounted method, as the dataStateChange

2/ When I click on the "add" button of my grid, get my custom modal and click on the submit button, nothing happend, nothing is triggered.

I would like to trigger the dataSourceChanged, and then if the "state" is "add", call an API fonction to store my new datas.

3/ Moreover, I really dont get how the "state" variable works. Where is the state.action intialized ? Like here : 

dataSourceChanged: function (state) {
if (state.action === 'add') {
  this.orderService.addRecord(state).subscribe(() => state.endEdit());
} else if (state.action === 'edit') {
  this.orderService.updateRecord(state).subscribe(() => state.endEdit());
} else if (state.requestType === 'delete') {
  this.orderService.deleteRecord(state).subscribe(() => state.endEdit());
}


4/ Then How can I change the "save"/ cancel" button of the dialog wich is opened by the toolbar action from the grid ? 
Is it possible to make them not visible ? or Change the name ? 

Thank you in advance, 


Attachment: add_item_with_modal_89dbeae0.zip

3 Replies

TS Thiyagu Subramani Syncfusion Team July 13, 2021 03:17 AM UTC

Hi Sandra, 

Thanks for contacting Syncfusion support. 

Based on your shared information, when the custom binding approach is implemented in the Grid the dataStateChange(Grid action ( sorting, paging, filtering etc.))/dataSourceChanged(CRUD action(Add, Edit, Update, Delete)) event will be triggered for the corresponding action along with the query details returned in the event arguments perfectly but we unsuccessful to reproduce your reported issue at our end. The argument is accessed with the name ‘state’ in the documentation and it will return the corresponding action details from the source with which you can handle the action in your server and return back the response to the Grid. This case can be checked in the above shared sample for reference.  

So when the CRUD action is performed in the Grid, the action details will be returned in the dataSourceChanged event as shown in the below image, 

 

And in this event you can update the details in your server and on success of this action you need to call the state’s(The dataSourceChanged event argument) endEdit method in order to update the changes in the Grid(This will trigger the dataStateChange event which will fetch the updated data from the server).  

Here the “OrderService” mentioned in the specified line is a service file that is written for handling the actions like, CRUD, Sort, Page, etc. in the Grid. So we have prepared a Grid sample with local server(for fetching data) to demonstrate this case for your reference. You can find it below, 


Note: Before launching the above application, the express server needs to be started using the ‘node server.js’ command from the application’s root folder and then the Vue application can be launched using ‘npm run dev’ command. These actions need to be performed after installing the node modules. 

The dataSourceChanged is a Grid event so you can define it same like the dataStateChange is defined which is demonstrated below for your reference, 

<template> 
    <ejs-grid ref="grid" :dataSource="data" :dataSourceChanged="dataSourceChanged" :dataStateChange="dataStateChange"></ejs-grid> 
</template> 
<script> 
import Vue from "vue"; 
import { GridPlugin, Toolbar, Edit } from "@syncfusion/ej2-vue-grids"; 
import { getOrders, addOrder, updateOrder, deleteOrder } from "./orderService"; 
 
Vue.use(GridPlugin); 
 
export default { 
  name: 'app', 
  data() { 
              ... 
  }, 
  methods: { 
    dataStateChange: function (state) { 
              ... 
    }, 
    dataSourceChanged: function (state) { 
              ... 
    } 
  } 
} 
</script> 

For using custom binding, you need to bind the response data(Grid data) returned from your API as an object of result(JSON data source) and count(Total data count) properties and set it to the Grid’s dataSource property. On setting the data source in this format, the ‘dataStateChange’ event will be triggered with corresponding query for every Grid action performed like Paging, Sorting and Filtering.., and the ‘dataSourceChanged’ event will be triggered while performing CRUD action in Grid. So using this you can send the queries in your required format to your API, process and return the result and then assign the returned result to the Grid’s dataSource property as an object of result and count properties. 

More details on custom binding with online demo sample can be checked in the below links, 




Note:  The ‘dataStateChange’ event will not be triggered on Grid initial render. So for this case you need to return the data in the above mentioned format on initialization and assign it to the Grid’s dataSource property. 
 
If still facing the issue please share your issue reproducing sample or reproduce the issue above provided sample, Syncfusion package version, and video demonstration of your issue then only we provide the appropriate solution as soon as possible. 

Please get back to us if your require any further assistance. 

Regards, 
Thiyagu S 



SB Sandra Bordier July 13, 2021 12:15 PM UTC

Thank you for your answer.

I have a problem in the code, the "dataSource.json" file does not seem to be included anywhere in the App.vue, so I can't ahev any data in the grid.

Moreover, it seems like all the import needed are not present.
I have an error with the "dataSourceChanged" which is not included in the import, whereas in the following documentation : https://ej2.syncfusion.com/vue/documentation/grid/data-binding/

you have the " import { GridPlugin, DataStateChangeEventArgs, Sorts, Sort, Group, Page, DataResult, Toolbar, Edit } from "@syncfusion/ej2-vue-grids" import.

Is it possible to have an example with EVERYTHING that is needed ? Datasource , import, etc ...

So, instead of the zip file :

https://www.syncfusion.com/downloads/support/forum/166918/ze/GridCustomDataBinding722982509

Is it possible to have an online example like this one ?

Sample link: https://codesandbox.io/s/form-validation-forked-2263r?file=/src/App.vue


It is easier for me to check the code when it is already online, this way I am sure that evrything needed is included.

Moreover, can you please give me a full example of this : "The argument is accessed with the name ‘state’ in the documentation and it will return the corresponding action details from the source with which you can handle the action in your server and return back the response to the Grid. This case can be checked in the above shared sample for reference.  "
=> I don't understand how I have to do that ? In your zip.file, the "action" attribute is not set in the orderService.ts file, so I don't know/can't see how to do it.


best regards :)



SK Sujith Kumar Rajkumar Syncfusion Team July 15, 2021 12:32 PM UTC

Hi Sandra, 
 
We had shared you a local Vue sample for your requirement since we have created a local server to fetch and update data for the Grid. We will not be able to demonstrate the CRUD action case with online sample since it is not handled in our hosted data service which is why we shared the local sample. So we have explained how the custom binding works in the previously shared sample and also addressed your queries below, 
 
Query – 1: “I have a problem in the code, the "dataSource.json" file does not seem to be included anywhere in the App.vue, so I can't ahev any data in the grid.” 
 
As mentioned above we have used a local node server for fetching and updating the data in the shared sample, so initially you need to start this server using the following command – “node server.js”. This file is present in the root folder of the sample and in that file we have accessed the dataSource.json file as shown in the below image, 
 
 
 
So in the Vue mounted event we have manually invoked the dataStateChange event handler(defined for the Grid’s dataStateChange event) from where the action for retrieving data(written in the OrderService.ts file) is called. 
 
This is demonstrated below, 
 
mounted() { 
    // Used vue mounted hook to send initial request for fetching data from server 
    let state = { skip: 0, take: 12 }; 
    this.dataStateChange(state); 
}, 
methods: { 
    dataStateChange: function (state) { 
        getOrders().then(gridData => { 
                ... 
        }); 
    }, 
} 
 
 
The getOrders method called above(written in the OrderService file) will make the server request for fetching the data from the hosted service(which in this case is the local node server that is launched initially) and returning it back. So its URL is provided for fetching data as shown in the below code snippet, 
 
const baseUrl = http://localhost:8080/api; 
 
// get 
export function getOrders() { 
    return fetch(baseUrl + "/orders").then(res => res.json()).then(data => { 
        // Response data is returned back 
        return data; 
    }); 
} 
 
 
The returned data is finally set as the Grid’s dataSource property value as demonstrated below, 
 
<ejs-grid :dataSource="data" :dataStateChange="dataStateChange"></ejs-grid> 
 
<script> 
          ... 
    methods: { 
        dataStateChange: function (state) { 
            getOrders().then(gridData => { 
                this.data = gridData; 
            }); 
        }, 
    } 
</script> 
 
 
Query – 2: “Moreover, it seems like all the import needed are not present. I have an error with the ‘dataSourceChanged’ which is not included in the import, you have the import  DataStateChangeEventArgs,” 
 
The ‘DataStateChangeEventArgs’ is just the argument type of the dataStateChange event arguments and it can be referenced like below, 
 
import { GridPlugin, DataStateChangeEventArgs } from "@syncfusion/ej2-vue-grids"; 
 
methods: { 
    dataStateChange: function (state: DataStateChangeEventArgs) { 
        getOrders().then(gridData => { 
            this.data = gridData; 
        }); 
    }, 
} 
 
 
This is only used for preventing type mismatch issues while accessing the event arguments. 
 
Query – 3: “Moreover, can you please give me a full example of this. I don't understand how I have to do that ? In your zip.file, the "action" attribute is not set in the orderService.ts file, so I don't know/can't see how to do it.” 
 
By “action” details we just meant the corresponding Grid action(Page, Sort, etc.) queries that is returned on performing the corresponding action. And it does not mean an argument with the name of action will be received in the event. 
 
Note: We have used the service - ‘OrderService’ here to handle these actions but you do not have to use the same thing as you can use your own approach and handle these cases. 
 
For your reference, please follow the below steps to launch the previously shared sample for understanding the workflow implemented here, 
 
  • Install npm packages.
  • Run the following command in the sample root folder – “node server.js”.
  • Then, run the sample application using the following command – “npm run dev”.
 
Let us know if you have any concerns. 
 
Regards, 
Sujith R 


Loader.
Up arrow icon