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

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

本文关键字:一个 字符串 单列 数组 ui-grid 显示      更新时间:2023-09-26

我调用的 REST API 返回一个格式为:

["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设置为此函数的输出,其中包含我的列名和传入的数组。

遵循这个>绑定到一维数组

UI-Grid 还可以绑定到基元的一维数组 - 在这种情况下,使用 uiGridConstants.ENTITY_BINDING 将使用数据数组中的整个条目作为单元格的值,而不是其中的字段。如果数据是字符串数组,或者单元格筛选器需要访问每个行对象中的多个字段,这将非常有用。

示例:检查此 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"
        ]
      };
}]);