Create a Static Custom Item for the Web Dashboard
- 6 minutes to read
Important
A custom item is an independent module called the 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:
Prerequisites
A custom item script depends on the platform you use:
- Module script
- Client components (Angular, React, Vue) use imports and classes to define a custom item.
- Classic script
- A 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 Toolbox Icon
In the HelloWorldItem.js file, add a 24x24 SVG icon as a string variable:
var 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 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
).
var helloWorldItemMetaData = {
// ...
icon: 'helloWorldItemIcon',
title: 'Hello World Item'
};
As a result, the following icon is added to the Toolbox:
Create a Custom Property
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:
- Create the CustomPropertyMetadata object and define a string
customProperty
option. Add the created option to the ICustomItemMetaData.customProperties collection. - Create the SectionOptions object that defines a new Custom Properties section in the dashboard item’s Options menu. The SectionOptions.items collection contains editors used to manage custom property values in the UI. Define dxTextBox as an editor. Add the created option to the ICustomItemMetaData.optionsPanelSections collection.
import { CustomItem } from 'devexpress-dashboard/model'
// ...
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"
}
}]
}],
// ...
};
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, its viewer part (CustomItemViewer is copied with all its elements and the element ID will not be unique on the page.
import { CustomItemViewer, ResourceManager } from 'devexpress-dashboard/common'
// ...
class HelloWorldItemViewer extends CustomItemViewer {
renderContent(element, changeExisting) {
element.innerText = this.getPropertyValue('customProperty');
};
}
Create an Extension
The ICustomItemExtension interface allows you to define a custom item as an extension. Use the ResourceManager.registerIcon static method to register the custom item’s icon. Pass the created metadata to the ICustomItemExtension.metaData property. Call the ICustomItemExtension.createViewerItem method to implement the created custom item.
import { CustomItemViewer, ResourceManager } from 'devexpress-dashboard/common'
// ...
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;
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:
- Manage Extensions in ASP.NET Core
- Manage Extensions in ASP.NET MVC
- Manage Extensions in ASP.NET Web Forms
- Manage Extensions in Angular
- Manage Extensions in React
- Manage Extensions in Vue
- Manage Extensions in the JavaScript Dashboard Control
- JavaScript Customization of Blazor Dashboard Component
Register the created extension in the DashboardControl.registerExtension method call:
function onBeforeRender(e) {
var dashboardControl = e.component;
dashboardControl.registerExtension(new DashboardPanelExtension(dashboardControl));
dashboardControl.registerExtension(new HelloWorldItem(dashboardControl));
// ...
}
Run the Project to See the 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.
Export Limitations
A custom item does not support the default export mechanism. For example, you cannot use WebDashboardExporter / ASPxDashboardExporter to export custom items. In this instance, you need to implement custom export:
- On the client, override the CustomItemViewer.getExportInfo method and pass an object with the
image
field. Encode the image in the ASCII format using the Base64 scheme to display a custom item in the exported document. - If you cannot generate an image on the client, implement your own export logic on the server side. For this, handle the DashboardConfigurator.CustomExport / ASPxDashboard.CustomExport events.