remove all nodes and looping through nodes

I see that the dash.nodes property is deprecated, but there isn't any documentation that states it replacement.  How do I loop through code behind through each panel if I need to do something to each panel at run time?  Also, calling the removeall Task returns a null reference exception, so I can't do something like:

if(dashboardobj.nodes != null)
{
await dashboardobj.RemoveAll();
}

What would the alternative be?

4 Replies 1 reply marked as answer

HA HappyCamper February 10, 2021 03:31 AM UTC

I did find this:

List<PanelModel> panel = await this.dashboardObject.Serialize();
        Length = panel.Count;
        if (Length != 0)
        {
            for (var i = Length - 1; i < Length; i++)
            {
                await dashboardObject.RemovePanel(panel[Length - 1 - i].Id);
            }
        }

However, I get a nullreference exception again at the Serialize() task.  should I be doing a check?



SS Sharon Sanchez Selvaraj Syncfusion Team February 10, 2021 10:55 AM UTC

Hi HappyCamper, 
 
Thanks for contacting Syncfusion support. 
 
We have checked with your queries. 
 
Query 1: However, I get a nullreference exception
We were unable to reproduce the issue in our end. We have created a sample here for your reference. Please modify the for loop code in your sample with the following snippet. 

  public async Task removeClick(EventArgs args) 
    { 
        List<PanelModel> panel = await this.dashboardObject.Serialize(); 
        var count = dashboardObject.Panels.Count; 
        for (var i = count - 1; i >= 0; i--) 
        { 
            await dashboardObject.RemovePanel(dashboardObject.Panels[i].Id); 
        } 
    } 


Please find the sample attached below. 


Query 2: dash.nodes property is deprecated 
 
We regret to inform you that, we have no direct property such as “nodes” as mentioned in your update. Refer to the following link for changes made in 18.4 release 
 
 
Can you please provide the exact details of your mentioned property so that we can assist you promptly? 
 
To know more details about Dashboard Layout component please check with the below links. 
 
 
 
 
Please get back to us if you need any further assistance. 
 
Regards, 
 
Sharon Sanchez S. 



HA HappyCamper February 10, 2021 02:51 PM UTC

Hi Sharon,

Thanks for the response.  Going through the samples I figured out that Serialize() does return the panels object.  Fair, but the function name "Serialize" is a bit misleading.  In my project, I dynamically build the panels based on a json object and in my dashboard page, you are able to switch dashboards where I am attempting to do the following:

1. Clear any panels in the dashboardlayout 
2. add new panels based on json config.

When the page first loads, there won't be any panels in the dashboardlayout (panels == null), however running serialize returns a nullreference exception:

List<PanelModel> panels = await this.dashboardObject.Serialize()

Would it be possible to return null instead of an exception?  How should I check for any panels in the dashboardlayout objet, given that in some cases, the panels could be null


Markup (Intentionally no panels, as the panels are build on runtime):

<SfDashboardLayout ID="default_dashboard"
                   @ref="dashboardObject"
                   AllowFloating="false"
                   Columns="6"
                   AllowResizing="@EditMode"
                   AllowDragging="@EditMode">
    <DashboardLayoutEvents Changed="layoutChanged" Created="layoutChanged" />
</SfDashboardLayout>




Below the code that builds the panels. The first time that the below code is run, Serialize() returns an exception, as there is no panels in the control.  My preference would be to get a null objet back from Serialize, or being able to run await RemoveAllAsync without an exception being returned if there's no panels in the control.  Thus, RemoveAllAsync is more like a "TryRemoveAllAsync" task rather than a hard break if there's no panels in the control. Running the below in a try catch block doesn't work, as the exception is thrown within the control. 

Which method should I use to check for any existing panels in the control, regardless if there is any or not?


     if (DashboardId.HasValue)
            {
                var Dashboard = Data.Where(x => x.Id == Id.Value.ToString()).FirstOrDefault();
                if (Dashboard != null)
                {
                    DashboardName = Dashboard.Name;
                    DashboardCategory = Dashboard.Type;
                }

                if(dashboardObject != null)
                {
                    List<PanelModel> panels = await this.dashboardObject.Serialize();
                    if (panels.Count > 0)
                    {
                        await dashboardObject.RemoveAllAsync();
                    }
                }

              await AddPanels(DashboardId);

                
            }






SS Sharon Sanchez Selvaraj Syncfusion Team February 11, 2021 01:19 PM UTC

Hi HappyCamper, 

We checked your query regarding the Null Reference exception, it arises only when the below format is used. 
 
<SfDashboardLayout ID="default_dashboard" 
                   @ref="dashboardObject" 
                   AllowFloating="false" 
                   Columns="6" 
                   AllowResizing="@EditMode" 
                   AllowDragging="@EditMode"> 
    <DashboardLayoutEvents Changed="layoutChanged" Created="layoutChanged" /> 
</SfDashboardLayout> 

We suggest you to add empty panels to the code as given below.  

<SfDashboardLayout ID="default_dashboard" 
                   @ref="dashboardObject" 
                   AllowFloating="false" 
                   Columns="6" 
                   AllowResizing="@EditMode" 
                   AllowDragging="@EditMode"> 
<DashboardLayoutPanels> 
</<DashboardLayoutPanels> 
    <DashboardLayoutEvents Changed="layoutChanged" Created="layoutChanged" /> 
</SfDashboardLayout> 


As suggested in the previous sample, you can serialize the panel and you will be able to get the count. 

By using the reference of the dashboard, the count can be obtained as shown below. 

List<PanelModel> panel  = await this.dashboardObject.Serialize(); 
var count = panel.Count; 

Please get back to us if you need any further assistance. 

Regards, 

Sharon Sanchez S. 


Marked as answer
Loader.
Up arrow icon