BoldSignA modern eSignature application with affordable pricing. Sign up today for unlimited document usage!
I am trying to make a big grid test use case, and pack everything in to it. I have my Grid working ok, but as soon as I try to use Offline it all stops working. I am also programatically trying to add a button based on content in the API, but not sure where to start with that.
My issues:
1) When I use Offline, my Grid and a CustomAdapter looses all its pagination and sort and filter options.
2) I am trying to create a button (myButton) based on data within the API data.
<template>
<div class="container">
<h2>Title</h2>
<div >
<ejs-grid :dataSource='data' :allowSorting='true' :allowFiltering='true' :filterSettings='filterOptions' :allowPaging="true" :pageSettings='pageSettings' :toolbar='toolbarOptions' :commandClick='commandClick'>
<e-columns>
<e-column field='id' headerText='ID' textAlign='Right' width=75></e-column>
<e-column field='name' headerText='Name' width=120></e-column>
<e-column field='config' headerText='Config' width=150></e-column>
<e-column field='isTrueFalse' headerText='Active' width=150></e-column>
<e-column headerText='Commands' :commands='commands' width=100></e-column>
<e-column field='Sno' headerText='SNO' textAlign='Right' width=100></e-column>
<e-column field='myButton' headerText='Button' textAlign='Right' width=100></e-column>
</e-columns>
</ejs-grid>
</div>
</div>
</template>
<script>
import { Page, Sort, Filter, CommandColumn, Toolbar, Search } from "@syncfusion/ej2-vue-grids";
import { DataManager, WebApiAdaptor, Query } from "@syncfusion/ej2-data";
import { ButtonComponent } from "@syncfusion/ej2-vue-buttons";
class cleanUpTrueFalse extends WebApiAdaptor {
processResponse() {
let original = super.processResponse.apply(this, arguments);
//console.log(arguments);
console.log(original);
original.result.forEach((item) => item['Sno'] = 5);
original.result.forEach((item) => {
if(item['isTrueFalse'] == false){
item['isTrueFalse'] = "No";
item['myButton'] = "<ejs-button>Button - On Click ALert</ejs-button>";
} else {
item['isTrueFalse'] = "Yes";
item['myButton'] = "<ejs-button>Button - On Click Console Log</ejs-button>";
}
original.result.push(item);
});
return {result: original.result, count: original.count};
}
}
export default({
data() {
return {
data: new DataManager({
url: 'http://example.com/api/v1/data',
adaptor: new cleanUpTrueFalse(),
crossDomain: true,
offline: true,
onSuccess: console.log("Done")
}),
commands: [{ buttonOption: { cssClass: 'e-flat', iconCss: 'e-edit e-icons' } }
],
filterOptions: {
matchCase: false,
operator: 'contains',
predicate: 'and',
ignoreAccent: true,
type: 'Excel'
},
toolbarOptions: ['Search'],
pageSettings: { pageSize: 15 },
sortOptions: { columns: [{ field: 'name', direction: 'Ascending' }, { field: 'config', direction: 'Descending' }] },
query: new Query().addParams('aaaaaaa', 'true')
};
},
components: {
"ejs-button": ButtonComponent
},
methods: {
commandClick: function(args) {
console.log(JSON.stringify(args.rowData));
},
},
provide: {
grid: [Page, Sort, Filter, CommandColumn, Toolbar, Search]
}
});
</script>
<style>
@import "@syncfusion/ej2-material-theme/styles/material.css";
@import "@syncfusion/ej2-buttons/styles/material.css";
@import "@syncfusion/ej2-popups/styles/material.css";
@import "@syncfusion/ej2-navigations/styles/material.css";
@import "@syncfusion/ej2-vue-buttons/styles/material.css";
</style>
Hi Verum Genus,
Thanks for contacting Syncfusion support.
Query#1: When I use Offline, my Grid and a CustomAdapter looses all its pagination and sort and filter options.
In EJ2 DataManager, setting offline true will send the request for
initial data loading only. All the other data actions like filtering, sorting,
paging, etc. will be performed on the client side (offline). It will not send any
further requests to the server this is the default behavior. So, we suggest removing
the offline property assigned from the DataManager to achieve your requirement.
Or please share the purpose of using
the
“offline” property in your DataManager which will be helpful for us to provide
a better solution as early as possible.
https://ej2.syncfusion.com/documentation/data/how-to/#work-in-offline-mode
Query#2: I am trying to create a button (myButton) based on data within the API data.
If you want to show a custom element in a column based on another field value, we suggest the “Column Template” feature. Please refer to the below documentation and demo link for more information.
Documentation : https://helpej2.syncfusion.com/vue/documentation/grid/columns/column-template
Demo : https://ej2.syncfusion.com/vue/demos/#/material/grid/column-template.html
Or if you need to set a button field in the Grid dataSource as in your requirement and need to show the html element in the column instead of text we suggest the “disbleHtmlEncode” property.
https://helpej2.syncfusion.com/vue/documentation/grid/cell/content
Regards,
Pavithra S
thanks for this Pavithra.
When I uncomment offline, and set it true, i get a error:
ERROR
Cannot read properties of undefined (reading 'forEach')
TypeError: Cannot read properties of undefined (reading 'forEach')
at cleanUpTrueFalse.processResponse (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/views/TracksView.vue?vue&type=script&lang=js:37:21)
at Ajax.fnSuccess [as onSuccess] (webpack-internal:///./node_modules/@syncfusion/ej2-data/src/manager.js:275:34)
at Ajax.successHandler (webpack-internal:///./node_modules/@syncfusion/ej2-base/src/ajax.js:112:12)
at Ajax.stateChange (webpack-internal:///./node_modules/@syncfusion/ej2-base/src/ajax.js:138:22)
at _this.httpRequest.onreadystatechange (webpack-internal:///./node_modules/@syncfusion/ej2-base/src/ajax.js:74:15)
Also, when using the custom adapter, and offline commented out, its still sending the request back to the server, and not performing the action client side. Like a sort.
re Query2.. that is perfect, thank you!!
Verum Genus,
While using the WebAPIAdaptor, we need to return the response as “Items” and “Count” pair. So, while removing the “offline” property, please return the data as “Items” and “Count” to bind the API data properly. So, a server request will be sent for each data actions.
https://ej2.syncfusion.com/vue/documentation/grid/data-binding/remote-data#web-api-adaptor
If I do this (which is basically un-comment in my example OfflineView.vue, and change my return keys to Item and Count), I get nothing in the grid.
class postProcessing extends WebApiAdaptor {
processResponse() {
let original = super.processResponse.apply(this, arguments);
original.forEach((item) => item['newItem'] = 5);
original.forEach((item) => {
if(item['completed'] == false){
item['completedCleaned'] = "No";
} else {
item['completedCleaned'] = "Yes";
}
original.push(item);
});
return {Items: original, Count: original.length};
}
}
export default({
data() {
return {
data: new DataManager({
url: 'https://jsonplaceholder.typicode.com/todos',
adaptor: new postProcessing(),
//adaptor: new WebApiAdaptor(),
crossDomain: true,
//offline: true
}),
Is that what you mean?
Maybe this will help show it better. To confirm:
As it is, the below code gives me this error and no data in the table:
If I comment out offline: true, then my Grid is populated as a long list of 200 items, but i have lost the ability to paginate, filter, sort, search and of course subsequent paginate, filter, sort, search events are sent to the server (which I do not want to happen).
Code:
<template>
<div class="container">
<h2>Data is ok, but no longer get the postProcessiong of Data option</h2>
<div id="app">
<ejs-grid :dataSource='data' :allowSorting='true' :allowFiltering='true' :filterSettings='filterOptions' :allowPaging="true" :pageSettings='pageSettings' :toolbar='toolbarOptions' >
<e-columns>
<e-column field='id' headerText='ID' textAlign='Right' width=175></e-column>
<e-column field='trackName' headerText='User' width=100></e-column>
<e-column field='title' headerText='Title' width=100></e-column>
<e-column field='newItem' headerText='Pretty Completed' width=100></e-column>
<e-column field='completed' headerText='Raw Completed' width=180></e-column>
<e-column headerText='Buttom' width='150' textAlign='Center' :template="'cTemplate'"></e-column>
</e-columns>
<template v-slot:cTemplate="{data}">
<ejs-button v-if="data.completed == true" @click="myClick(data)">Yesss</ejs-button>
<ejs-button v-else @click="myClick(data)">Noooo</ejs-button>
</template>
</ejs-grid>
</div>
</div>
</template>
<script>
import { Page, Sort, Filter , Toolbar, Search } from "@syncfusion/ej2-vue-grids";
import { DataManager, WebApiAdaptor , Query } from "@syncfusion/ej2-data";
class postProcessing extends WebApiAdaptor {
processResponse() {
let original = super.processResponse.apply(this, arguments);
original.forEach((item) => item['newItem'] = 5);
original.forEach((item) => {
if(item['completed'] == false){
item['completedCleaned'] = "No";
} else {
item['completedCleaned'] = "Yes";
}
original.push(item);
});
let response = {"result": original, "Count": original.length };
console.log(response);
return response;
}
}
export default({
data() {
return {
data: new DataManager({
url: 'https://jsonplaceholder.typicode.com/todos',
adaptor: new postProcessing(),
crossDomain: true,
offline: true
}),
filterOptions: {
matchCase: false,
operator: 'contains',
predicate: 'and',
ignoreAccent: true,
type: 'Excel'
},
toolbarOptions: ['Search'],
pageSettings: { pageSize: 10 },
sortOptions: { columns: [{ field: 'title', direction: 'Ascending' }] },
query: new Query().addParams('aaaaaaa', 'true')
};
},
methods: {
myClick: function(args) {
alert(JSON.stringify(args.title));
}
},
provide: {
grid: [Page, Sort, Filter, Toolbar, Search]
}
});
</script>
<style>
@import "https://cdn.syncfusion.com/ej2/material.css";
</style>
Verum,
In your query, you have mentioned that “want to do some post processing on the JSON I get back from the remote end point.”, so please share what processing you are doing on the JSON, share your requirement with detailed description and also share your syncfusion package version.
this sort of thing from the above example where I call class postProcessing extends WebApiAdaptor. I want to clean up some of the values, add some, adjust some..
original.forEach((item) => item['newItem'] = 5);
original.forEach((item) => {
if(item['completed'] == false){
item['completedCleaned'] = "No";
} else {
item['completedCleaned'] = "Yes";
}
original.push(item);
});
Hi Verum Genus,
While using the Custom Adaptor with offline true we suggest returning the JSON array instead of “result” and “count” inside the processResponse method. Please refer to the below code example for more information.
class postProcessing extends WebApiAdaptor { processResponse() {
let original = super.processResponse.apply(this, arguments);
original.forEach((item) => item['newItem'] = 5);
original.forEach((item) => { if (item['completed'] == false) { item['completedCleaned'] = "No"; } else { item['completedCleaned'] = "Yes"; } original.push(item); }); return original; } }
|
that worked perfect, thank you!
Hi Verum,
We are glad that the provided solution worked. We are marking this thread as solved.