2 xy focus xy_focus 于 2015.06.04 11:49 提问

Easyui dtagrid 绑定数据问题

json串如下:
{"total":6,"rows":[{"classes":{"id":11,"name":"123456","students":[{"classes":null,"id":28,"money":100
,"stuAge":"15","stuName":"张三","stuSex":"女"},{"classes":null,"id":25,"money":100,"stuAge":"13","stuName"
:"田七","stuSex":"男"},{"classes":null,"id":24,"money":100,"stuAge":"12","stuName":"赵六","stuSex":"女"},{"classes"
:null,"id":26,"money":100,"stuAge":"14","stuName":"王五","stuSex":"女"},null]},"id":23,"money":300,"stuAge"
:"13","stuName":"李四","stuSex":"男"},{"classes":{"id":11,"name":"123456","students":[{"classes":null,"id"
:28,"money":100,"stuAge":"15","stuName":"张三","stuSex":"女"},{"classes":null,"id":25,"money":100,"stuAge"
:"13","stuName":"田七","stuSex":"男"},null,{"classes":null,"id":26,"money":100,"stuAge":"14","stuName":"
王五","stuSex":"女"},{"classes":null,"id":23,"money":300,"stuAge":"13","stuName":"李四","stuSex":"男"}]},"id"
:24,"money":100,"stuAge":"12","stuName":"赵六","stuSex":"女"},{"classes":{"id":11,"name":"123456","students"
:[{"classes":null,"id":28,"money":100,"stuAge":"15","stuName":"张三","stuSex":"女"},null,{"classes":null
,"id":24,"money":100,"stuAge":"12","stuName":"赵六","stuSex":"女"},{"classes":null,"id":26,"money":100,"stuAge"
:"14","stuName":"王五","stuSex":"女"},{"classes":null,"id":23,"money":300,"stuAge":"13","stuName":"李四","stuSex"
:"男"}]},"id":25,"money":100,"stuAge":"13","stuName":"田七","stuSex":"男"},{"classes":{"id":11,"name":"123456"
,"students":[{"classes":null,"id":28,"money":100,"stuAge":"15","stuName":"张三","stuSex":"女"},{"classes"
:null,"id":25,"money":100,"stuAge":"13","stuName":"田七","stuSex":"男"},{"classes":null,"id":24,"money"
:100,"stuAge":"12","stuName":"赵六","stuSex":"女"},null,{"classes":null,"id":23,"money":300,"stuAge":"13"
,"stuName":"李四","stuSex":"男"}]},"id":26,"money":100,"stuAge":"14","stuName":"王五","stuSex":"女"},{"classes"
:{"id":10,"name":"131311","students":[null]},"id":27,"money":100,"stuAge":"14","stuName":"旺旺","stuSex"
:"男"},{"classes":{"id":11,"name":"123456","students":[null,{"classes":null,"id":25,"money":100,"stuAge"
:"13","stuName":"田七","stuSex":"男"},{"classes":null,"id":24,"money":100,"stuAge":"12","stuName":"赵六","stuSex"
:"女"},{"classes":null,"id":26,"money":100,"stuAge":"14","stuName":"王五","stuSex":"女"},{"classes":null
,"id":23,"money":300,"stuAge":"13","stuName":"李四","stuSex":"男"}]},"id":28,"money":100,"stuAge":"15","stuName"
:"张三","stuSex":"女"}]}
绑定的样式如下:

 $(function(){
            $('#dg').datagrid({    
                url:'show_showStu.action',  
                pagination:true,
                fitColumns:true,
                idField:'id',
                pageSize:10, 
                pageList:[10,20,30] ,
                columns:[[    
                    {field:'id',title:'编号',width:100},    
                    {field:'stuName',title:'姓名',width:100},    
                    {field:'stuSex',title:'性别',width:100,align:'right'}  ,  
                    {field:'stuAge',title:'年龄',width:100,align:'right'} ,   
                    {field:'money',title:'余额',width:100,align:'right'} ,   
                    [{field:'?这里怎么取',title:'班级',width:100,align:'right'} ]   
                ]]    
            });  
        });
这个班级是一个数组怎么取值,求大神告知一二。

5个回答

showbo
showbo   Ds   Rxr 2015.06.04 15:27

直接配置为你classs,还需要配置formatter进行数组内容获取,最后的classes不要加[]这个,去掉

.........
{field:'money',title:'余额',width:100,align:'right'} , 
{field:'classes',title:'班级',width:100,align:'right',formatter:function(){
 //v就是你的classes json对象
 var r='',s=v.students;
 r=v.name;
 r+='学生人数:'+(s?'没有学生':s.length);
 //其他的处理,如遍历students数组获取你要的人什么的东东
 return r;
 }} 
showbo
showbo formatter配置少了参数了。。改这样:formatter:function(v)//////
2 年多之前 回复
xy_focus
xy_focus   2015.06.16 17:52

唉!我前几天没办法,把JSON串在后台改好了传过来

pop32399
pop32399   2015.06.04 12:55

这个问题问得好,不太好回答

My_cxd
My_cxd   2016.04.06 13:19

{field:'cname',title:'班级',width:100,align:'center',sortable:true,

editor:{type:'combobox',
options:{
valueField:'cname',

textField:'cname',
panelHeight:true,
url:'ShowClassServlet',
required:true
}}},]],
这里的cname,就是你查出的班级在后台转换成Json串传过来,就可以了

editor这个是将渲染成下拉框
班级就出来了

xy_focus
xy_focus   2016.04.07 11:02

我的班级是个对象图片说明

Csdn user default icon
上传中...
上传图片
插入图片