DevExtreme React - Customize Item Appearance

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

jQuery
JavaScript
$(function() {
    $("#selectBoxContainer").dxSelectBox({
        valueExpr: 'text',
        displayExpr: 'text',
        dataSource: [
            { text: "HD Video Player" },
            { text: "SuperHD Video Player", disabled: true },
            { text: "SuperPlasma 50", visible: false }
        ],
        placeholder: "Select a product..."
    });
});
Angular
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>
Vue
App.vue
<template> 
    <DxSelectBox ...
        :data-source="products"
        display-expr="text"
        value-expr="text"
    />
</template>

<script>
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import { DxSelectBox } from 'devextreme-vue/select-box';

export default {
    components: {
        DxSelectBox
    },
    data() {
        const products = [
            { text: "HD Video Player" },
            { text: "SuperHD Video Player", disabled: true },
            { text: "SuperPlasma 50", visible: false }
        ];
        return {
            products
        }
    }
}
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import SelectBox from 'devextreme-react/select-box';

const products = [
    { text: "HD Video Player" },
    { text: "SuperHD Video Player", disabled: true },
    { text: "SuperPlasma 50", visible: false }
];

class App extends React.Component {
    render() {
        return (
            <SelectBox ...
                dataSource={products} 
                displayExpr="text"
                valueExpr="text"
            />
        );
    }
}
export default App;

If you need a more flexible solution, define a custom template for widget items. For AngularJS and Knockout apps, DevExtreme provides the dxTemplate markup component. The following code shows how to use dxTemplate to define a template for the SelectBox items.

Angular
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
     ],
     // ...
 })
Vue
App.vue
<template> 
    <DxSelectBox ...
        :data-source="selectBoxData"
        display-expr="name"
        value-expr="id"
        item-template="item"
    >
        <template #item="{ data }">
            <div>
                <img :src="data.imgSrc">
                <div style="display:inline-block">
                    {{ data.name }}
                </div>
            </div>
        </template>
    </DxSelectBox>
</template>

<script>
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import { DxSelectBox } from 'devextreme-vue/select-box';

export default {
    components: {
        DxSelectBox
    },
    data() {
        const selectBoxData = [{
            id: 1,
            name: "HD Video Player",
            imgSrc: "images/products/1-small.png"
        }, {
            id: 2,
            name: "UltraHD Player",
            imgSrc: "images/products/2-small.png"
        },
        // ...
        ];
        return {
            selectBoxData
        }
    }
}
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import SelectBox from 'devextreme-react/select-box';

const renderItem = (data) => {
    return (
        <div>
            <img src={data.imgSrc} />
            <div style="display:inline-block">{data.name}</div>
        </div>
    );
}

const selectBoxData = [{
    id: 1,
    name: "HD Video Player",
    imgSrc: "images/products/1-small.png"
}, {
    id: 2,
    name: "UltraHD Player",
    imgSrc: "images/products/2-small.png"
},
// ...
];

class App extends React.Component {
    render() {
        return (
            <SelectBox ...
                dataSource={selectBoxData} 
                displayExpr="name"
                valueExpr="id"
                itemRender={renderItem}
            />
        );
    }
}
export default App;
AngularJS
HTML
JavaScript
<div ng-controller="DemoController">
    <div dx-select-box="{
        dataSource: selectBoxData,
        valueExpr: 'id',
        displayExpr: 'name',
        itemTemplate: 'item'
    }" dx-item-alias="product">
        <div data-options="dxTemplate: { name: 'item' }">
            <img ng-src="{{product.imgSrc}}" />
            <div style="display:inline-block">{{product.name}}</div>
        </div>
    </div>
</div>
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function ($scope) {
        $scope.selectBoxData = [{
            id: 1,
            name: "HD Video Player",
            imgSrc: "images/products/1-small.png"
        }, {
            id: 2,
            name: "UltraHD Player",
            imgSrc: "images/products/2-small.png"
        },
        // ...
        ];
    });
NOTE
The dx-item-alias directive specifies the variable that is used to access the item object.
Knockout
HTML
JavaScript
<div data-bind="dxSelectBox: {
    dataSource: selectBoxData,
    valueExpr: 'id',
    displayExpr: 'name',
    itemTemplate: 'item'
}">
    <div data-options="dxTemplate: { name: 'item' }">
        <img data-bind="attr: { src: imgSrc }" />
        <div style="display:inline-block" data-bind="text: name"></div>
    </div>
</div>
var viewModel = {
    selectBoxData: [{
        id: 1,
        name: "HD Video Player",
        imgSrc: "images/products/1-small.png"
    }, {
        id: 2,
        name: "UltraHD Player",
        imgSrc: "images/products/2-small.png"
    },
    // ...
    ]
};

