Search code examples
angularjsangular-ui-grid

Prevent column moving/rearranging


I am using UI Grid. I have a plnkr here

var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.pinning', 'ui.grid.moveColumns']);

app.controller('MainCtrl', ['$scope', '$http', '$log', function ($scope, $http, $log) {
  $scope.gridOptions = {};

  $scope.gridOptions.columnDefs = [
    { name:'id', width:50, pinnedLeft:true  },
    { name:'name', width:100, pinnedLeft:true },
    { name:'age', width:100, pinnedLeft:true  },
    { name:'company', width:100},
    { name:'address.street', width:150  },
    { name:'address.city', width:150 },
    { name:'address.state', width:50 },
    { name:'address.zip', width:50 },
    { name:'email', width:100 },
    { name:'phone', width:200 },
    { name:'about', width:300 },
    { name:'friends[0].name', displayName:'1st friend', width:150 },
    { name:'friends[1].name', displayName:'2nd friend', width:150 },
    { name:'friends[2].name', displayName:'3rd friend', width:150 },
  ];
  $scope.gridOptions.jqueryUIDraggable= false;

  $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/500_complex.json')
    .success(function(data) {
      $scope.gridOptions.data = data;
    });
}]);

, where I want to achieve the following:

  1. the first 3 columns to be pinned to the left
  2. I want to be able to move/rearrange the other non-pinned columns by drag-dropping them

I was able to achieve both.

However, I was not able to STOP/prevent drag-drop of the columns that are pinned. How can I prevent certain columns from being drag-dropped?


Solution

  • In the app.js, try adding enableColumnMoving:false to the columnDefs like so:

    var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.pinning', 'ui.grid.moveColumns']);
    
    app.controller('MainCtrl', ['$scope', '$http', '$log', function ($scope, $http, $log) {
      $scope.gridOptions = {};
    
    $scope.gridOptions.columnDefs = [
    { name:'id', width:50, pinnedLeft:true, enableColumnMoving:false },
    { name:'name', width:100, pinnedLeft:true, enableColumnMoving:false },
    { name:'age', width:100, pinnedLeft:true, enableColumnMoving:false  },
    { name:'company', width:100},
    { name:'address.street', width:150  },
    { name:'address.city', width:150 },
    { name:'address.state', width:50 },
    { name:'address.zip', width:50 },
    { name:'email', width:100 },
    { name:'phone', width:200 },
    { name:'about', width:300 },
    { name:'friends[0].name', displayName:'1st friend', width:150 },
    { name:'friends[1].name', displayName:'2nd friend', width:150 },
    { name:'friends[2].name', displayName:'3rd friend', width:150 },
    ];    
    
    $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/500_complex.json')
    .success(function(data) {
      $scope.gridOptions.data = data;
    });
    }]);