1. 程式人生 > >easyui datagrid動態列

easyui datagrid動態列

在網上找了許多方法,也參考了許多大佬

感謝他們的成功,綜合來看如下

jsp

沒啥好說的,就一個table

<table id="dg"></table>

js

主要就是columns是從後臺傳過來的,因為是後臺傳的,
所以幾列隨你組合

/**
 * 初始化列數
 */
function initColumn(indId) {
    $.ajax({
        type : "post",
        data : {
            "indId" : indId
        },
        dataType : "json"
, url : basePath+ "/industryDataServlet.do?action=getAllColumn", dataType : "json", async : false,// 同步請求 success : function(data) { initDataGrid(data, indId); } }); } /** * 初始化資料 */ function initDataGrid(columns, indId) { $("#dg").datagrid({ title : '<div id="mytitle" style="text-align:center;font-size:18px">資料</div>'
, url : basePath+ "/industryDataServlet.do?action=getAllInfo",// 載入的URL queryParams : {indId : indId}, // 分頁 pagination : true, singleSelect : true, rownumbers : true, fit : true,// 自動補全 fitColumns : true, // title : "使用者管理", columns : [ columns ], toolbar : '#tb'
}); }

servlet—>getAllColumn

PrintWriter out = response.getWriter();
        String industryId = request.getParameter("indId");
        String retValue = "";
        List<FieldBean> beans =industryDataService.getAllColumn(industryId);

        retValue = JSONArray.fromObject(beans).toString();
        out.write(retValue);
        out.flush();
        out.close();

service—>getAllColumn

public List<FieldBean> getAllColumn(String industryId) {
        String index = new IndustryindexService().getIndustryIndex(industryId);
        int beansLen = 0;
        if (index == null) {
            beansLen = 10;
        } else {
            beansLen = 11;
        }//動態判斷幾列
        List<FieldBean> beans = new ArrayList<FieldBean>();
        for (int i = 1; i < beansLen; i++) {
            FieldBean bean = new FieldBean();
            bean.setTitle("第"+i+"列");
            bean.setField("column" + i);
            bean.setAlign("center");
            bean.setWidth(80);
            beans.add(bean);
        }

        return beans;
    }

servlet—>getAllInfo

正常的獲取資料,沒啥好說的

...
...

FieldBean

package com.jcfx.bean.industryData;

public class FieldBean {//datagrid列數

    private String title;
    private String field;
    private int width;
    private String align;
    private boolean sortable;
    private boolean resizable;
    private boolean hidden;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getField() {
        return field;
    }
    public void setField(String field) {
        this.field = field;
    }
    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public String getAlign() {
        return align;
    }
    public void setAlign(String align) {
        this.align = align;
    }
    public boolean isSortable() {
        return sortable;
    }
    public void setSortable(boolean sortable) {
        this.sortable = sortable;
    }
    public boolean isResizable() {
        return resizable;
    }
    public void setResizable(boolean resizable) {
        this.resizable = resizable;
    }
    public boolean isHidden() {
        return hidden;
    }
    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }

}

效果圖

這裡寫圖片描述

這裡寫圖片描述

這兩個因為傳向後臺的indId形成的列也不同