DevExtreme Vue - In the Data Source

Items in the TagBox can be grouped if they are grouped in the data source. The TagBox recognizes a group when it finds an object with the key and items fields. The key is the group header, the items are items that fall into the group. For example, the fruitsVegetables array from the following code produces two groups with three items each. Note that the TagBox needs to be informed that it deals with grouped data, therefore its grouped option is set to true.

JavaScript
var fruitsVegetables = [{
    key: "Fruits",
    items: [
        { name: "Apples", count: 10 },
        { name: "Oranges", count: 12 },
        { name: "Lemons", count: 15 }
    ]
}, {
    key: "Vegetables",
    items: [
        { name: "Potatoes", count: 5 },
        { name: "Tomatoes", count: 9 },
        { name: "Turnips", count: 8 }
    ]
}];

$(function() {
    $("#tagBoxContainer").dxTagBox({
        dataSource: fruitsVegetables,
        grouped: true,
        displayExpr: 'name',
        valueExpr: 'count'
    });
});
NOTE
Only one-level grouping is supported.

If objects in your data source miss the key and items fields, use the map function of the DevExtreme DataSource to create key + items field mappings. You can find more information on the map function in the Data Layer - Item Mapping topic.

JavaScript
var fruitsVegetables = [{
    type: "Fruits",
    collection: [
        { name: "Apples", count: 10 },
        { name: "Oranges", count: 12 },
        { name: "Lemons", count: 15 }
    ]
}, {
    type: "Vegetables",
    collection: [
        { name: "Potatoes", count: 5 },
        { name: "Tomatoes", count: 9 },
        { name: "Turnips", count: 8 }
    ]
}];

$(function() {
    $("#tagBoxContainer").dxTagBox({
        dataSource: new DevExpress.data.DataSource({
            store: fruitsVegetables,
            map: function(item) {
                return {
                    key: item.type,
                    items: item.collection
                }
            }
        }),
        grouped: true,
        displayExpr: 'name',
        valueExpr: 'count'
    });
});

If your data is not grouped at all, you can group it using the group option of the DataSource. See the Data Layer - Grouping topic for details.

JavaScript
var fruitsVegetables = [
    { type: "Fruits", name: "Apples", count: 10 },
    { type: "Fruits", name: "Oranges", count: 12 },
    { type: "Fruits", name: "Lemons", count: 15 },
    { type: "Vegetables", name: "Potatoes", count: 5 },
    { type: "Vegetables", name: "Tomatoes", count: 9 },
    { type: "Vegetables", name: "Turnips", count: 8 }
];

$(function() {
    $("#tagBoxContainer").dxTagBox({
        dataSource: new DevExpress.data.DataSource({
            store: fruitsVegetables,
            group: "type"
        }),
        grouped: true,
        displayExpr: 'name',
        valueExpr: 'count'
    });
});

View Demo

See Also