Вопрос

I have an array that I am trying to display using ng-grid like so:

http://plnkr.co/edit/G33IlPCNAdh1jmNTtVNO?p=preview

I understand that ng-grid cannot have the field portion as an array--it requires a JSON object. I also understand that, in reference to my plunkr above, if you uncomment out $scope.test2 and replace all instances of $scope.test with $scope.test2, the information would display all the data inside $scope.test2 as desired, as opposed to the undefined messages I am getting in the current state of the plunkr.

How would I change my $scope.test to look something like $scope.test2 programatically? I just want to change the format of the array to look like $scope.test2 just for this ng-grid. Is it possible? If anyone could help that would be great.

Edit:

Basically I want to change

$scope.test = ['blah', 'blah2'];

to looking like

$scope.test2 = [{name:['blah', 'blah2']}]; //name can be changed to anything
//name just has to specify the name of the array inside this object.

Is this possible?

Это было полезно?

Решение

The core problem is that you need an array of objects that represent one row each, meaning you want test to look something like this:

$scope.test = [
  { test: 'blah' },
  { test: 'blah2' }
];

To do that programmatically, you could do something like this:

$scope.test = [ 'blah', 'blah2' ];
$scope.testObjects = [];
$scope.test.forEach( function( val ){
  $scope.testObjects.push( { test: val } );
});
$scope.gridOptions = { 
  data: 'testObjects',
  columnDefs: [
    {field: 'test', displayName: 'Test'},
  ]
};

which gets rid of your need for a filter. See my revised plunker.

Другие советы

Just do:

$scope.test2 = [{name: $scope.test}];

$scope.test is an object reference, so anything changed in $scope.test will also be reflected in $scope.test2[0].name

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top