Back to Devexpress

Create a Static Custom Item for the Web Dashboard

dashboard-119836-web-dashboard-advanced-customization-create-a-custom-item-create-a-static-item.md

latest17.8 KB
Original Source

Create a Static Custom Item for the Web Dashboard

  • Sep 01, 2025
  • 7 minutes to read

Important

A custom item is an independent module called an extension. If you are not familiar with the basic concepts of extensions, please review the following topic before you continue: Extensions Overview.

This tutorial shows how to create a custom item that displays text and adds a custom property that allows you to change this static text in the UI.

You can download the ready-to-use projects based on the Create a custom item tutorials:

View Example: ASP.NET CoreView Example: React

Prerequisites

A custom item script depends on the platform you use:

Module scriptClient components (Angular, React, Vue) use imports and classes to define a custom item.Classic scriptA script for the ASP.NET Web Forms, ASP.NET MVC, ASP.NET Core, BLazor, and JavaScript controls encapsulates a custom item in the IIFE function expression.

Before you begin, create a Web Dashboard project for the required platform.

Create an External Script

In your project, create an external HelloWorldItem.js file, so you can reuse the created extension in other Web Dashboard applications.

Define a class that will be a custom item (HelloWorldItem).

js
import { CustomItemViewer, ResourceManager } from 'devexpress-dashboard/common'
import { CustomItem } from 'devexpress-dashboard/model'
class HelloWorldItem {
}

export default HelloWorldItem;
js
window.HelloWorldCustomItem = (function () {
    class HelloWorldItem {
    }
    return HelloWorldItem;
})();

Warning

If you use the Classic script, place the code below inside the self-invoking function.

Define a Toolbox Icon

In the HelloWorldItem.js file, add a 24x24 SVG icon as a string variable:

js
var svgIcon = '<svg id="helloWorldItemIcon" viewBox="0 0 24 24"><path stroke="#42f48f" fill="#42f48f" d="M12 2 L2 22 L22 22 Z" /></svg>';
js
const svgIcon = '<svg id="helloWorldItemIcon" viewBox="0 0 24 24"><path stroke="#42f48f" fill="#42f48f" d="M12 2 L2 22 L22 22 Z" /></svg>';

The icon will be used later to display the custom item in the Toolbox:

Specify the Custom Item’s Settings

Implement the ICustomItemMetaData interface to define this custom item’s settings:

  • In the icon option, pass the SVG icon’s id (helloWorldItemIcon).
  • In the title option, specify the text displayed in the dashboard item caption and as the Toolbox element’s tooltip (Hello World Item).

Use the ICustomItemMetaData.customProperties and ICustomItemMetaData.optionsPanelSections properties to define a custom option that allows a user to change the custom dashboard item’s display text:

js
var helloWorldItemMetaData = {
    customProperties: [{
        ownerType: CustomItem,
        propertyName: 'customProperty',
        valueType: 'string',
        defaultValue: 'Hello World!'
    }],
    optionsPanelSections: [{
        title: 'Custom Properties',
        items: [{
            dataField: 'customProperty',
            label: {
                text: 'Item Text'
            },
            editorType: 'dxTextBox',
            editorOptions: {
                placeholder: "Enter text to display"
            }
        }]
    }],
    icon: 'helloWorldItemIcon',
    title: 'Hello World Item'
};
js
const helloWorldItemMetaData = {
    customProperties: [{
        ownerType: DevExpress.Dashboard.Model.CustomItem,
        propertyName: 'customProperty',
        valueType: 'string',
        defaultValue: 'Hello World!'
    }],
    optionsPanelSections: [{
        title: 'Custom Properties',
        items: [{
            dataField: 'customProperty',
            label: {
                text: 'Item Text'
            },
            editorType: 'dxTextBox',
            editorOptions: {
                placeholder: "Enter text to display"
            }
        }]
    }],
    icon: 'helloWorldItemIcon',
    title: 'Hello World Item'
};

Implement Custom Item Rendering

The CustomItemViewer class specifies visual representation of the custom item. Override the CustomItemViewer.renderContent method to display custom item content. Use the CustomItemViewer.getPropertyValue method to get the value of the created custom option and display this value in the viewer.

Note

Do not set IDs for DOM elements in the CustomItemViewer.renderContent method call. When the custom item is maximized, the renderContent method is called again and creates one more DOM element with the same ID. If you create an element inside this method, the element ID will not be unique on the page.

js
import { CustomItemViewer, ResourceManager } from 'devexpress-dashboard/common'
import { CustomItem } from 'devexpress-dashboard/model'
// ...
class HelloWorldItemViewer extends CustomItemViewer {
    renderContent(element, changeExisting) {
        element.innerText = this.getPropertyValue('customProperty');
    };
}
js
class HelloWorldItemViewer extends DevExpress.Dashboard.CustomItemViewer {
    constructor(model, $container, options) {
        super(model, $container, options);
    }
    renderContent = function ($element, changeExisting) {
        var element = $element.jquery ? $element[0] : $element;
        element.innerText = this.getPropertyValue('customProperty');
    }
}

Create an Extension

The ICustomItemExtension interface allows you to define a custom item as an extension.

js
import { CustomItemViewer, ResourceManager } from 'devexpress-dashboard/common'
import { CustomItem } from 'devexpress-dashboard/model'
// ...
class HelloWorldItem {
    constructor(dashboardControl) {
        ResourceManager.registerIcon(svgIcon);    
        this.name = "helloWorldItem";
        this.metaData = helloWorldItemMetaData;
    }

