window.onload = function () {
const checkedItems = ko.observableArray([]);
const viewModel = {
treeViewOptions: {
items: products,
width: 320,
showCheckBoxesMode: 'normal',
onItemSelectionChanged(e) {
const item = e.node;
if (isProduct(item)) {
processProduct($.extend({
category: item.parent.text,
}, item));
} else {
$.each(item.items, (index, product) => {
processProduct($.extend({
category: item.text,
}, product));
});
}
},
},
listOptions: {
width: 400,
items: checkedItems,
},
};
function isProduct(data) {
return !data.items.length;
}
function processProduct(product) {
let itemIndex = -1;
$.each(checkedItems(), (index, item) => {
if (item.key === product.key) {
itemIndex = index;
return false;
}
return true;
});
if (product.selected && itemIndex === -1) {
checkedItems.push(product);
} else if (!product.selected) {
checkedItems.splice(itemIndex, 1);
}
}
ko.applyBindings(viewModel, document.getElementById('treeview'));
};
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>DevExtreme Demo</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>window.jQuery || document.write(decodeURIComponent('%3Cscript src="js/jquery.min.js"%3E%3C/script%3E'))</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/22.2.3/css/dx.light.css" />
<script src="https://cdn3.devexpress.com/jslib/22.2.3/js/dx.all.js"></script>
<script src="data.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css" />
<script src="index.js"></script>
</head>
<body class="dx-viewport">
<div class="demo-container">
<div class="form" id="treeview">
<h4>Store: Super Mart of the West</h4>
<div data-bind="dxTreeView: treeViewOptions">
<div data-options="dxTemplate: {name: 'item'}">
<div data-bind="text: $data.text +
(($data.price) ? (' ($' + $data.price + ')') : '')">
</div>
</div>
</div>
<div class="selected-data">
Selected Products
<div id="checked-items" data-bind="dxList: listOptions">
<div data-options="dxTemplate: {name: 'item'}">
<div data-bind="text: $data.text +
' (' + $data.category + ') - $' + $data.itemData.price">
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
.form > h4 {
margin-bottom: 20px;
}
.form > div,
#selection-treeview {
display: inline-block;
vertical-align: top;
}
#checked-items {
margin-top: 20px;
}
.selected-data {
padding: 20px;
background: #f5f5f5;
font-size: 115%;
font-weight: bold;
}
.dx-list-item-content {
padding-left: 0;
}
const products = [{
id: '1_1_1',
text: 'Video Players',
items: [{
id: '1_1_1_1',
text: 'HD Video Player',
price: 220,
}, {
id: '1_1_1_2',
text: 'SuperHD Video Player',
price: 270,
}],
}, {
id: '1_1_2',
text: 'Televisions',
expanded: true,
items: [{
id: '1_1_2_1',
text: 'SuperLCD 42',
price: 1200,
}, {
id: '1_1_2_2',
text: 'SuperLED 42',
price: 1450,
}, {
id: '1_1_2_3',
text: 'SuperLED 50',
price: 1600,
}, {
id: '1_1_2_4',
text: 'SuperLCD 55',
price: 1350,
}, {
id: '1_1_2_5',
text: 'SuperLCD 70',
price: 4000,
}],
}, {
id: '1_1_4',
text: 'Projectors',
items: [{
id: '1_1_4_1',
text: 'Projector Plus',
price: 550,
}, {
id: '1_1_4_2',
text: 'Projector PlusHD',
price: 750,
}],
}];