• easyui-combobox加载json中对象的属性---datagrid的数据


     这次做项目遇到了一个问题,在easyui-combobox加载json数据中对象的属性。后台返回的json中包含一个对象,而我想调用对象中的属性,后台返回的json如下:

    1 {    "rows": [        {            "chances": [],            "name": "系统管理员",            "password": "admin",            "status": 1,            "userId": 1,            "userName": "admin"        },        {            "chances": [],            "name": "于经理",            "password": "123456",            "status": 1,            "userId": 2,            "userName": "yulibin"        },        {            "chances": [],            "name": "郝经理",            "password": "123456",            "status": 1,            "userId": 3,            "userName": "haominghui"        },        {            "chances": [],            "name": "吴经理",            "password": "123456",            "status": 1,            "userId": 4,            "userName": "wumingliang"        }    ],    "total": 4}
    {
        "rows": [
            {
                "chances": [],
                "name": "系统管理员",
                "password": "admin",
                "status": 1,
                "userId": 1,
                "userName": "admin"
            },
            {
                "chances": [],
                "name": "于经理",
                "password": "123456",
                "status": 1,
                "userId": 2,
                "userName": "yulibin"
            },
            {
                "chances": [],
                "name": "郝经理",
                "password": "123456",
                "status": 1,
                "userId": 3,
                "userName": "haominghui"
            },
            {
                "chances": [],
                "name": "吴经理",
                "password": "123456",
                "status": 1,
                "userId": 4,
                "userName": "wumingliang"
            }
        ],
        "total": 4
    }

    json中是datagrid 格式包含total和rows,而rows下面才是我要显示在combobox的值,API规定必须用下面格式的json:

    [{
        "id":1,
        "text":"text1"
    },{
        "id":2,
        "text":"text2"
    },{
        "id":3,
        "text":"text3",
        "selected":true
    },{
        "id":4,
        "text":"text4"
    },{
        "id":5,
        "text":"text5"
    }]

    按着常规讲我会像json.rows.userId 这样设置valueField属性,结果明显不行,但这并不出乎我的意料,就是出于尝试的心态。现在的问题是如何获得这个json的对象,并且调用时只用属性名userId,name。
    其实,很简单就解决了,还是先看api示例

    $('#cc').combobox({
        url:'combobox_data.json',
        valueField:'id',
        textField:'text'
    });
    

     由于json数据都是对象,所以可以处理为

    var url = "${pageContext.request.contextPath}/user_findUlist.action";
    $.getJSON(url, function(json) {
        $('#cc').combobox({
            data : json.rows,
            valueField:'userId',
            textField:'name'
        });
    });
    

     

    combobox必须按着api指定格式加载json数据,我这里的json是层层嵌套的,所以肯定不信了,那我们可以换个思路。调用Jquery提供的getJSON方法,获取到我json,在初始化combobox使指定data属性为:json.rows,OK,就这么简单!!!

  • 相关阅读:
    mysql分表技术(学习心得)
    Linux下搭建DNS服务器
    php简单单例模式
    JQuery的ajaxFileUpload图片上传初试
    Binary Tree Level Order Traversal II
    Remove Element
    Symmetric Tree
    Balanced Binary Tree
    Power of Two
    Merge Two Sorted Lists
  • 原文地址:https://www.cnblogs.com/tested/p/3272378.html
Copyright © 2020-2023  润新知