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>
   /**
     * @description:数据拼装
     * @param queryReqBean
     * @return
     * @throws Exception
     */
    public Map<String,Object> doCreate(QueryReqBean<Map<String,Object>> queryReqBean) throws Exception {
        try {
            Map<String,Object> map = new HashMap<String, Object>();
            List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
            List<Map<String,Object>> listHolidayMap = this.searchSumHours(queryReqBean.getSearchParams());
            //这个统计出来的时间是资源能力工时调整表中的数据,需要加上该工作中心的当天能力工时,然后用来和实际工序工时进行比对判断
            List<Map<String,Object>> adjustHoursMap = this.searchSumHoursAndHoliday(queryReqBean.getSearchParams());
            
            QueryRespBean<Map<String,Object>> queryRespBean = this.searchMesResourceCapacityBottleneckByPage(queryReqBean);
            /*Page<Map<String,Object>> mapPage = queryRespBean.getResult();
            for (Map<String, Object> mp : mapPage) {
                queryReqBean.getSearchParams().put("mdsWorkCenterId",mp.get("MDS_WORK_CENTER_ID"));
            }*/
            List<Map<String,Object>> mdsFactoryCalendarDateMap = this.searchMdsFactoryCalendarDate(queryReqBean.getSearchParams());
            
            Page<Map<String,Object>> dataList = queryRespBean.getResult();
            Page<Map<String,Object>> pageList = new Page<Map<String,Object>>();
            pageList.setPageNum(dataList.getPageNum());
            pageList.setPageSize(dataList.getPageSize());
            pageList.setTotal(dataList.getTotal());
            
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            
            List<Map<String,Object>> listHeadMap = dataList.getResult();
            for (Map<String, Object> map2 : listHeadMap) {
                Map<String,Object> mapList = new HashMap<String, Object>();
                mapList.put("ID", map2.get("ID"));
                mapList.put("WorkLineName", map2.get("WORK_LINE_NAME"));
                mapList.put("Zycode", map2.get("ZYCODE"));
                mapList.put("FixedCapacity", map2.get("FIXED_CAPACITY"));
                mapList.put("Overdue", map2.get("OVERDUE"));
                mapList.put("Zyid", map2.get("ZYID"));
                mapList.put("BottleneckType", map2.get("BOTTLENECK_TYPE"));
                for (int i= 0;i<mdsFactoryCalendarDateMap.size();i++) {
                    for (Map<String, Object> map4 : listHolidayMap) {
                        String s = (String)map4.get("HOLIDAY");
                        String s1 = dateFormat.format(mdsFactoryCalendarDateMap.get(i).get("CALENDAR_DATE"));
                        if(s==null||s1==null){
                            continue;
                        }
                        if(s.equals(s1)){
                            if(map2.get("ZYID").equals(map4.get("ID"))){
                                mapList.put("date"+i, map4.get("SUMHOURS"));
                            }
                        }
                    }
                    if(!mapList.containsKey("date"+i)){
                        mapList.put("date"+i, 0);
                    }
                    for (Map<String, Object> map5 : adjustHoursMap) {
                        String s = (String)map5.get("HOLIDAY");
                        String s1 = dateFormat.format(mdsFactoryCalendarDateMap.get(i).get("CALENDAR_DATE"));
                        if(s==null||s1==null){
                            continue;
                        }
                        if(s.equals(s1)){
                            if(map2.get("ZYID").equals(map5.get("ID"))){
                                mapList.put("dateContrast"+i, Integer.parseInt(map2.get("FIXED_CAPACITY").toString())+Integer.parseInt(map5.get("SUMADJUSTHOURS").toString()));
                            }
                        }
                    }
                    if(!mapList.containsKey("dateContrast"+i)){
                        mapList.put("dateContrast"+i, map2.get("FIXED_CAPACITY"));
                    }
                }
                listMap.add(mapList);
                pageList.add(mapList);
            }
            queryRespBean.setResult(pageList);
            map.put("title", mdsFactoryCalendarDateMap);
            map.put("data", queryRespBean);
            return map;
        } catch (Exception e) {
            LOGGER.error("doCreate出错:", e);
            e.printStackTrace();
            throw new DaoException(e.getMessage(), e);
        }
    }

 

posted @ 2019-08-07 17:20  暖然  阅读(1271)  评论(0编辑  收藏  举报