zoukankan      html  css  js  c++  java
  • easyui tree 模仿ztree 使用扁平化加载json

    1,载入扩展JS

    //作者孙宇
    //自定义loadFilter的实现
    $.fn.tree.defaults.loadFilter = function (data, parent) {
        var opt = $(this).data().tree.options;
        var idFiled,
        textFiled,
        parentField;
        if (opt.parentField) {
            idFiled = opt.idFiled || 'id';
            textFiled = opt.textFiled || 'text';
            parentField = opt.parentField;
             
            var i,
            l,
            treeData = [],
            tmpMap = [];
             
            for (i = 0, l = data.length; i < l; i++) {
                tmpMap[data[i][idFiled]] = data[i];
            }
             
            for (i = 0, l = data.length; i < l; i++) {
                if (tmpMap[data[i][parentField]] && data[i][idFiled] != data[i][parentField]) {
                    if (!tmpMap[data[i][parentField]]['children'])
                        tmpMap[data[i][parentField]]['children'] = [];
                    data[i]['text'] = data[i][textFiled];
                    tmpMap[data[i][parentField]]['children'].push(data[i]);
                } else {
                    data[i]['text'] = data[i][textFiled];
                    treeData.push(data[i]);
                }
            }
            return treeData;
        }
        return data;
    };

    2.举例

    <ul id="tt"></ul> 

    //
    实例化。这里增加了三个属性,可以指定idFiled,textFiled和parentField。所以这里的simpleData可以不严格转换成tree的数据格式。 $(function(){ $('#tt').tree({ checkbox: true, url: 'tree_data_simp.json', parentField:"pid", textFiled:"name", idFiled:"key" }); });

    3.JSON

    [
        {
            "key": 1,
            "name": "Folder1",
            "iconCls": "icon-ok"
        },
        {
            "key": 2,
            "pid": 1,
            "name": "File1",
            "checked": true
        },
        {
            "key": 3,
            "pid": 1,
            "name": "Folder2",
            "state": "open"
        },
        {
            "key": 4,
            "pid": 3,
            "name": "File3",
            "attributes": {
                "p1": "value1",
                "p2": "value2"
            },
            "checked": true,
            "iconCls": "icon-reload"
        },
        {
            "key": 8,
            "pid": 3,
            "name": "Async Folder"
        },
        {
            "key": 9,
            "name": "language",
            "state": "closed"
        },
        {
            "key": "j1",
            "pid": 9,
            "name": "Java"
        },
        {
            "key": "j2",
            "pid": 9,
            "name": "C#"
        }
    ]

    4.扩展

    ComboTree(树形下拉框)

    扩展自$.fn.combo.defaults和$.fn.tree.defaults。使用$.fn.combotree.defaults重写默认值对象。

    所以要想使ConboTree也可以加载扁平数据,可如下修改

    $.fn.combotree.defaults.loadFilter = function(data, parent) {
        var opt = $(this).data().tree.options;
        var idFiled, textFiled, parentField;
        if (opt.parentField) {
            idFiled = opt.idFiled || 'id';
            textFiled = opt.textFiled || 'text';
            parentField = opt.parentField;
    
            var i, l, treeData = [], tmpMap = [];
    
            for (i = 0, l = data.length; i < l; i++) {
                tmpMap[data[i][idFiled]] = data[i];
            }
    
            for (i = 0, l = data.length; i < l; i++) {
                if (tmpMap[data[i][parentField]] && data[i][idFiled] != data[i][parentField]) {
                    if (!tmpMap[data[i][parentField]]['children'])
                        tmpMap[data[i][parentField]]['children'] = [];
                    data[i]['text'] = data[i][textFiled];
                    tmpMap[data[i][parentField]]['children'].push(data[i]);
                } else {
                    data[i]['text'] = data[i][textFiled];
                    treeData.push(data[i]);
                }
            }
            return treeData;
        }
        return data;
    };
  • 相关阅读:
    我在互联网的第一年
    变速变调原理与方法总结
    TIMIT语音库
    基于HTK语音工具包进行孤立词识别的使用教程
    Praat 音频分析 人工标注
    HTK源码学习总结
    HTK搭建语音拨号系统实验材料下载
    应用HTK搭建语音拨号系统4: 识别器评估
    应用HTK搭建语音拨号系统3:创建绑定状态的三音素HMM模型
    应用HTK搭建语音拨号系统2:创建单音素HMM模型
  • 原文地址:https://www.cnblogs.com/liaojie970/p/5319252.html
Copyright © 2011-2022 走看看