<script>
import API from '../services/api';
import { Grid, Sort } from '@syncfusion/ej2-grids';
import {onMount} from "svelte";
let AllCustomers;
onMount( () => {
API.get("/customers/get", null).then((res) => {
AllCustomers = res;
console.log(AllCustomers);
Grid.Inject(Sort);
let grid = new Grid({
dataSource: AllCustomers,
allowSorting: true,
columns: [
{ field: 'id', headerText: 'Customer ID', textAlign: 'Right', width: 120 },
{ field: 'company_name', headerText: 'Name', width: 150 },
]
});
grid.appendTo('#Grid');
})
});
script>
<div class="container is-fluid">
<div id="Grid">div>
div>
function getData(gridquery: any) {
// Grid action queries
let state = gridquery;
// Request URL
let BASE_URL = "https://services.odata.org/V4/Northwind/Northwind.svc/Orders";
let sortQuery = "";
const skipquery = state.skip ? `$skip=${state.skip}` : null;
let pageQuery = "";
const takeQuery = state.take ? `$top=${state.take}` : null;
if (skipquery) {
pageQuery = `${skipquery}&`;
}
if (takeQuery) {
pageQuery = `${pageQuery}${takeQuery}`;
}
// Generate query with your request URL for API call
var ajax = new Ajax(
`${BASE_URL}?${pageQuery}${sortQuery}&$count=true`
);
ajax.send();
ajax.onSuccess = (data: any) => {
// Response data is parsed and set as Grid’s data source
let final: any = JSON.parse(data);
grid.dataSource = { result: final.value, count: final["@odata.count"] };
};
} |
// Grid’s created event handler
function created() {
// Initial state is created and method defined for making API call is involed
var state = { skip: 0, take: 10 };
getData(state);
}
// Grid’s dataStateChange event handler
function dataStateChange(state: any) {
// Here the query for the performed Grid action will be returned as event arguments
// You can pass this to the method defined for making the API call
getData(state);
} |