0

我使用 Angular ui-grid 来表示响应中的 json 对象数组。但是,我当前的用例在响应中返回了一个字符串值数组。在这种情况下,应该如何配置网格?

$http.get(url)
.success(function(data) {
  if(data.length <= 0 && data != undefined) {
    $scope.noDataGridEpr="Data is not Available";
    console.log("Data is not Available");
  }
  console.log("Data is" + data);
  $scope.prList = data; 
  console.log("prList is" + $scope.prList); //an array of strings
  $scope.dataLoadedEpr=true;
  return true;
})
.error(function(data, status) {
  console.log("Error from controller. Could not query.");
  return false;
})

这打印

prList is[A/DN,B/LCK,10,10,same,match,match],
[B/DN,D/LCK,10,10,same,mismatch,match],
[G/DN,D/LCK,10,10,same,mismatch,mismatch]

这是我当前的网格配置的样子

$scope.prGrid = { 
data: 'prList',
columnDefs: [
{field: 'DPin', displayName: 'DPin', width: "5%"},
{field: 'CPin', displayName: 'CPin', width: "5%"},
{field: 'slack1', displayName: 'slack1', width: "5%"},
{field: 'slack2', displayName: 'slack2', width: "5%"},
{field: 'cComp', displayName: 'cComp', width: "10%"},
{field: 'dComp', displayName: 'dComp', width: "5%"},
{field: 'gComp', displayName: 'dComp', width: "5%"}
 enableFiltering: true,
  multiSelect: false,
  enableGridMenu: true,
  enableRowHeaderSelection: false, 
  enableSorting: true,
  enableColumnResizing: true
};

我了解上面的字段配置不正确。鉴于我有一个字符串数组作为 i/p,有人可以指导我如何在网格中设置字符串数组,每个字符串代表一行吗?

谢谢。

4

2 回答 2

1

按照这个->绑定到二维数组

示例:检查此 plnkr。

app.controller('TwoDimensionCtrl', ['$scope','$timeout','uiGridConstants', function ($scope,$timeout,uiGridConstants) {
  $scope.data=[
              ["Cox", "Carney", "Enormo", true],
              ["Lorraine", "Carney", "Comveyer", false],
              ["Nancy", "Waters", "Fuelton", false]
            ];

  $scope.gridOptions = {
          enableSorting: true,
          columnDefs: [
            { name: "firstName", field: "0" },
            { name: "lastName", field: "1" },
            { name: "company", field: "2" },
            { name: "employed", field: "3" }
          ],
          data : $scope.data
        };
  $timeout(function () {
    $scope.data[0][0]="TestMod";
  },5000);

}]);
于 2017-06-29T09:02:28.780 回答
0

按照这个 - >我需要在ui-grid中显示一个字符串数组。单列,每行一个字符串

尝试这个来创建/设置 ui-grid 使用的 json 对象。这行得通。

$scope.prList = convertArrayOfStringsToGridFriendlyJSON(data);

function convertArrayOfStringsToGridFriendlyJSON(arr) {
var out = [];
arr.forEach(function(entry){
entry = entry.replace('[' , '')
entry = entry.replace(']' , '')
entry = entry.split(',')

var obj = {}; //json object        
var DPin= "DPin"; //keys
var CPin= "CPin"; 
obj[DPin] = entry[0];
obj[CPin] = entry[1];
out.push(obj);
});
return out;
};
于 2016-08-21T08:47:38.163 回答