Search code examples
javascriptarraysangularjsstringng-grid

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


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

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

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

I have:

  $scope.items = [];

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

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

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??


Solution

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

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