The solution is:
var collapsed = [];
function OnClientLoadHandler(sender, args) {
$(sender.get_filterElement()).on('keydown', function (e) {
var ddt = sender;
var embeddedTree = ddt.get_embeddedTree();
var nodes = embeddedTree.get_allNodes();
if (ddt.get_filterElement().value == "") {
collapsed = [];
for (var i = 0; i < nodes.length; i++) {
if (!nodes[i].get_expanded()) {
collapsed.push(nodes[i]);
}
}
}
})
$(sender.get_filterElement()).on('keyup', function (e) {
var ddt = sender;
var tree = ddt.get_embeddedTree()
var allNodes = tree.get_allNodes();
if (ddt.get_filterElement().value == "") {
setTimeout(function () {
for (var i = 0; i < collapsed.length; i++) {
var node = tree.findNodeByValue(collapsed[i].get_value());
node.collapse();
}
}, 200);
}
})
}