2

I have a some simple JSON array of strings in which I want to display on my ng-grid. For example, if I have an array that is looks like so:

arrayOfStrings: ["test", "blah", "foo"];

I want to list all elements within this array in a column in my ng-grid, but I am having trouble doing so. Here is how I tried to render the elements within the array:

{field: 'arrayOfStrings', displayName: 'Array', width:'20%',
    cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()"><span ng-cell-text>{{row.getProperty(col.field)}} </span></div>'},

However, nothing shows up. If anyone knows how to display the elements within an array into a column, that would be appreciated. Thanks!

2 Answers 2

3

I think you're asking how to display all of the items in your array within the same column. Let me know if that's not the case and I'll edit my answer. If your field contains an array of strings, ng-grid will by default display it as a string representation of the whole array:

["test", "blah", "foo"]

You can add a cellFilter on the column

{field: 'arr', displayName: 'arrayOfStrings', cellFilter: 'stringArrayFilter'}

to control how the array data is represented:

filter('stringArrayFilter', function() {
  return function(myArray) {
    return myArray.join(', ');
  };
})

See an example Plunker here.

Sign up to request clarification or add additional context in comments.

6 Comments

Ah, so I got it kind of working, but I get an error that says the following: Can't interpolate: {{$eval('row.entity.' + col.field) |stringArrayFilter}} TypeError: Cannot call method 'join' of undefined any ideas why?
Can you create a Plunker that demonstrates the behavior or post your full code? What versions of things are you using?
Yes, here is a Plunker that demonstrates the behavior of my code. As you can see here, it displays undefined, and I'm not sure why. plnkr.co/edit/Ba7hoGFhI7cWWaD3itZx?p=preview
ng-grid is looking for a field within each object in the array named "test", but you don't have one. $scope.gridOptions.data should point to an array of objects with named fields, but you are giving it an array of strings. See correction here: plnkr.co/edit/TNHk4PIAy8vnDguxBLUS?p=preview
Hi Geoff, is there a way to programatically change the commented-out $scope.test to looking like the $scope.test that works for this plunkr you just posted? Thank you for answering the question and I apologize for the late upvote.
|
-1

Make sure your html page has the attribute set

<div class="gridStyle" ng-grid="gridOptions"></div>

Then in your controller:

$scope.arrayOfStrings = ["test", "blah", "foo"];
$scope.gridOptions = {
    data:'arrayOfStrings'   
};

Get this part working, then you can begin adding in other options

2 Comments

Hm, what do you mean setting the properties of the columnDefs? Do you mean the cell Templates? I tried it with your approach and it didn't seem to work for me
Sorry, I missed part of your initial question. I have fixed and simplified my answer. See if you can get that working first, then worry about customizing it further. If this doesn't work, then you may not be loading your ng-grid properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.