Skip to content

DevExpress-Examples/blazor-use-devextreme-diagram

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blazor - Use DevExtreme Diagram in Blazor Applications

This example adds the DevExtreme Diagram widget into your Blazor application.

Diagram in DevExpress Blazor App

Implementation Details

Register DevExtreme Resources

DevExtreme widgets require the use of DevExtreme scripts and stylesheets. You must register scripts in the following order:

  1. JQuery library (https://code.jquery.com/jquery-3.5.1.min.js>)
  2. Custom or component-specific scripts, for example, diagram (https://cdn3.devexpress.com/jslib/25.1.3/js/dx-diagram.min.js)
  3. Base DevExtreme script (https://cdn3.devexpress.com/jslib/25.1.3/js/dx.all.js)

The DevExpress Blazor Resource Manager automatically registers JQuery and standard DevExtreme scripts if your project includes the DevExpress.Blazor package. To load component-specific DevExtreme resources correctly, you must:

  • Unregister JQuery and base DevExtreme scripts using the DxResourceManager.RegisterScripts method.
  • Reference scripts in the <head> section of the Components/App.razor file.
<head>
    <!--...-->
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdn3.devexpress.com/jslib/25.1.3/js/dx-diagram.min.js"></script>
    <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/25.1.3/js/dx.all.js"></script>
    @DxResourceManager.RegisterTheme(ThemesService.ActiveTheme)
    @DxResourceManager.RegisterScripts((config) => {
        config.Unregister(CommonResources.JQueryJS);
        config.Unregister(CommonResources.DevExtremeJS);
    })
    <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/25.1.3/css/dx-diagram.min.css">
    <link href=@AppendVersion("https://cdn3.devexpress.com/jslib/25.1.3/css/dx.fluent.blue.light.css") rel="stylesheet" />
    <!--...-->
</head>

Implement a Wrapper

Implement a Blazor component that wraps DevExtreme Diagram. The wrapper consists of two files:

  • DevExtremeDiagram.razor.js configures Diagram settings (binds the component to a data model defined in the ProjectTask.cs file).

    export async function initializeDiagram(element, dataSource) {
         const projectTasks = !!dataSource ? dataSource : null;
    
         return $(element).dxDiagram({
             nodes: {
                 dataSource: new DevExpress.data.ArrayStore({
                     key: 'id',
                     data: projectTasks,
                 }),
                 keyExpr: "id",
                 parentKeyExpr: "parent_ID",
                 textExpr: "task_Name",
             },
         });
    }
  • DevExtremeDiagram.razor renders the Diagram. In this file, call the LoadDxResources method to force the Resource Manager to load all client scripts.

    <div @ref="Diagram"></div>
        protected override async Task OnAfterRenderAsync(bool firstRender) {
            if (firstRender) {
                await JS.LoadDxResources();
                ClientModule = await JS.InvokeAsync<IJSObjectReference>("import", "./DevExtremeComponents/DevExtremeDiagram.razor.js");
                ClientDiagram = await ClientModule.InvokeAsync<IJSObjectReference>("initializeDiagram", Diagram, DataSource);
            }
            await base.OnAfterRenderAsync(firstRender);
        }

Render the Blazor component

Use the wrapper as a standard Blazor component. The following code adds a DevExtremeDiagram wrapper to the page and binds it to data:

<DevExtremeDiagram DataSource="DataSource" />

@code {
    List<ProjectTask> DataSource { get; set; }

    protected override void OnInitialized() {
        DataSource = new() {
            new() { ID = 1, Task_Name = "Project Planning", Description = "Define project scope and goals" },
            new() { ID = 2, Parent_ID = 1, Task_Name = "Requirement Analysis", Description = "Gather and document requirements" },
            // ...
        };
    }
}

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Add the DevExtreme Diagram widget into your Blazor application.

Resources

License

Stars

Watchers

Forks

Contributors 6