Skip to main content

getData()

Description

Gets a promise with an array of objects with data for the Grid
info

The getData() method is a part of the RestDataProvider service intended for working with a server

Usage

getData(): Promise<[]>

Response

The getData() method sends a request to the server by the GET method and returns a promise with data for the Grid (columns IDs with values for each row). For an example of returned objects, see data.

Example

import { useState, useEffect, useMemo } from "react";
import { RestDataProvider } from "@svar-ui/grid-data-provider";
import { Grid } from "@svar-ui/react-grid";

const columns = [
{
id: "name",
header: "Title",
editor: "text",
},
{
id: "year",
header: "Year",
editor: "text",
},
{
id: "votes",
header: "Votes",
editor: "text",
},
];

export default function Example() {
const [data, setData] = useState([]);

// Create provider once
const provider = useMemo(
() =>
new RestDataProvider("https://some-backend-url", (obj) => {
obj.year = obj.year * 1;
obj.votes = obj.votes * 1;
}),
[]
);

// Fetch data on mount
useEffect(() => {
provider.getData().then((v) => setData(v));
}, [provider]);

// Grid init callback receives the Grid API and sets the provider
const init = (api) => {
api.setNext(provider);
};

return <Grid data={data} columns={columns} init={init} />;
}

Related articles: