Managing Data
Loading and managing data is a fundamental part of Adaptable.
Declaring the data and the primary key
You specify your data using the data
prop of <Adaptable.Provider />
.
Note
In addition, the component needs a primaryKey
prop - which is the name of the field that uniquely identifies each row in your DataGrid.
<Adaptable.Provider
primaryKey="id"
data={[...]}
adaptableId="Data Management Demo"
>
<Adaptable.UI />
</Adaptable.Provider>
The data
you specify can be one of the following:
- an array of objects
- a Promise tha resolves to an array
- a function that returns any of the above
Find Out More
The data
prop in Adaptable is passed as-is to InfiniteTable.
So for more details, read more about the data-loading strategies that are available.
Note
The primaryKey
field doesn't need (though it can) to be a column in the DataGrid. It simply needs to be an existing property in all the items of the loaded data.
It's up to you to decide what columns to display in the grid and what fields they will be bound to.
For more on configuring and using columns, read the Working with Columns page.
This example is configured with a basic array of objects as data. All of the properties described above are available, though this example only shows a few columns.
The primaryKey
is bound to the id
property, but we don't have a column specified for it.
<Adaptable.Provider primaryKey="id" data={rowData}>
<Adaptable.UI />
</Adaptable.Provider>
Data Loading Strategies
We're aware there are many strategies for loading data - each with its own strengths.
However, our approach gives you the flexibility to use your preferred data-fetching library/solution.
Any of the following are accepted as the data
prop:
- an array of objects
- a Promise that resolves to an array
- a function that returns any of the above
This makes it very easy to load your data with fetch
, axios
or another library - and pass the resulting array to Adaptable.
Or you can give us the promise that resolves to the data, and that's fine too.
Or, for when you need full extensibility and flexibility, you can specify a function that we call to get the data. Whenever data needs to be loaded, we will call your function, with all the filtering, aggregation, sorting and pivoting details in the current Adaptable state. In this way, you can send whatever information you need to your server, and return a promise that resolves to the data you want to display.
Another option would be to use various React-focused popular libraries like @tanstack/react-query
or swr
for data fetching, and pass the resulting data to Adaptable.
Using fetch
For basic datasets, which have simple data requirements, using fetch
is probably sufficient, so here is an example:
This demo uses fetch
to trigger a request to the server to load the data. The resulting promise is passed to Adaptable.
The dataset represents fictional employees in companies.
This demo loads and displays 10k
rows.
Using a function that returns a promise
As well as using fetch
, you can also use a function that returns a promise. This is useful when you need to pass additional information to the server, or when you need to do some processing on the client side before returning the data.
This demo uses a function for the data
property. The function uses fetch
to trigger a request to load data.
This demo loads and displays 10k
rows.
Refetching data
When the reference of the data
prop you pass to <Adaptable.Provider />
changes, Adaptable will use the new data - so if you use an array, that will be displayed, but if a function is used, Adaptable will call it again to get the new data.
Note
Make sure your data
prop is stable across renders if you don't want to trigger a refetch.
You can select the size of the dataset to load from the server. When the size changes, the data
prop is changed to reference a new function, which triggers a refetch.
In this demo, because the data
function is updated whenever a new size is selected, the data is refetched.
Note
Adaptable offers more ways to refetch data - we will document them soon. It will allow you to specify which actions trigger a data-refetch.
CRUD
Adaptable offers API methods (via the DataSourceApi
) to add/update/delete rows in the grid. To access this api, use adaptableApi.dataSourceApi
. You can get a reference to the adaptableApi
by using the onReady
callback prop of the <Adaptable.Provider />
component or by using the useAdaptableApi
hook.
Adding data
adaptableApi.dataSourceApi.addData(dataObject)
- adds the given object at the end of the data-source. The object needs to have a value for the property that is theprimaryKey
of the data-source.adaptableApi.dataSourceApi.addDataArray(dataArray)
- adds the given objects at the end of the data-source.
Inserting data
adaptableApi.dataSourceApi.insertData(data, options)
- inserts the given object at the position specified in theoptions
object. Theoptions
for theinsertData
andinsertDataArray
methods is an object of the following shape:{ position: 'start' | 'end' }
- adds the data at the start or end of the data-source.{ position: 'before'| 'after', primaryKey: any }
- whenbefore
orafter
is specified, theprimaryKey
is required, since Adaptable needs to know the row before or after which to insert the new data.
adaptableApi.dataSourceApi.insertDataArray(dataArray, options)
- same asinsertData
, but for an array of data objects.
Updating data
adaptableApi.dataSourceApi.updateData(dataObject)
- thedataObject
must at least have a value for the property that is theprimaryKey
of the data-source. All other properties the object has will be updated in the data-source.adaptableApi.dataSourceApi.updateDataArray(dataArray)
- an array of data objects to update - similar toupdateData
.
Removing data
adaptableApi.dataSourceApi.removeData(dataObject)
- looks for the object with the primary key value found in the passeddataObject
and removes it from the data-source.adaptableApi.dataSourceApi.removeDataArray(dataArray)
- an array of data objects to remove - similar toremoveData
.adaptableApi.dataSourceApi.removeDataByPrimaryKey(primaryKey)
- removes the object with the specifiedprimaryKey
from the data-source.adaptableApi.dataSourceApi.removeDataArrayByPrimaryKeys(primaryKeys)
- removes the objects with the specifiedprimaryKey
s from the data-source.
This demo shows how to use the CRUD API methods to add, update and delete rows in the grid imperatively.