我需要在ui-grid中显示一个字符串数组.单列,每行一个字符串 [英] I need to display an array of strings within ui-grid. Single column, one string per row

查看:163
本文介绍了我需要在ui-grid中显示一个字符串数组.单列,每行一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用的REST API返回以下格式的数组:

The REST API I'm calling returns an array in the format:

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

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

And my ui-grid needs to display those data entries in a single column, one per row.

我有:

  $scope.items = [];

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

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

And my success callback function within my $http call just sets $scope.items to response.data.

这对于以JSON对象数组形式接收的其他方法中的数据很好用,但是在这种情况下,我只是获取字符串,在控制台中两次出现以下错误:

This works fine for data in other methods that's received as an array of JSON objects, but in this case where I just get strings, I get the following error in the console twice:

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

Wat do吗?

推荐答案

我通过创建以下实用程序函数使其起作用:

I got it to work by creating this utility function:

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

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

and then setting $scope.items to the output of this function with my column name and the array passed in.

这篇关于我需要在ui-grid中显示一个字符串数组.单列,每行一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