Samim Samim - 19 days ago 7
AngularJS Question

push column between columnames inangular js ng-grid

I am trying to add a column on condition basis in a particular position ..this is the code ..it adds the column name at the last position...but i want to add the column "jkl" after the column "def" ...how i will do that??? by the following code it adds the column in end position.....

$scope.columnNames = [
{field: "abc", displayName: $scope.lable_abc, cellTemplate: '<div class="celltemplate" tooltip="{{row.entity.abc}}" tooltip-append-to-body="true" style="padding-left:10px" > {{row.entity.abc}}<div>'},

{field: "def", displayName: $scope.lable_def, cellTemplate: '<div class="celltemplate" title="{{row.entity.def}}" style="padding-left:10px" > {{row.entity.def}}<div>'},

{field: 'ghi', displayName: $scope.lable_ghi, cellTemplate: '<center><div> <img src="{{row.entity.ghi}}" width="20px" ></div></center>', width: 60},

];
if($scope.x == "2" || $scope.y == "3")
{
$scope.columnNames.push({field: "jkl", displayName: $scope.lable_jkl, cellTemplate: '<div class="celltemplate" title="{{row.entity.jkl}}" style="padding-left:10px" > {{row.entity.jkl}}<div>'});
}

Answer

You should use splice method like below :

$scope.columnNames.splice(2, 0, {field: "jkl", displayName: $scope.lable_jkl, cellTemplate: '<div class="celltemplate" title="{{row.entity.jkl}}" style="padding-left:10px" > {{row.entity.jkl}}<div>'});

'push' method always adds at the last.