简体   繁体   中英

dijit.tree with empty folders

i have created a tree select that shows a dijit.tree in the dropdown. Now I do not want the user to select a folder even if it is empty. User should only be able to select the end nodes or leaves. dijit.tree treats all empty folders as leafs. how do I get that sorted?

You need to override the _onClick or setSelected methods. This gets complicated if you use the multi-parental model ForestStoreModel.

See fiddle.net

Try doing as such, this will only work for select multiple false:

    getIconClass: function fileIconClass(item, nodeExpanded) {
        var store = item._S,
            get = function() {
                return store.getValue(item, arguments[0]);
            };
        // scope: dijit.Tree
        if (item.root || get("isDir")) {

            if (!item || this.model.mayHaveChildren(item) || get("isDir")) {
                return (nodeExpanded ? "dijitFolderOpened" : "dijitFolderClosed");
            } else {
                return "dijitLeaf";
            }

        } else {

            return "dijitLeaf";

        }
    },
    onClick: function(item, treeNode, e) {

        var store = item._S,
            get = function() {
                return store.getValue(item, arguments[0]);
            };
        if (get("isDir")) this.set("selectedItems", []);
    }

Adapt as you see fit, matching your json data - in particular the isDir , the above works on a sample of json like this

{
    identifier: 'id',
    label: 'foo',
    items: [
        {
        id: 'item1',
        foo: 'file1',
        isDir: false},
    {
        id: 'item2',
        foo: 'emptyDir',
        isDir: true},
    {
        id: 'item3',
        foo: 'dir',
        isDir: true,
        children: [
            {
            id: 'item3_1',
            foo: 'fileInDir',
            isDir: false}
        ]}
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM