Firstly, thanks for this great project as it is exactly what I was looking for to bring datatables into Blazor.
I found an issue that when initially loading the datatable it runs as expected but if I wanted to change/modify/update the data, it did not seem to take effect, specifically by changing the DOM or updating the data object.
To replicate the issue, using your demo project I used the WeatherDataset
Dataset as the source of data for the object and DOM display options. I modified the DOM table as below:
<DataTable @ref="WeatherDomTable" Id="MyTableDomSourced" Class="table table-striped table-bordered w-100">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (Weather w in WeatherDataset)
{
<tr>
<td>@w.Date.ToString("yyyy-MM-dd")</td>
<td>@w.TemperatureC</td>
<td>@w.Summary</td>
</tr>
}
</tbody>
</DataTable>
I then created a button and function that would generate a random list of Weather
objects and assign them to the WeatherDataset
and call StateHasChanged
. The DOM table would update the HTML but the datatable would not recognise the change and just show all the rows but not paginate, search or show the correct total number of items. The object table would not update at all until I navigated to another tab and back again.
I found a solution by adding the following function to the Datatables.Blazor\Datatable.razor page:
public async Task RefreshTable() {
await Datatables.DestroyAsync(_tableReference);
await Datatables.InitialiseAsync(_tableReference, DataTableOptions.FromComponent(this));
await InvokeAsync(StateHasChanged);
}
So then I reference your datatable instance on my page, then when I update the WeatherDataset
I then call WeatherDomTable.Refresh()
and the table seems to update correctly.
I was wondering if this change could be added to main branch of this project or if there is any better way to update data and have it displayed.
To give some background on my project, I have a timer that will poll a database every 2 minutes and I need to update the datatable accordingly.