加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

生成符合flexigrid的json格式的java代码

发布时间:2020-12-15 01:05:11 所属栏目:百科 来源:网络整理
导读:public class FlexGridJSONData { ??? private int page=1; ??? private int total=0; ??? private ListRowData rows=new ArrayListRowData(); ??? ??? private String rowid=null; ??? private ListString coldatas=null; ??? ??? /** ???? * @return ????

public class FlexGridJSONData {
??? private int page=1;
??? private int total=0;
??? private List<RowData> rows=new ArrayList<RowData>();
???
??? private String rowid=null;
??? private List<String> coldatas=null;
???
??? /**
???? * @return
???? */
??? public int getPage() {
??????? return page;
??? }
??? /**
???? * 设置页码。
???? * @param page
???? */
??? public void setPage(int page) {
??????? this.page = page;
??? }
???
???
??? public int getTotal() {
??????? return this.total;
??? }
???
??? /**
???? * 设置总记录数
???? * @param total
???? */
??? public void setTotal(int total) {
??????? this.total = total;
??? }
???
???
??? public List<RowData> getRows() {
??????? return rows;
??? }



??? public void setRows(List<RowData> rows) {
??????? this.rows = rows;
??? }
???
???
??? public void addRow(String rowid,List<String> coldatas){
??????? RowData rd=new RowData(rowid,coldatas);
??????? this.rows.add(rd);
??? }
???
??? /**
???? * 设置每一行的id。
???? * 配合addColdata(),commitData()方法是用。
???? * 例:setRowId("row1");
???? *???? addColdata("1");
???? *???? addColdata("2");
???? *????
???? *???? setRowId("row2");
???? *???? addColdata("a");
???? *???? addColdata("b");
???? *????
???? *???? commitData();
???? *?? 表示 1,2两个数据都为行row1中第一列,第二列的数据。
???? *? a,b 两个数据都为row2中第一列,第二列的数据。
???? *? commitData()的调用表示,row2行的数据已经组织完成。
???? *?
???? *? 在设置row2行的数据时,会自动提交row1行的数据。
???? *?
???? * @param rowid
???? */
??? public void setRowId(String rowid){
??????? commitData();
??????? this.rowid=rowid;
??????? this.coldatas=null;
??? }
???
??? /**
???? * 该方法配合setRowId和commitData()使用。该方法必须在调用setRowId()后调用,否则会抛出NullPointerException
???? * 请参考setRowId的说明
???? * @param coldata 每一列数据
???? */
??? public void addColdata(String coldata){
??????? if(rowid==null) throw new NullPointerException("please set rowid");
??????? if(coldatas==null) coldatas=new ArrayList<String>();
???????
??????? coldatas.add(coldata);
??? }
???
??? public void commitData(){
??????? if(this.rowid!=null && this.coldatas!=null){
??????????? addRow(this.rowid,this.coldatas);
??????????? this.rowid=null;
??????????? this.coldatas=null;
??????? }
??? }
???
???
??? /*
???? * 这里生成的是符合flexgrid for jquery 的json格式字符串
???? * 其格式如下:
???? * {
??????? page:1,
??????? total:0,
??????? rows:[
????????? {id:'row2',cell:['col','col','col']},
????????? {id:'row3',
????????? {id:'row1','col']}
?????????? ]
??????? }
???? */
??? public String toString(){
??????? StringBuffer sb=new StringBuffer();
??????? sb.append("{rn");
??????? sb.append("page:").append(page).append(",rn");
??????? sb.append("total:").append(total).append(",rn");
??????? sb.append("rows:[rn");
???????
??????? int keynum=1;
??????? List<RowData> rowdatalist=this.rows;
??????? for(RowData rowdata:rowdatalist){
??????????? sb.append("? {id:'").append(rowdata.getRowid()).append("',").append("cell:[");
??????????? int i=1;
??????????? List<String> coldatalist=rowdata.getColdata();
??????????? for(String data:coldatalist){
??????????????? sb.append("'").append(data).append("'");
??????????????? if(i<coldatalist.size()){
??????????????????? sb.append(",");
??????????????? }
??????????????? i++;
??????????? }
???????????
??????????? if(keynum<rowdatalist.size()){
??????????????? sb.append("]},rn");
??????????? }else{
??????????????? sb.append("]}rn");
??????????? }
???????????
??????????? keynum++;
??????? }
??????? sb.append("? ]rn}");
??????? return sb.toString();
??? }
???
??? public class RowData{
??????? String rowid=null;
??????? List<String> coldata=null;
???????
??????? public RowData(){
???????????
??????? }
???????
??????? public RowData(String rowid,List<String> coldata){
??????????? this.rowid=rowid;
??????????? this.coldata=coldata;
??????? }
???????
??????? public List<String> getColdata() {
??????????? return coldata;
??????? }
??????? public String getRowid() {
??????????? return rowid;
??????? }
??????? public void setColdata(List<String> coldata) {
??????????? this.coldata = coldata;
??????? }
??????? public void setRowid(String rowid) {
??????????? this.rowid = rowid;
??????? }
???????
???????
???????
??? }
???
???
??? public static void main(String args[]){
???????
??????? ArrayList<String> list=new ArrayList<String>();
??????? list.add("col");
??????? list.add("col");
??????? list.add("col");
??????? list.add("col");
??????? list.add("col");
??????? list.add("col");
???????
??????? FlexGridJSONData fgjd=new FlexGridJSONData();
??????? fgjd.setRowId("row1");
??????? fgjd.addColdata("cols");
??????? fgjd.addColdata("cols");
??????? fgjd.addColdata("cols");
???????
??????? fgjd.setRowId("row2");
??????? fgjd.addColdata("cols");
??????? fgjd.addColdata("cols");
??????? fgjd.addColdata("cols");
???????
??????? fgjd.setRowId("row3");
??????? fgjd.addColdata("cols");
??????? fgjd.addColdata("cols");
??????? fgjd.addColdata("cols");
???????
??????? fgjd.commitData();
??????? System.out.println(fgjd.toString());
???????
??? }
}

本文为转载 http://dmqianlicao.blog.163.com/blog/static/10435692008111193035765/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读