    createViewerItem(model, $element, content) {
        return new HelloWorldItemViewer(model, $element, content);
    }
}

export default HelloWorldItem;
js
class HelloWorldItem {
    constructor(dashboardControl) {
        DevExpress.Dashboard.ResourceManager.registerIcon(svgIcon);
        this.name = "helloWorldItem";
        this.metaData = helloWorldItemMetaData;
    }
    createViewerItem(model, $element, content) {
        return new HelloWorldItemViewer(model, $element, content);
    }
}
return HelloWorldItem;

The complete extension code:

Show code

js
// #region imports
import { CustomItemViewer, ResourceManager } from 'devexpress-dashboard/common'
import { CustomItem } from 'devexpress-dashboard/model'
// #endregion
// #region svgIcon
var svgIcon = '<svg id="helloWorldItemIcon" viewBox="0 0 24 24"><path stroke="#42f48f" fill="#42f48f" d="M12 2 L2 22 L22 22 Z" /></svg>';
// #endregion
// #region metadata
var helloWorldItemMetaData = {
    customProperties: [{
        ownerType: CustomItem,
        propertyName: 'customProperty',
        valueType: 'string',
        defaultValue: 'Hello World!'
    }],
    optionsPanelSections: [{
        title: 'Custom Properties',
        items: [{
            dataField: 'customProperty',
            label: {
                text: 'Item Text'
            },
            editorType: 'dxTextBox',
            editorOptions: {
                placeholder: "Enter text to display"
            }
        }]
    }],
    icon: 'helloWorldItemIcon',
    title: 'Hello World Item'
};
// #endregion
// #region viewer
class HelloWorldItemViewer extends CustomItemViewer {
    renderContent(element, changeExisting) {
        element.innerText = this.getPropertyValue('customProperty');
    };
}
// #endregion
// #region createItem
class HelloWorldItem {
    constructor(dashboardControl) {
        ResourceManager.registerIcon(svgIcon);    
        this.name = "helloWorldItem";
        this.metaData = helloWorldItemMetaData;
    }

    createViewerItem(model, $element, content) {
        return new HelloWorldItemViewer(model, $element, content);
    }
}

export default HelloWorldItem;
// #endregion
js
window.HelloWorldCustomItem = (function () {
    // #region svgIcon
    const svgIcon = '<svg id="helloWorldItemIcon" viewBox="0 0 24 24"><path stroke="#42f48f" fill="#42f48f" d="M12 2 L2 22 L22 22 Z" /></svg>';
    // #endregion
    // #region metadata
    const helloWorldItemMetaData = {
        customProperties: [{
            ownerType: DevExpress.Dashboard.Model.CustomItem,
            propertyName: 'customProperty',
            valueType: 'string',
            defaultValue: 'Hello World!'
        }],
        optionsPanelSections: [{
            title: 'Custom Properties',
            items: [{
                dataField: 'customProperty',
                label: {
                    text: 'Item Text'
                },
                editorType: 'dxTextBox',
                editorOptions: {
                    placeholder: "Enter text to display"
                }
            }]
        }],
        icon: 'helloWorldItemIcon',
        title: 'Hello World Item'
    };
    // #endregion
    // #region viewer
    class HelloWorldItemViewer extends DevExpress.Dashboard.CustomItemViewer {
        constructor(model, $container, options) {
            super(model, $container, options);
        }
        renderContent = function ($element, changeExisting) {
            var element = $element.jquery ? $element[0] : $element;
            element.innerText = this.getPropertyValue('customProperty');
        }
    }
    // #endregion
    // #region createItem
    class HelloWorldItem {
        constructor(dashboardControl) {
            DevExpress.Dashboard.ResourceManager.registerIcon(svgIcon);
            this.name = "helloWorldItem";
            this.metaData = helloWorldItemMetaData;
        }
        createViewerItem(model, $element, content) {
            return new HelloWorldItemViewer(model, $element, content);
        }
    }
    return HelloWorldItem;
    // #endregion
})();

Register the Custom Item Extension

Attach the created HelloWorldItem.js script file to the page containing the Web Dashboard code. The external script you created is now available in the application.

You need to register the extension before the control is rendered. Refer to the following topics for information on how to get access to the DashboardControl instance and customize it before any element in the Web Dashboard control has been rendered:

Register the created extension in the DashboardControl.registerExtension method call:

js
function onBeforeRender(e) { 
  var dashboardControl = e.component;
  dashboardControl.registerExtension(new DashboardPanelExtension(dashboardControl));
  dashboardControl.registerExtension(new HelloWorldItem(dashboardControl));
  // ...
}
razor
function onBeforeRender(dashboardControl) {
    dashboardControl.registerExtension(new DevExpress.Dashboard.DashboardPanelExtension(dashboardControl));
    dashboardControl.registerExtension(new HelloWorldCustomItem(dashboardControl));
    <!-- // ... -->
}

Result

Run the project and click the Hello World Item Toolbox item to add the custom item to the dashboard:

This action adds the custom item with the predefined ‘Hello World!’ text on its surface. Open the item’s Options menu and go to the Custom Properties section to change the displayed text:

In the next lesson, you will learn how to create a custom item that is bound to data, and how to display data from a data source.