PDA

View Full Version : select/deselect children of tree node, extjs 4



GeorgeHernandez
2 Nov 2011, 9:19 AM
This is solves a common little problem, so I thought I'd share it. Double-clicking on a tree node will select its children (if any are not selected) or deselect its children (if all were selected). I used the beforeitemdblclick event instead of itemdblclick in order to avoid the auto-collapse of the children.


Ext.define('MyNS.controller.MyController', {
extend: 'Ext.app.Controller',
init: function () {
this.control({
'mytree': {beforeitemdblclick: this.clickChildren}
});
},
clickChildren: function (view, record, item, index, evObj, eOpts) {
var hasUnchecked = false;
record.eachChild(function (n) {
if (!n.data.checked) {
hasUnchecked = true;
}
});
if (hasUnchecked) {
record.eachChild(function (n) {
n.set('checked', true);
});
} else {
record.eachChild(function (n) {
n.set('checked', false);
});
}
return false; // Otherwise will collapse children
}
});

mitchellsimoens
2 Nov 2011, 9:46 AM
Nice work!

I moved to the user extensions and plugins as this is probably where people would come to find things like this.