本文介绍了角度UI网格枢轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下这些Javascript对象的集合:(显示为DTO)

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; }
}

默认情况下,AngularJS UI-Grid将为每个ParameterValue对象创建一行,并包含3列:Id,Comment,Description可以正常工作.

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

图像:标准对象映射到表格

但是,我想为每个对象的"Comments"值创建一列并将其绑定到相应的"Description"值.本质上是旋转表,使其只有1行(暂时忘记ID列).

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).

我尝试过的javascript:

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:

图像:一列,每行描述都有新行

是否可以使用当前的数据结构来完成此操作,或者我应该采用的正确方法到底是什么?

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

推荐答案

我将修改代码以重新处理您的数据. ui-grid真的只喜欢绑定到列和行,它不能颠倒顺序.

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.

所以您会:

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.

这篇关于角度UI网格枢轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 21:36