gustavnavar / Grid.Blazor

Grid component with CRUD for Blazor (client-side and server-side) and ASP.NET Core MVC
GNU Lesser General Public License v2.1
696 stars 134 forks source link

Checkboxes in GridComponent does not include all the rows when pagination is active #388

Closed mnaranjoc closed 11 months ago

mnaranjoc commented 1 year ago

I have a grid with 30 items and a pagination of 25. If I click on the header checkbox to select all rows, visually they are selected, I can switch between page 1 and 2 and they are all selected.

However, the Checkboxes property only shows me the first 25 rows if I am on page 1, if I switch to page 2, it only shows me the remaining 5.

Is there any way that independently of the page in which I am positioned, it shows me that the 30 rows are checked?

gustavnavar commented 1 year ago

The selected checkboxes are configured in the following ways:

These 2 mechanisms are implemented and configured by the GridBlazor component at the same time because their purpose is different, but complementary.

GridComponent.Checkboxes.Get("columnName") dictionary includes the checked boxes of the current page using the the row id of the page, starting by 0 for the first row, as key. Each time that the page is rendered this dictionary is initialized and each checkbox value is setted as follows using the global settings:

    bool _value = false;

    // init value when header checkbox is enabled and is not null
    var header = GridComponent.HeaderComponents.Get(_columnName);
    var exceptCheckedRows = GridComponent.ExceptCheckedRows.Get(_columnName);
    var keys = GridComponent.Grid.GetPrimaryKeyValues(Item);
    string stringKeys = string.Join('_', keys);
    if (exceptCheckedRows != null && !string.IsNullOrWhiteSpace(stringKeys) && exceptCheckedRows.ContainsKey(stringKeys))
        _value = exceptCheckedRows.Get(stringKeys);
    else if (header != null && header.IsChecked().HasValue)
        _value = header.IsChecked().Value;

As you can see the checkbox value is false by default. Then if GridComponent.ExceptCheckedRows dictionary contains the row key, the value is the one on the dictionary. If GridComponent.ExceptCheckedRows dictionary doesn't contain the row key but the header IsChecked returns a non null value, the checkbox value is this one.

If you want to get the checked rows value you can follow one of the following approaches: