ligerui中如何动态创建grid

在线测试例子:http://vazumi.net.s1.kingidc.net/example/dynamicgrid.htm

截图:

原理:

开发一个系统,比如说有几十个基本资料表,新增修改删除这些基本功能,按照ligerui创建模式,每个表要一个页面吧

虽然是简单的复制粘贴代码,也很麻烦吧,代码冗余严重

能否根据数据源,自动创建grid,可以,思路有2种

1.后台根据数据源创建js,前台页面引用,达到动态grid的效果

2.前台根据json,把表格搭建起来

本文采用方法2,提供一个思路,读json,组合字符串拼接列名,然后生成grid

 

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html   xmlns = "http://www.w3.org/1999/xhtml" >
< head >
     < title ></ title >   
     < link  href = "../lib/ligerUI/skins/Aqua/css/ligerui-all.css"  rel = "stylesheet"  type = "text/css"  />
     < script  src = "../lib/jquery/jquery-1.3.2.min.js"   type = "text/javascript" ></ script >  
     < script  src = "../lib/ligerUI/js/plugins/ligerGrid.js"  type = "text/javascript" ></ script >
 
     < script  type = "text/javascript" >
       var grid=null;
       var j;
       function creategrid(divname,url)
       {       
             $("#"+divname).remove();      
             var div = "< div  id = '"+divname+"'  style = 'margin:0; padding:0' ></ div >";
             $(document.body).append(div);
       
             $.getJSON(url,{ page: 1,pagesize:10 ,Rnd: Math.random() },
             function(json)
             {
                 var colnames="";
                 for(var i in json.Rows[0]) //在这里读json的列名,当作表格的列名
                 {
                     if (!(i=='RECORDCOUNT' || i=='PASSWORD'))
                         colnames+=",{name:'"+i+"',display:'"+i+"'}";
                 }
                 colnames=colnames.substr(1,colnames.length);
                 j=json;
                 eval(               
                         "grid=$('#"+divname+"').ligerGrid({"+
                         "checkbox: true,"+
                         "columns:["+colnames+"],"+  //然后么拼字符串                       
                         //"data:j,"+    //这么写适合不分页的grid,还少读一次数据库
                         "url:'"+url+"',"+
                         "dataAction:'server',"+
                         "pageSize:10,pageSizeOptions: [10, 15, 20, 30, 50, 100]"+
                         "});"
                     );                                   
             });                    
       }  
 
     </ script >
</ head >
< body  style = "padding:10px" >
     < input  type = "button"  onclick = "creategrid('g','/service/DataHandler.ashx?View=rulelist')"  value = "动态grid1"  />
     < input  type = "button"  onclick = "creategrid('g','/service/DataHandler.ashx?View=thread&id=999&type=system')"  value = "动态grid2"  />
     < input  type = "button"  onclick = "creategrid('g','/service/DataHandler.ashx?View=city')"  value = "动态grid3"  />
     
     < input  type = "button"  onclick = "creategrid('g2','/service/DataHandler.ashx?View=thread&id=999&type=system')"  value = "增加grid2"  />
     < input  type = "button"  onclick = "creategrid('g3','/service/DataHandler.ashx?View=city')"  value = "增加grid3"  />
     
     < input  type = "button"  onclick = "$('#g2').remove();"  value = "删除grid2"  />
     < input  type = "button"  onclick = "$('#g3').remove();"  value = "删除grid3"  />
     
     
</ body >
</ html >

 

转载于:https://www.cnblogs.com/zhaoxd/archive/2013/05/31/3110218.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值