datatables中columns.render的使用

时间:2021-02-28 03:46:54

可直接在columns申明中对应列下方使用render改变该列样式

也可单独在columnsDefs中用targets指定目标。

"render":function(data,type,row,meta){}

完整的为4个参数,其中参数数量是可变的。

data指该行获取到的该列数据,

row指该行,可用row.name或row[2]获取第3列字段名为name的值,

type调用数据类型,可用类型“filter”,"display","type","sort",具体用法还未研究,

meta包含请求行索引,列索引,tables各参数等信息。

实例1:(在genre列生成下拉列表形式并绑定控制器传来的数据)

"columns": [
                        {"data": "actual_id"},
                        {"data": "sort_index"},                       
                        {"data": "category"},
                        {"data": "genre",
                         "render":function(data,type,row,full){
                                     if(data.length!=0){
                                        var str;
                                        for(var i=0;i<data.length;i++){                                      
                                            str+="<option value="+data[i].id+">"+data[i].name+"</option>";
                                        }                              
                                        return "<select id="+row.sort_index+">"+str+"</select>";
                                    }else{
                                        return "<select id="+row.sort_index+"></select>";
                                    }
                                  }
                        },
                        {"data": "created_at"},
                        {"data": "operation","width":"18%",},
                        {"data":null}                        
            ],

实例2:静态绑定

columnDefs:{

"targets":3,

"data":null,

"render":function(){}

}

相关文章