参考链接
/qq_36025814/article/details/103938509
html
let cols_arr = assembleTableCol(); //重新定义标题那行//重新拼接数组标题行function assembleTableCol(){let thisCols = []; //定义标题空间var responseData;$.ajax({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},url: "query/signup/cols_arr",method: 'get',dataType: 'json',async:false, // 重要,外赋值到responseDatasuccess: function(res) {let defaultField = [];defaultField = "{field:'id', title: '', width:20, sort: true},,{field:'day', title: '某天', width:120}";for (var i = 0; i < res.length; i++) {var l = res[i].split('_');// defaultField += ",,{field:'"+res[i]+"', title: '某天', width:120}";defaultField += ",,{field:'"+res[i]+"', title: '"+l[1]+"级', width:120}";}defaultField += ",,{fixed: 'right',title: '操作',width: 150,align: 'center',toolbar: '#barDemo'}";defaultField = defaultField.split(',,');for (var i = 0; i < defaultField.length; i++) {defaultField[i]=eval("("+defaultField[i]+")"); }// 下面是正确格式// let defaultField2 =[{field:'id', title: '', width:20, sort: true}// ,{field:'day', title: '某天1', width:120}// ,{field:'level_1_give', title: '某天2', width:120}// ,{field:'level_2_give', title: '某天3', width:120}// ,{fixed: 'right',title: '操作',width: 150,align: 'center',toolbar: '#barDemo'}];// console.log(defaultField);responseData = defaultField;}});let defaultLength = responseData.length;for(let j=0;j<defaultLength;j++){ //追加默认标题thisCols.push( responseData[j]);}return [thisCols]; //注意结果为:array[0]=thisCols}//方法级渲染table.render({elem: '#LAY_table_user' // 表id,url: 'query/signup/list' , cols: cols_arr, parseData: function(res) { //res 即为原始返回的数据},id: 'testReload',page: true,height: 610});
控制器中
// array(2) {// [0]=>// string(12) "level_1_give"// [1]=>// string(12) "level_2_give"// }我返回的是如上数据,要遍历,动态显示的值