0

I have a JSON array in my CI view like

<?php $columns = [".","Product Name","Subtracking","Sale Amount","Merchant Sale Amount","AN","Advertiser Name"," Date "]?>

I want to set these as my ng-grid header I have tried the following but did not work for me

$scope.gridOptions = {
            data: 'myData',
            columnDefs: <?php echo $columns?>
        };
4
  • How are you planning to write php code in javascript file? Commented Aug 27, 2014 at 1:46
  • its like file is being uploaded and after reading that file and converting it to array, i am separating the 1st index of that array as there are column headings in that array Commented Aug 27, 2014 at 1:48
  • I mean to say, Php doesn't work in .js files Commented Aug 27, 2014 at 1:51
  • i have included this JS in my php file, and its working if i do console.log(<?php echo $column?>) Commented Aug 27, 2014 at 1:52

2 Answers 2

0

If you have an array, use this syntax for looping over it

<div ng-repeat="col in gridOptions.columnDefs">{{col}}</div>

for more info read this https://docs.angularjs.org/api/ng/directive/ngRepeat

Edit: Is this what you want http://plnkr.co/edit/LIAqMq4TEz9xOCUGPAUs?p=preview

4
  • this just repeats the columns array, i need to set this array as col header :( Commented Aug 27, 2014 at 2:08
  • check the plunkr link i just added Commented Aug 27, 2014 at 2:16
  • yeah exactly, but columns might be changed, how i can achieve this? Commented Aug 27, 2014 at 2:22
  • You are already doing it using <?php echo $columns?>, whatever it returns will be set here and if the data changes columns change automatically in angularJs. What exactly do you want? you should be clear about that in the question itself. Commented Aug 27, 2014 at 2:29
0

you might want to do something like this:

$scope.gridOptions = {
            data: 'myData',
            columnDefs: <?php echo json_encode($columns) ?>
        };

Just to ensure that you're actually passing valid JSON to the JS file. Hope that helps.

1
  • this is already done in PHP, i have converted the array to JSON and $columns in my code is the output of that JSON array Commented Aug 27, 2014 at 1:57

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.