[App.vue]
<template> <div class="col-lg-12 control-section">
<div>
<ejs-grid ref="grid" id="transaction_grid" :enableVirtualization="true" :allowGrouping="false"
gridLines="Vertical" :height="height" :dataSource="data" :allowSorting="true" :allowTextWrap="true" :allowExcelExport="true" :dataStateChange="dataStateChange" >
<e-columns>
<e-column field="OrderID" headerText="Order ID" width="130" textAlign="Right"></e-column>
<e-column field="CustomerID" headerText="Customer Name" width="150"></e-column>
<e-column field="ShipName" headerText="Ship Name" width="200"></e-column>
<e-column field="ShipCity" headerText="Ship City" width="150"></e-column>
</e-columns>
</ejs-grid>
</div>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin,Sort,Group,Page, VirtualScroll} from "@syncfusion/ej2-vue-grids";
import { OrderService } from "./order-service";
Vue.use(GridPlugin);
export default Vue.extend({
data() {
return {
data: {},
height: 800,
orderService: new OrderService()
};
},
mounted() {
// Used vue mounted hook to provide dataSource to Grid and handled the initial paging
let state = { skip: 0, take: 40 };
this.dataStateChange(state);
},
methods: {
dataStateChange: function(state) {
// Handled the other Grid actions like paging, sorting etc.. by using dataState change event
this.orderService.execute(state).then(gridData => {
this.data = gridData;
});
}
},
provide: {
grid: [Sort, Group, Page, VirtualScroll]
}
});
</script>
<style>
@import "https://cdn.syncfusion.com/ej2/material.css";
</style>
--------------------------------------------------------------------------------------------------------------------------------------- [order-service.ts] export class OrderService { public ajax: Ajax = new Ajax({
type: “GET”,
mode: true,
onFailure: (e: Error) => {
return false;
}
});
private BASE_URL: string =
public execute(state: DataStateChangeEventArgs): Promise<DataResult> {
return this.getData(state);
}
private getData(state: DataStateChangeEventArgs): Promise<DataResult> {
const pageQuery = `$skip=${state.skip}&$top=${state.take}`; // handled the page query
let sortQuery: string = “”;
if ((state.sorted || []).length) {
sortQuery =
`&$orderby=` +
(<any>state).sorted
.map((obj: Sorts) => {
return obj.direction === “descending”
? `${obj.name} desc`
: obj.name;
})
.reverse()
.join(“,”);
}
this.ajax.url = `${
this.BASE_URL
}?${pageQuery}${sortQuery}&$inlinecount=allpages&$format=json`;
return this.ajax.send().then((response: any) => {
let data: any = JSON.parse(response);
return <DataResult>{
result: data[“d”][“results”],
count: parseInt(data[“d”][“__count”], 10)
};
});
}
} |