Completed
Last Updated: 07 Jul 2020 16:22 by ADMIN
Release 2.15.0
Roy
Created on: 26 Sep 2019 07:30
Category: Grid
Type: Feature Request
4
grid with observable collection - page index to reset if unavailable upon data source change
When binding to an observable collection, and the data source changes, the current behavior is that the page index remains the same (let's say, page 4). If the new data source has less data, you will see nothing. In such cases, the page index should remain the same only if there is sufficient data. If there isn't, it should reset to the first page.
1 comment
ADMIN
Marin Bratanov
Posted on: 07 Jul 2020 16:22

Hello,

Recently, improvements were made on the grid paging that ensure data will be visible when the data source changes (see more here).

Here's a sample snippet to test this with, and a short video shows the behavior with the current latest (2.15.0) which is correct - there is always data that is seen even when the new data has fewer items than the original.

 

@using System.Collections.ObjectModel

<div class="d-flex flex-row my-2">
    <TelerikButton OnClick="@AddForecast">Add Item</TelerikButton>
    <TelerikButton OnClick="@RemoveForecast">Remove Item</TelerikButton>
    <TelerikButton OnClick="@ChangeData">Change Collection</TelerikButton>
</div>

<TelerikGrid Data=@GridData Height="400px" Pageable="true">
    <GridColumns>
        <GridColumn Field="Id" />
        <GridColumn Field="Name" />
    </GridColumns>
</TelerikGrid>

@code {
    public ObservableCollection<SampleData> GridData { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await LoadData();
    }

    private async Task LoadData()
    {
        var forecasts = Enumerable.Range(1, 45).Select(x => new SampleData { Id = x, Name = $"Name {x}" });

        GridData = new ObservableCollection<SampleData>(forecasts);
    }

    void AddForecast()
    {
        GridData.Add(new SampleData { Id = GridData.Count + 1, Name = $"name {GridData.Count + 1}" });
    }

    void RemoveForecast()
    {
        if (GridData.Count > 0)
        {
            GridData.RemoveAt(0);
        }
    }

    void ChangeData()
    {
        var forecasts = Enumerable.Range(1, 15).Select(x => new SampleData { Id = x, Name = $"Second collection - Name {x}" });

        GridData = new ObservableCollection<SampleData>(forecasts);
    }

    public class SampleData
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

 

If you want to change this behavior further, you can use the Page parameter of the grid to set the desired page (e.g., always reset it to 1 when you change the data source). You can find a similar example of binding the parameter in the second snippet in the docs here.

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.