我正在尝试通过遍历javascript数组对象向slickgrid添加动态列。
js数组中对象的格式为:
auditor = {"firstName":"test audit manager","lastName":".","middleName":null,"partyId":"11140"}
var columns = [];
/*from the ajax result, I'm obtaining 'auditors' js array */
for (var key in auditors) {
var auditor = auditors[key];
var name = auditor["firstName"];
if(auditor["middleName"]){
name += auditor["middleName"];
}
if(auditor["lastName"]){
name += auditor["lastName"];
}
if(auditors[parseInt(key)+parseInt(1)]){
columns = columns += '{id: '+'"auditor_'+key+'", name: '+'"'+name+'"'+', field: '+'"auditor_'+key+'", editor: Slick.Editors.Text, width:80, minWidth:80, sortable:true, focusable:false},';
}
else{
columns = columns += '{id: '+'"auditor_'+key+'", name: '+'"'+name+'"'+', field: '+'"auditor_'+key+'", editor: Slick.Editors.Text, width:80, minWidth:80, sortable:true, focusable:false}';
}
}//end for loop
columns.replace(/\\\//g, "/");
var options = {
enableCellNavigation: true,
enableColumnReorder: false,
editable: true,
forceFitColumns: false,
enableCellNavigation: true,
//enableAddRow: true,
asyncEditorLoading: false,
autoEdit: true,
secondaryHeaderRowHeight: 25
};
$(function () {
grid = new Slick.Grid("#myGrid", data, columns, options);
grid.setSelectionModel(new Slick.CellSelectionModel());
var columnpicker = new Slick.Controls.ColumnPicker(columns, grid, options);
grid.onAddNewRow.subscribe(function (e, args) {
var item = args.item;
grid.invalidateRow(data.length);
data.push(item);
grid.updateRowCount();
grid.render();
});
});
答案 0 :(得分:1)
您正确地将columns
声明为顶部的数组,但是稍后您要向其添加字符串值。使用.push
将元素添加到数组中,并且值不能为字符串。
类似的东西:
columns.push(
{
id: "auditor_" + key,
name: name,
field: "auditor_"+ key,
editor: Slick.Editors.Text,
width:80,
minWidth:80,
sortable:true,
focusable:false
}
);
那么显然您的.replace()
必须在数组的元素上完成,或者在将值分配给数组之前完成。