动态创建easyui datagrid代码

		$(function(){
			var title = ['票款','建设费','张数'];
			var title1 = ['zje','jjf','sl'];
			var columns = new Array();
			var columns1 = new Array();
			var dataa;
			$.ajax({
				url:'<%=path%>/rxshud/queryRxszhd.mvc',
				type:'post',
				async:false, //将异步转化成同步
				dataType:"json",
				data:{},
				success:function(data){
					dataa = data;
					var index = 0;					
					$.each(data[0],function(i,field){
						if(i != 'date'){
							var column = {};
							column['title'] = title[index%3];
							column['field'] ="" +Math.floor(index/3) + title1[index%3];
							column['width'] = 100;
							columns.push(column);
							index++;
						}
						
					});
					
					var hkgsmc = data[data.length -1];	
					var hkgsmc1 = [];
					for(var i = 0,len = index/3; i < len; ++i){
						hkgsmc1.push(eval('hkgsmc.hkgsmc'+i));
					}
					var column3 = {};
					column3['title'] = '日期';
					column3['field'] ="date";
					column3['width'] = 100;
					column3['rowspan'] = 2;
					columns1.push(column3);
					$.each(hkgsmc1,function(i,value){
						var column1 = {};
						column1['title'] = value;
						column1['width'] = 300;
						column1['colspan'] = 3;
						columns1.push(column1);
					});
				}
			});
			
			var grid = $('#dg').datagrid({
				url:'<%=path%>/rxshud/queryRxszhd.mvc',
				loadmsg:'',
				singleSelect:true,
				height:450,
				rownumbers:true,
				toolbar:[{
					text:'导出',
					iconCls: 'export',
					handler: function(){
						location.href = '<%=path%>/rxshud/exportRxshzb.mvc';
					}
				}],
				columns:[columns1,columns]
			}).datagrid('loadData',dataa);
		});

  其中:

       Math.floor()函数  :是取整数 除去小数部分

       eval('hkgs.hkgsmc');: eval函数是去js数组中指定字段的value值,即数组 hkgs 中 hkgsmc 的值

你可能感兴趣的:(javascript)