ej.base.enableRipple(true);
var dataClients = new ej.data.DataManager({
url: 'SchedulersAdd/LoadClients',
adaptor: new ej.data.UrlAdaptor(),
crossDomain: true,
});
var listTreeObj = new ej.navigations.TreeView({
fields: { dataSource: dataClients, id: 'Id', text: 'ClientName', child: 'child', selected: 'isSelected' },
allowDragAndDrop: true,
addClass: "treeview-external-drag",
nodeDragStop: "onTreeDragStop",
nodeDragging: "onItemDrag",
nodeTemplate: "#treeTemplate"
});
listTreeObj.appendTo('#tree');
// Render the MaskedTextBox input element
var mask = new ej.inputs.MaskedTextBox({
placeholder: "Enter the tree node to search",
change: searchNodes
});
mask.appendTo('#inputBox');
// Change the dataSource for TreeView
function changeDataSource() {
listTreeObj.fields = { dataSource: dataClients, id: 'Id', text: 'ClientName', child: 'child', selected: 'isSelected' };
listTreeObj.allowDragAndDrop = true;
}
// Filtering the tree nodes
function searchNodes(args) {
//var listTreeObj = ej.base.getComponent(document.querySelector('#tree'), 'treeview');
var _text = mask.element.value;
var predicats = [], _array = [], _filter = [];
if (_text == "") {
//changeDataSource(listTreeObj.getTreeData());
changeDataSource();
}
else {
var predicate = new ej.data.Predicate('name', 'contains', _text, true);
var filteredList = new ej.data.DataManager(dataClients).executeLocal(new ej.data.Query().where(predicate));
for (var j = 0; j < filteredList.length; j++) {
_filter.push(filteredList[j]["id"]);
var filters = getFilterItems(filteredList[j], dataClients);
for (var i = 0; i < filters.length; i++) {
if (_array.indexOf(filters[i]) == -1 && filters[i] != null) {
_array.push(filters[i]);
predicats.push(new ej.data.Predicate('id', 'equal', filters[i], false));
}
}
}
if (predicats.length == 0) {
changeDataSource([]);
} else {
var query = new ej.data.Query().where(new ej.data.Predicate.or(predicats));
var newList = new ej.data.DataManager(dataClients).executeLocal(query);
changeDataSource(newList);
setTimeout(function () {
listTreeObj.expandAll();
}, 100);
}
}
}
// Find the parent nodes for corresponding childs
function getFilterItems(fList, list) {
var nodes = [];
nodes.push(fList["id"]);
var query2 = new ej.data.Query().where('id', 'equal', fList["pid"], false);
var fList1 = new ej.data.DataManager(dataClients).executeLocal(query2);
if (fList1.length != 0) {
var pNode = getFilterItems(fList1[0], list);
for (var i = 0; i < pNode.length; i++) {
if (nodes.indexOf(pNode[i]) == -1 && pNode[i] != null)
nodes.push(pNode[i]);
}
return nodes;
}
return nodes;
}