如何检查json对象是否有子节点

时间:2014-04-09 07:09:24

标签: json sapui5

我有一个sapui5树表,它有2列(1:文本视图,2:下拉框)。表格数据:

var oData = [
                                {
            Scopes : "QT15", ExecutionGroup: "group2", 
            0 : {Scopes : "Job1",  ExecutionGroup: "",  
                0 : {Scopes : "Act1",  ExecutionGroup: "",
                    0 : {Scopes : "Material1",ExecutionGroup: ""},
                    1 : {Scopes : "Material2",ExecutionGroup: ""},
                    2 : {Scopes : "Material3",ExecutionGroup: ""}
                },
                1 : {Scopes : "Act2",  ExecutionGroup: ""},
                2 : {Scopes : "Act3",  ExecutionGroup: ""}
            },
            1 : {Scopes : "Job2",  ExecutionGroup: "", 
                0 : {Scopes : "11111",  ExecutionGroup: ""},
                1 : {Scopes : "22222",  ExecutionGroup: ""},
                2 : {Scopes : "33333",  ExecutionGroup: ""}
                }
            },
            {
            Scopes : "QT16",  ExecutionGroup: "",// ExecutionGroup: "exgroup1", 
            0 : {Scopes : "Job1",  ExecutionGroup: "", 
                0 : {Scopes : "11111",  ExecutionGroup: ""},
                1 : {Scopes : "22222",  ExecutionGroup: ""},
                2 : {Scopes : "33333",  ExecutionGroup: ""}
                },
            1 : {Scopes : "Job2",  ExecutionGroup: "", 
                0 : {Scopes : "11111",  ExecutionGroup: ""},
                1 : {Scopes : "22222",  ExecutionGroup: ""},
                2 : {Scopes : "33333",  ExecutionGroup: ""}
                }
            }
        ];

DropDown的数据:          var dataDD = [                     {值: “组1”},                     {值: “组2”},                     {值: “组3”}                 ];

将数据提交给模型:

var oModel = new sap.ui.model.json.JSONModel();
    oModel.setData({
    oData: oData,
    dataDD: dataDD
});

sap.ui.getCore().setModel(oModel);

模型绑定到表,DropDown设置为列上的模板。 应该来的功能,如果用户从DD中选择任何值,那么  该节点的所有子节点的默认DD值必须更改为该值。  最初DD的值是“group1”。如果父DD值更改为“group2”,  显示的儿童DD值必须设置为“group2”) 我想在DD更改时触发一个函数。

var oDropdownBox = new sap.ui.commons.DropdownBox({
    change: function(evt){

        var path = evt.getSource().getBindingContext().getPath();
        var value = evt.getSource().getValue();
        var currentContextObj = sap.ui.getCore().getModel().getProperty(path);

        console.log("path: " + path);
        refreshChilds();
    }
});

如何检查树上的节点是否有子节点?非常感谢!

1 个答案:

答案 0 :(得分:2)

不确定我完全理解问题,但要查看上下文是否有0或1的孩子

var obj = evt.getSource().getBindingContext().getObject();

var hasChild = (obj.hasOwnProperty(0) || obj.hasOwnProperty(1));