在网上找了许多方法,也参考了许多大佬
感谢他们的成功,综合来看如下
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形成的列也不同