Opened

Sorting dynamic list in datagrid

mdwyer commented

We are trying to create a generic grid that can handle various datasets that are unknown at compile time. I have a dynamic object which I am not able to find to the "Field" property or "SortField" property, but I can display via displaytemplate. The SortChanged will only get call if I have a property on the Field or SortField which i can't do in this case. Is there any way to sort in this scenario?

 var unboxed = ((IDictionary<string, object>)except.ExceptionObject);
                        var colnames = unboxed.Keys.ToArray();


                        <DataGrid TItem="MniDataExceptionModel" 
                      Data="@exceptions1" SortChanged="testsortchanged"  SortMode="DataGridSortMode.Single"
                          ShowCaptions="true" Style="font-size:10pt;margin:0;padding:0;line-height:5px;"
                          Filterable="false" PageSize="@exceptions1.Count()" Striped="true" Padding="Blazorise.Padding.Is0" 
                      TotalItems="@exceptions1.Count()" Narrow="true" Editable="false" Sortable="true">

                         @foreach (var col in colnames)
                        {
                                var type = unboxed[col].GetType();
                                string SortField = "ExceptionObject." + col;
                                <DataGridColumn TItem="MniDataExceptionModel"  Caption="@col" Sortable="true">
                                    <DisplayTemplate>
                                        @{

                                            var value = ((IDictionary<string, object>)(context as MniDataExceptionModel).ExceptionObject)[col];
                                        }
                                        @value
                                    </DisplayTemplate>
                                </DataGridColumn>

                        }

                            </DataGrid>
David-Moreira commented

Hello,

So let me see if I got this right. Since you are using a Dictionary to grab the values, You do not need to use our Field / SortField, etc... Is this right? This is because our internals do not know how to detect & grab the values out of a Dictionary, it just expects a regular class with value types.

Currently we have no other way of specifying the sort value other then the SortField.
But this is what you are after, right?

@mladenmacanovic
Two solutions:

  • We've already talked about some sort of SortGetter the user can override and this seems ideal
    • (We could even go as far as make the other Getters available for override also?)
  • We could try to support dynamic / ExpandoObject which would also be an option and add a bit more flexibility for other stuff users can do. (Altough I am personally not a big fan of dynamic myself)

This is an example of what could potentially work with the dynamic (simplified just for testing)

David-Moreira commented edited

As a dirty workaround you could type all possible columns as regular properties and then fill their values using reflection. That way DataGrid could use Field / SortField successfully.

However I understand this is a dirty solution and might not be feasible for your use case if you have hundreds of possible columns.... and kinda defeats the whole purpose.
Just throwing it out... just in case, for your use case, it was possible for the time being, while we implement a solution.

mdwyer commented

I am not a big fan of dynmic/expando either, however we have a process that can pulls various data and we just need to display/sort/filter it, so we want something generic to handle it so all we have to do is change/add a query and the grid will update without any code changes.

I am looking at trying to hook into SortDirectionTemplate and tracking the column/direction and updating that way. I have been able to pass in the column to sort and use a dictionary to store the current state of all columns, however trying to sort the dynamic column is a little tricky still.

mdwyer commented

For the dirty solution, the whole goal is to have something very generic plus the users like to add a few fields to some of the queries, and if we create a new exception that could have new fields altogether, vs just adding a new query and the page just working.

David-Moreira commented

Oh maybe you can try ReadData? You have more freedom that way.
DataGrid should pass you the column list and what the user has requested to sort by. Just that you have to do all the handling of the data yourself... filtering, sorting, etc....

There's an example on our Demo.

mdwyer commented

That is a very interesting approach, I am looking deeper into that now as that appears to be a workable solution for what we are trying to do.

David-Moreira commented

Alright, Let us know your findings.

Want to comment on this issue? Log in and write your comment.
Asignee
No assignee
Labels
No Labels