EasyUI-datagrid动态列创建

<script type="text/javascript">
    $(function () {
        $.ajax({
            url: "${url}getMesResourceCapacityBottleneckTitle",//这里这个方法是用来获取数据的动态表头的
            data: {param: JSON.stringify(serializeObject($('#form')))},
            type: 'post',
            dataType: 'json',
            success: function (r) {
                if (r.flag == "success") {
                    var columns=[[]];
                    var list = r.title;
                    for(var i=0;i<list.length;i++){//根据查询结果,动态的拼接表头,具体这个地方怎么拼接需要根据自身情况来做
                        var titleDay = new Date(list[i].CALENDAR_DATE).format("yyyy-MM-dd");
                        columns[0].push({field: 'date'+i, title: titleDay, width: 80,halign: 'center',align: 'center', formatter:formatColor});
                        columns[0].push({field: 'dateContrast'+i, title: titleDay, width: 80,halign: 'center',align: 'center',hidden:true});
                    }
                    $('#dgMesResourceCapacityBottleneck').datagrid({    
                           frozenColumns : [[//这一部分属于固定的已知表头的列,因为不是所有列都是动态的
                            {field: 'WorkLineName', halign: 'center', align: 'center', title: '生产线', width: 80},
                            {field: 'Zycode', halign: 'center', align: 'center', title: '资源名称', width: 80},
                            {field: 'FixedCapacity', halign: 'center', align: 'center', title: '天-能力', width: 80},
                            {field: 'Overdue', halign: 'center', align: 'center', title: '超期工时', width: 80, formatter:formatOverdue}
                        ]],
                        columns : columns,//这里是动态拼出来的列
                        url: "${url}getMesResourceCapacityBottleneckByPage.json",//这里是加载数据的方法,后台加载的数据一定要和前台字段匹配
                        queryParams:{
                            param: JSON.stringify(serializeObject($('#form')))
                        }
                    });
                } else {
                    $.messager.alert('提示','加载出错,请联系管理员!','warning');
                }
            }
        });
    });
</script>
<div data-options="region:'center'" border="false" style="background:#ffffff;">
     <table id="dgMesResourceCapacityBottleneck"
       data-options="
         fit: true,
         border: false,
         rownumbers: false,
         animate: true,
         collapsible: false,
         autoRowHeight: false,
         idField :'id',
         singleSelect: true,
         checkOnSelect: true,
         selectOnCheck: false,
         pagination:true,
         pageSize:dataOptions.pageSize,
         pageList:dataOptions.pageList,
         striped:true">
     </table>
</div>

猜你喜欢

转载自www.cnblogs.com/1012hq/p/11316556.html