How can I access UI-Grid isolate scope from outside?

左心房为你撑大大i 提交于 2019-12-13 05:34:30

问题


There is a button outside of angular UI grid. I would like to call a function on "Default" button click and post grid object parameter to the callback function.

There is a second example as well. But instead of posting grid object to the buttons inside of the grid I would like to post the grid object to the button outside of the grid.

To cut a long story short I would like to have an edit button outside of the grid (select one row mode is turned on) instead of adding a column of edit buttons without select row option.

Is it possible starting from v3.1.0? http://plnkr.co/edit/JyiN7MejqkiTuvczsOk1

For some reason gridOptions.appScopeProvider is null when I expand $scope object in MainCtrl.Here is js-code sample:

angular.module('modal.editing', ['ui.grid', 'ui.grid.selection', 'ui.grid.edit', 'ui.bootstrap', 'schemaForm'])

.constant('PersonSchema', {
  type: 'object',
  properties: {
    name: { type: 'string', title: 'Name' },
    company: { type: 'string', title: 'Company' },
    phone: { type: 'string', title: 'Phone' },
    'address.city': { type: 'string', title: 'City' }
  }
})

.controller('MainCtrl', MainCtrl)
.controller('RowEditCtrl', RowEditCtrl)
.service('RowEditor', RowEditor)
;

MainCtrl.$inject = ['$http', 'RowEditor', '$modal'];
function MainCtrl ($http, RowEditor) {
  var vm = this;

  vm.editRow = RowEditor.editRow;

  vm.gridOptions = {
    enableRowSelection: true,
    enableRowHeaderSelection: false,
    multiSelect: false,
    selectionRowHeaderWidth: 35,
    columnDefs: [
      { field: 'id', name: '', cellTemplate: 'edit-button.html', width: 34 },
      { name: 'name' },
      { name: 'company' },
      { name: 'phone' },
      { name: 'City', field: 'address.city' },
    ]
  };

  vm.test = function() {
    debugger;
  };

  $http.get('http://ui-grid.info/data/500_complex.json')
    .success(function (data) {
      vm.gridOptions.data = data;
    });
}

RowEditor.$inject = ['$rootScope', '$modal'];
function RowEditor($rootScope, $modal) {
  var service = {};
  service.editRow = editRow;

  function editRow(grid, row) {
    debugger;
    $modal.open({
      templateUrl: 'edit-modal.html',
      controller: ['$modalInstance', 'PersonSchema', 'grid', 'row', RowEditCtrl],
      controllerAs: 'vm',
      resolve: {
        grid: function () { return grid; },
        row: function () { return row; }
      }
    });
  }

  return service;
}

function RowEditCtrl($modalInstance, PersonSchema, grid, row) {
  var vm = this;

  vm.schema = PersonSchema;
  vm.entity = angular.copy(row.entity);
  vm.form = [
    'name',
    'company',
    'phone',
    {
      'key': 'address.city',
      'title': 'City'
    },
  ];

  vm.save = save;

  function save() {
    // Copy row values over
    row.entity = angular.extend(row.entity, vm.entity);
    $modalInstance.close(row.entity);
  }
}

回答1:


That is exactly what onRegisterApi is for:

      vm.gridOptions = {
      enableRowSelection: true,
      enableRowHeaderSelection: false,
      multiSelect: false,
      selectionRowHeaderWidth: 35,
      columnDefs: [
        { field: 'id', name: '', cellTemplate: 'edit-button.html', width: 34 },
        { name: 'name' },
        { name: 'company' },
        { name: 'phone' },
        { name: 'City', field: 'address.city' },
      ],
      onRegisterApi: function (gridApi){
           vm.gridApi = gridApi;
      }
      };

Then the gridApi will be in your scope (vm.gridApi) and you can access the grid and all the rows in gridApi.grid.

Hope this helps.



来源:https://stackoverflow.com/questions/35300854/how-can-i-access-ui-grid-isolate-scope-from-outside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!