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

I need to display an array of strings within ui-grid. Single column, one string per row

REST API 我正在调用 returns 格式的数组:

["a", "b", "c", "d"]

我的 ui-grid 需要在单列中显示这些数据条目,每行一个。

我有:

  $scope.items = [];

  $scope.gridOptions = {
      data: 'items' 
  };

我的 $http 调用中的成功回调函数只是将 $scope.items 设置为 response.data

这对于以 JSON 对象数组接收的其他方法中的数据工作正常,但在这种情况下,我只是获取字符串,我在控制台中两次收到以下错误:

Error: colDef.name or colDef.field property is required

在做什么??

我通过创建这个实用函数让它工作:

function convertArrayOfStringsToGridFriendlyJSON(colName, arr) {
  var out = [];
  arr.forEach(function(entry){
    var obj = {};
    obj[colName] = entry;
    out.push(obj);
  });
  return out;
};

然后使用我的列名和传入的数组将 $scope.items 设置为此函数的输出。

关注这个->binding to 1D array

UI-Grid can also bind be to a one-dimensional array of primitives - in this case using uiGridConstants.ENTITY_BINDING will use the entire entry in the data array as the value for the cell instead of a field therein. This is useful if the data is an array of strings, or also if a cell filter needs access to multiple fields within each row object.

示例:Check this plnkr.

app.controller('OneDimensionCtrl', ['$scope', 'uiGridConstants', function ($scope, uiGridConstants) {

$scope.gridOptions = {
        enableSorting: true,
        columnDefs: [
          { name:'Name', field: uiGridConstants.ENTITY_BINDING }
        ],
        data : [
          "John Rogers",
          "David Michaels",
          "Andrew Johnson",
          "Donald McDonald"
        ]
      };
}]);