JavaScript/jQuery SelectBox - Customize Item Appearance

For a minor customization of SelectBox items, you can define specific fields in their data objects. For example, the following code generates three items: the first is not customized, the second is disabled and the third is hidden.

TypeScript
HTML
  • import { DxSelectBoxModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • products = [
  • { text: "HD Video Player" },
  • { text: "SuperHD Video Player", disabled: true },
  • { text: "SuperPlasma 50", visible: false }
  • ];
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxSelectBoxModule
  • ],
  • // ...
  • })
  • <dx-select-box
  • [dataSource]="products"
  • valueExpr="text"
  • displayExpr="text"
  • placeholder="Select a product...">
  • </dx-select-box>

If you need a more flexible solution, specify itemTemplate.

HTML
TypeScript
  • <dx-select-box
  • [dataSource]="selectBoxData"
  • displayExpr="name"
  • valueExpr="id"
  • itemTemplate="item">
  • <div *dxTemplate="let data of 'item'">
  • <img src="{{data.imgSrc}}" />
  • <div style="display:inline-block">{{data.name}}</div>
  • </div>
  • </dx-select-box>
  • import { DxSelectBoxModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • selectBoxData = [{
  • id: 1,
  • name: "HD Video Player",
  • imgSrc: "images/products/1-small.png"
  • }, {
  • id: 2,
  • name: "UltraHD Player",
  • imgSrc: "images/products/2-small.png"
  • },
  • // ...
  • ];
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxSelectBoxModule
  • ],
  • // ...
  • })

You can also customize individual SelectBox items. Declare them using the dxItem component.

HTML
TypeScript
  • <dx-select-box>
  • <dxi-item>
  • <span>User</span>
  • </dxi-item>
  • <dxi-item>
  • <span>Comment</span>
  • </dxi-item>
  • </dx-select-box>
  • import { DxSelectBoxModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • // ...
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxSelectBoxModule
  • ],
  • // ...
  • })

Using similar techniques, you can customize the input field of the SelectBox. The template for it should be assigned to the fieldTemplate property. Note that the input field must contain the TextBox UI component.

HTML
TypeScript
  • <dx-select-box
  • [dataSource]="selectBoxData"
  • valueExpr="id"
  • [value]="1"
  • displayExpr="name"
  • fieldTemplate="inputField">
  • <div *dxTemplate="let data of 'inputField'">
  • <img ng-src="{{data.imgSrc}}" />
  • <dx-text-box style="display:inline-block"
  • [value]="data.name"
  • ></dx-text-box>
  • </div>
  • </dx-select-box>
  • import { DxSelectBoxModule, DxTextBoxModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • selectBoxData = [
  • { id: 1, name: "HD Video Player", imgSrc: "images/products/1-small.png" },
  • // ...
  • ];
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxSelectBoxModule,
  • DxTextBoxModule
  • ],
  • // ...
  • })
See Also