easyUI之datagrid绑定后端返回数据的两种方式

先来看一下某一位大佬留下的easyUI的API对datagrid绑定数据的两种方式的介绍。url

虽然精简,可是,很具备“师傅领进门,修行靠我的”的精神,先发自心里的赞一个。spa

可是,不少人和小编同样,第一次接触easyUI,对这么精简的问题,问题颇多,所以,小编在这里献上一份我的认为比较详尽的版本code

 

经过HTML/JSP页面初始化表格,JS绑定数据orm

在JSP中定义table和列名,以及列属性。blog

列属性却不定义在data-option属性中,field对应的字段名,需和后台返回的字段名相同。ip

    <table id="good_tables" style="height: 484px;">
        <thead>
        <tr>
           <th data-options="field:'id',sortable:true">商品ID</th>
            <th data-options="field:'goodsName'">商品名称</th>
            <th data-options="field:'supplier'">供应商</th>
        </tr>
        </thead>
    </table>

在JS文件中获取并绑定数据rem

$(document).ready(function () {
    initGoodsTable();
});

function initGoodsTable(){
    $('#good_tables').datagrid({
        nowrap: true,
        autoRowHeight: true,
        striped: true,
        fitColumns: true,
        collapsible: true,
        url: 'xxx',
        border: false,
        idField: 'id',
        selectOnCheck: true,
        singleSelect: true,
        width:'100%' ,
        resizable:true,
        remoteSort: false,
        pagination: true,
        pageSize: 10,
        rowNumbers: false,
        success:function (data) {
            var rows=[];
            for(var i=0; i< data.length; i++){
                rows.push({
                    id:data[i].id,
                    goodsName:data[i].goodsName,
                    supplier:data[i].supplier
                });
            }
            $("#good_tables").datagrid({data:rows});
        },
        error:function () {
            $.messager.alert("提示","获取数据失败");
        }
    });
}

经过JS获取并绑定数据it

在JSP中定义tableio

<table id="good_tables" style="height: 484px;"></table>

在JS页面中初始化列名和数据table

$(document).ready(function () {
    initGoodsTable();
});

function initGoodsTable(){
    $('#good_tables').datagrid({
        nowrap: true,
        autoRowHeight: true,
        striped: true,
        fitColumns: true,
        collapsible: true,
        url: 'xxx',
        border: false,
        idField: 'id',
        selectOnCheck: true,
        singleSelect: true,
        width:'100%' ,
        resizable:true,
        remoteSort: false,
        columns: [[
            {
                field: 'id',
                title: '商品ID',
                align: 'center',
                formatter: function (value) {
                    return value;
                }
            },
            {
                field: 'goodsName',
                title: '商品名称',
                align: 'center',
                formatter: function (value) {
                    return value;
                }
            }, {
                field: 'supplier',
                title: '供应商',
                align: 'center',
                formatter: function (value,row) {
                    return value;
                }
            }
        ]],
        pagination: true,
        pageSize: 10,
        rowNumbers: false
    });
}

以上就是小编的分享,以为有用的小伙伴,记得点赞!

相关文章
相关标签/搜索