• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
风拂晚柳
博客园    首页    新随笔    联系   管理    订阅  订阅

EasyUI学习-----DataGrid动态创建列

1.JSON本地文件

{
    "total": 2,
    "rows": [
        {
            "code": 1001,
            "name": "苹果",
            "price": 8.00
        },
        {
            "code": 1002,
            "name": "葡萄",
            "price": 10.00
        }
    ]
}

2.动态创建列

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title>动态生成列</title>
        <link rel="stylesheet" type="text/css" href="jquery-easyui-1.5.1/themes/default/easyui.css">
        <link rel="stylesheet" type="text/css" href="jquery-easyui-1.5.1/themes/icon.css">
        <script type="text/javascript" src="jquery-easyui-1.5.1/jquery.min.js"></script>
        <script type="text/javascript" src="jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="jquery-easyui-1.5.1/locale/easyui-lang-zh_CN.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#dg').datagrid({
                    title: '商品信息',
                    fitColumns: true,
                });

                $.ajax({
                    type: 'get',
                    url: 'datagridData.json',
                    dataType: 'json',
                    success: function(data) {
                        console.log(JSON.stringify(data.rows))
                        var totalData = data.rows;
                        var cols = [];
                        //获取属性名称
                        for(var st in totalData[0]) {
                            //动态生成列        
                            cols.push({
                                "field": st,
                                "title": st,
                                "width": 100,
                                align: "center"
                            });
                        }
                                        
                        $("#dg").datagrid({
                            columns: [cols]
                        }).datagrid('loadData', data.rows);
                    }
                })
            })
        </script>
    </head>

    <body>
        <table id="dg" style="width: 500px;height: 300px;"></table>
    </body>

</html>

 

posted @ 2019-07-05 17:03  风拂晚柳  阅读(482)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3