4

hi, I am developing angular js application In which i am using angular ui-grid to display bind the data coming from mvc controller.But after i bind data to ui-grid i want to add extra column to grid having buttons on each row dynamically.I dont know how to do it.Any help will be grateful..

code: Here columnDefs will be binded to http service method that will call mvc controll action and bind response.I want to add new column now.That will have buttons for each row.Is there any to bind array to field of columnDefs:

    $scope.gridOptions = {
            enableColumnsResize: true,
            enableSorting: false,
            enableFiltering: false,
            autoResize: true,
            enableFocusedCellEdit: true,
            enableCellSelection: true,
            enableCellEditOnFocus: true,
            enableColumnMenus: false,
            columnDefs: [

            ],
            data:$scope.swap,
            onRegisterApi: function (gridApi) {
                $scope.grid1Api = gridApi;
            }

        };
$http.get('/Default1/GetJsonData')
            .success(function (result) {
                $scope.swap = result;
                $scope.gridOptions.push({name:'newcol',filter:'newcolarray'})

                $scope.gridOptions.data = $scope.swap;
                console.log(result);
            })
            .error(function (data) {
                console.log(data);
            });
swapnil
  • 63
  • 2
  • 9
  • Hi, did you manage to find a solution for this? Im having the same issue. The push solution also overwrites the other columns displaying only the 'newCol' – Emad Apr 11 '18 at 08:58

1 Answers1

0

You should be able to do the following in your success() handler

$scope.gridOptions.columnDefs.push({name: 'newCol', displayName: 'My new col'});

And so on, one call like that per column, before you set $scope.gridOptions.data

S. Baggy
  • 950
  • 10
  • 21
  • I have query about this.will it overwrite the ui-grid..and display only 'newcol' instead of apppending new column to the existing ui-grid..can you tell me.. – swapnil Sep 07 '16 at 04:57
  • hi,S.Baggy i tried it ..but it overwrites the existing columns in the ui-grid only shows the new one.. – swapnil Sep 07 '16 at 07:05
  • Can you paste in the code you used to add the columns? – S. Baggy Sep 07 '16 at 10:58
  • i pasted the code i have slight change in requirement is it posssible to bind array in field of columnDefs.I have array of dates and i have bind it to field so that for each date column will be created. i have made some changes in code – swapnil Sep 07 '16 at 12:05
  • In your original code, you set the columnDefs to [] (empty array), so I am not sure how you are overwriting existing columns, because it doesn't look like there are any? Maybe some more code is missing? If you could create a Plunker, it would make things a lot easier, if you have the time. – S. Baggy Sep 07 '16 at 21:03
  • Firstly it bind columndefs to the data which is coming from success ..so that's why I kept.it coldefs [] eml – swapnil Sep 08 '16 at 03:26
  • it bind columndefs to the data which is coming from success ..so that's why I kept.it coldefs [] empty.now ui-grid shows the data from success...if I write $scope.gridoptionscoldefs.push (name:'newcol',f field:'array of dates')then only newcol is showing removing last ones.a nd i want to mention array in the field is it possible..but should aapend separate columns..for each date not in one column – swapnil Sep 08 '16 at 03:33
  • It is hard to figure this out. The push() call works for me in a similar case. Can you either create a Plunker or edit your original post to show the code that creates these other columns? – S. Baggy Sep 08 '16 at 06:09