HTMLElements / smart-blazor

Blazor UI Components & Examples

Home Page:https://www.htmlelements.com/blazor/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Strange issue on Tree elements when data is loaded as a DataSource

domingoladron opened this issue · comments

Hey there,

Perhaps it's my lack of understanding of how to access the data elements of a treeview, but it appears I have an occasional hiccup when trying to access the "id" property of a Tree item when a given element is selected.

I can provide source code for this, but in essence, imagine the following code:

   <Tree @ref="_treeMenu"
                          OnChange="TriggerLoadFileContents"
                          DataSource="@TreeItems">
@code {
     private List<Dictionary<string, object>> TreeItems { get; set; } = new();

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
       //each item in the tree has an id, label, sort Dictionary entry
        TreeItems = ProjectContentTreeFactory.GenerateTreeDataFromProject(CurrentProject);
    }
  
   private async Task TriggerLoadFileContents(Event arg)
    {
        if (arg["Detail"] is TreeChangeEventDetail eventData)
        {
            var itemId = eventData.Item.id.ToString();

            if (!string.IsNullOrEmpty(itemId))
            {
                var currentItem= MyDataSource.FindItemById(itemId);
                if (currentItem!= null)
                {
                    TriggerLoadFileContents(currentItem);
                }
            }
        }

        await Task.CompletedTask;
    }
}

The issue is that in 9 out of 10 tree elements, when I fetch it back as a TreeChangeEventDetail item and inspect the eventData.Item.id property, it IS the Id property I set on the TreeItems. Only 1 in 10 do I see what appears to be a nonsensically-generated key which looks something like this each time: "Smart0HMNK6T2AMEFEItemP0_1_1LItemLabelWithNoSpaces"

Am I missing something? It appears that in 9 of 10 cases, the Id dictionary entry I pass is being returned back inside the TreeChangeEventDetail, but in the 10th case, the Id field appears to contain a random string starting in the word 'Smart' and ending in the label I provided (just sans spaces).

Perhaps there is some other way I should be accessing the TreeChangeEventDetail data. I'm unsure.

Any help would be most appreciated.

Cheers

Note that when I inspect the datasource via

var selectedValues = await _treeMenu.GetDataSourceAsync();

I can see the element in question in the hierarchy with the id I assigned it as a Dictionary entry

Ah, so I'll leave this here for anyone who faces something similar.

After talking with one of the tech people, it seems the "id" property on your data elements in the tree must conform to the string format of ^[A-Za-z]+[\w\-\:\.]*$ and as mine contained a shortguid (a combination of letters and numbers), the internals of the Tree would ignore my id and instead generate a fresh value.

In order to get around this, I was forced to write a specialty parser for my shortguid values to swap out any numbers for expected strings. Once I did so, the Tree picked up my elements and their "id" value and everything worked as expected.