ko.applyBindings(viewModel);

If you use jQuery alone, use DOM manipulation methods to combine the HTML markup for items. To apply this markup, use the itemTemplate callback function as shown in the following code.

JavaScript
var selectBoxData = [{
    id: 1,
    name: "HD Video Player",
    imgSrc: "images/products/1-small.png"
}, {
    id: 2,
    name: "UltraHD Player",
    imgSrc: "images/products/2-small.png"
},
// . . .
];

$(function() {
    $("#selectBoxContainer").dxSelectBox({
        dataSource: selectBoxData,
        valueExpr: 'id',
        displayExpr: 'name',
        itemTemplate: function (itemData, itemIndex, itemElement) {
            return $("<div />").append(
                $("<img />").attr("src", itemData.imgSrc),
                $("<p />").text(itemData.name)
                          .css("display", "inline-block")
            );
        }
    });
});

You can also customize an individual SelectBox item. For this purpose, declare a template for this item as a script and pass its id to the template field.

HTML
JavaScript
<script id="individualTemplate" type="text/html">
    <!-- ... -->
</script>
var selectBoxData = [
    { text: "SuperHD Player"},
    { text: "HD Video Player", template: $("#individualTemplate") },
    // . . .
];

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

jQuery
JavaScript
$(function() {
    $("#selectBoxContainer").dxSelectBox({
        dataSource: selectBoxData,
        valueExpr: 'id',
        displayExpr: 'name',
        value: 1,
        fieldTemplate: function(selectedItem, fieldElement) {
            return $("<div />").append(
                $("<img />").attr("src", selectedItem.imgSrc),
                $("<div />").dxTextBox({ value: selectedItem.name })
                            .css("display", "inline-block")
            );
        }
    });
});
Angular
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
     ],
     // ...
 })
Vue
App.vue
<template> 
    <DxSelectBox ...
        :data-source="selectBoxData"
        :value="1"
        display-expr="name"
        value-expr="id"
        field-template="field"
    >
        <template #field='{ data }'>
            <div>
                <img :src="data.imgSrc">
                <DxTextBox
                    :value="data.name"
                    style="display:inline-block"
                />
            </div>
        </template>
    </DxSelectBox>
</template>

<script>
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import { DxSelectBox } from 'devextreme-vue/select-box';
import { DxTextBox } from 'devextreme-vue/text-box';

export default {
    components: {
        DxSelectBox,
        DxTextBox
    },
    data() {
        const selectBoxData = [{
            id: 1,
            name: "HD Video Player",
            imgSrc: "images/products/1-small.png"
        }, {
            id: 2,
            name: "UltraHD Player",
            imgSrc: "images/products/2-small.png"
        },
        // ...
        ];
        return {
            selectBoxData
        }
    }
}
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import { SelectBox } from 'devextreme-react/select-box';
import { TextBox } from 'devextreme-react/text-box';

const renderField = (data) => {
    return (
        <div>
            <img src={data.imgSrc} />
                <TextBox style={{display: 'inline-block'}}
                    defaultValue={data.name} />
        </div>
    );
}

const selectBoxData = [{
    id: 1,
    name: "HD Video Player",
    imgSrc: "images/products/1-small.png"
}, {
    id: 2,
    name: "UltraHD Player",
    imgSrc: "images/products/2-small.png"
},
// ...
];

class App extends React.Component {
    render() {
        return (
            <SelectBox ...
                dataSource={selectBoxData} 
                value={1}
                displayExpr="name"
                valueExpr="id"
                fieldRender={renderField}
            />
        );
    }
}
export default App;
AngularJS
HTML
<div ng-controller="DemoController">
    <div dx-select-box="{
        dataSource: selectBoxData,
        valueExpr: 'id',
        value: 1,
        displayExpr: 'name',
        fieldTemplate: 'inputField'
    }" dx-item-alias="product">
        <div data-options="dxTemplate: { name: 'inputField' }">
            <img ng-src="{{ product.imgSrc }}" />
            <div style="display:inline-block" dx-text-box="{ value: product.name }"></div>
        </div>
    </div>
</div>
Knockout
HTML
<div data-bind="dxSelectBox: {
    dataSource: selectBoxData,
    valueExpr: 'id',
    value: 1,
    displayExpr: 'name',
    fieldTemplate: 'inputField'
}">
    <div data-options="dxTemplate: { name: 'inputField' }">
        <img data-bind="attr: { src: imgSrc }" />
        <div style="display:inline-block" data-bind="dxTextBox: { value: name }"></div>
    </div>
</div>

In addition, you can use a 3rd-party template engine to perform the needed customizations. For more information, see the 3rd-Party Template Engines article.

See Also