我对角度的理解是有限的,但我会尝试提供帮助(免责声明:)虽然,我确信有一种更简单的方法,我希望这个方法有所帮助
//For convenience, lets call your dataset partSales.
//1st get dynamic columns
var myDynamicColumns = getMyDynamicColumns(partsales);
//2nd format data to make period data have a unique name.
//Note the periodValue will be assigned to its period e.g. 'Jan-15':'267'
//This should match the columnDefs field value. e.g. field: 'Jan-15'
var myData = myDataFormatter(partsales);
//fixed columns
var gridOColDefs = [
{
field: 'partNo',
enableCellEdit: false,
headerName: 'Part No',
},
{
field: 'partName',
enableCellEdit: false,
headerName: 'Part Name',
cellClass: 'text-right',
width: 45,
headerGroup: 'Farm'
}].concat(myDynamicColumns);
];
//Define you grid options
var gridOptimizerOptions = {
pinnedColumnCount:2,
columnDefs: myDynamicColumns,
rowData: myData
};
//Returns an list of dynamic column definitions
function getMyDynamicColumns(partsales){
var columnFields = [];
//loop though parts
_.each(partSales, function(singlePartSale){
//loop through periods
_.each(singlePartSale.periodList, function(period){
var periodTitle = period.period;
//Do something to make sure the column definition has not already been added. The conditional syntax below is not valid.
if(periodTitle is not in columnFields){
columnFields.push(
{
//You will have to flush this out. You may need to loop through you data and give each period an unique name.
field: [periodTitle],
headerName: [periodTitle],
width: 50
});
} //end condition
}); //periods loop
});//end parts loop
//Return new column defs so they can be concattinated to the fixed column Definitions
return columnFields;
}
function myDataFormatter(partSales){
var newDataList = [];
_.each(partSales, function(partSale){
var newData = {
partNo = partSale.partNo,
partName = partSale.partName
}
_.each(partSale.periodList, function(singlePeriod){
var newField = singlePeriod.period;
newData.push([newField] = singlePeriod.periodValue);
});
newDataList.push(newData);
})
return newDataList;
})
// so your data should look like this from the data formatter function.
[{
'partNo':"P00001",
'partName':'AAAAA',
'Jan-15':"267",
'Feb-15':"347",
...and so on.
},
{
'partNo':"P00002",
'partName':'AAAB',
'Jan-15':"421",
'Feb-15':"2",
...and so on.
}]