Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

angularjs - Angular UI-Grid Pivot

I have a collection of these Javascript objects: (Displayed as a DTO)

public class ParameterValueDTO : DataTransferObject
{
  public int Id { get; set; }
  public String Comments { get; set; }
  public String Description { get; set; }
}

By default, AngularJS UI-Grid will create a row for each ParameterValue object with 3 columns: Id, Comments, Description which works fine.

IMAGE: Standard objects mapping to table

What I would like to do however is create a column for each object's "Comments" value and bind it to the corresponding "Description" value. Essentially pivoting the table so it only has 1 row (forget the ID column for now).

The javascript I've tried:

var cols = [];
var row = obj.data.ProductAttributes[0].Specifications[0].ParameterValues
var length = row.length;


for (var i = 0; i < length; i++) {
    cols.push({
        field: "Description",
        displayName: row[i].Comments
    });
}

$scope.gridOptions = {
    columnDefs: cols,
    data: row
};

The above results in the following which is obviously wrong:

IMAGE: One column, new row for each Description

Is it possible to accomplish this with the current data structure or what exactly is the correct approach I should be taking?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'd modify the code to just reprocess your data. ui-grid really only likes to bind to columns and rows, it can't reverse the order.

So you'd:

var pivotData = [ {} ];
data.forEach(function(parameterDTO) {
  pivotData[0][parameterDTO.comments] = parameterDTO.description;
});

Then you should be able to use that new data object as grid data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...