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

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);
}
}

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.

Related

Kendo Grid Command parent controller function into directive-based template

I am using Kendo UI's Angular grid. I am trying to pass a parent controller's function to a template that is based on another Angular directive (as a handler). I have a component with a controller that grabs the Kendo Grid options from a service.
(function () {
'use strict';
angular.module('ins.admin.userreg')
.component('insUserRegGrid', {
templateUrl: 'admin/user-registration/grid/user-reg-grid.tpl.html',
controllerAs: 'vm',
controller: controller
});
controller.$inject = ['insUserRegGridSvc', 'insUserRegRouteSvc', '$timeout'];
function controller(insUserRegGridSvc, insUserRegRouteSvc, $timeout) {
var vm = this;
vm.options = insUserRegGridSvc.getOptions();
vm.goToCreate = insUserRegRouteSvc.goToCreate;
vm.onActiveChange = function(value) {
console.log('changed' + value);
}
}
})();
Here are the options within that service (where the command template for Active is defined):
(function () {
'use strict';
angular.module('ins.admin.userreg')
.factory('insUserRegGridSvc', insUserRegGridSvc);
insUserRegGridSvc.$inject = ['_', 'insUserRegResrc', 'insFormatUtil', 'insAppSettingsSvc'];
function insUserRegGridSvc(_, insUserRegResrc, insFormatUtil, insAppSettingsSvc) {
function getOptions() {
return {
dataSource: {
transport: {
read: function (e) {
insUserRegResrc.getUsersWithRoles().$promise.then(function (response) {
response.map(function (i) {
i.UserID = i.UserId;
i.Url = insAppSettingsSvc.apiRoot + 'UserRegistration/Post/' + i.UserId;
i.FirstName = i.FirstName;
i.MiddleName = i.MiddleName;
i.LastName = i.LastName;
if (i.Role[0]) {
i.RoleName = i.Role[0].RoleName;
} else {
i.RoleName = "";
}
if (i.Reports[0]) {
i.Manager = i.Reports[0].FullName;
} else {
i.Manager = "";
}
i.Email = i.Email;
i.Active = i.IsActive;
});
response = _.orderBy(response, ['UserId'], ['asc']);
e.success(response);
});
}
},
pageSize: 10
},
resizable: true,
pageable: true,
editable: false,
sortable: true,
toolbar: "<a ng-click='vm.goToCreate()' class='btn ins-btn-update k-grid-add pull-left' title='Add New'>add</a>",
columns: [
{ title: 'User Id', field: 'UserID', sortable: true, width: 150 },
{ title: 'First Name', field: 'FirstName', sortable: true, width: 150 },
{ title: 'Last Name', field: 'LastName', sortable: true, width: 150 },
{ title: 'Role', field: 'RoleName', sortable: true, width: 150 },
{ title: 'Manager', field: 'Manager', sortable: true, width: 150 },
{ title: 'Email', field: 'Email', sortable: true, width: 150 },
{
title: 'Active',
width: 85,
command: [
{
name: "Toggle Active",
template: "<ins-toggle ins-id='activeToggle' ins-model='dataItem.Active' ins-on-change='vm.onActiveChange' ins-on-text='yes' ins-off-text='no' ins-enabled='true'></ins-toggle>"
}
]
},
{
width: 85,
command: [
{ name: "Edit", template: "<a class='k-button k-button-icontext k-grid-edit' data-toggle='tooltip' ui-sref='.edit({ userId: {{dataItem.UserID}}})' title='#: name #'> <span class='k-icon k-edit'></span> </a>" }
]
}
]
}
}
return {
getOptions: getOptions
};
}
})();
From the above code, focus on the following:
{
title: 'Active',
width: 85,
command: [
{
name: "Toggle Active",
template: "<ins-toggle ins-id='activeToggle' ins-model='dataItem.Active' ins-on-change='vm.onActiveChange' ins-on-text='yes' ins-off-text='no' ins-enabled='true'></ins-toggle>"
}
]
},
As you can see I am trying to pass the parent controller's vm.onActiveChange method as a scope attribute to ins-toggle. ins-toggle's ins-on-change scope attribute is setup as insOnChange: '&?' and works in other scenarios outside of Kendo's grid.
Does Kendo not allow for us to pass parent functions into directive's as a part of a template?
I have found out that it has nothing to do with Kendo, and it actually had to do with the Toggle directive I was using from UI-Bootstrap. Sorry everyone.

Select some entities from row in ui-grid

I have in my grid sometimes few columns, I hide some that only two are visible. I want to select rows only with these two entities(visible) and pass them to array. In this example Date and Result. Is there specific method to do this? Because when I use
vm.chartData.push($scope.gridApi.selection.getSelectedRows()) in chartData array I have got all entities.
This is my controller code.
(function () { "use strict";
angular.module("app")
.controller("grid", grid);
function grid($scope) {
var vm = this;
vm.chartData = [];
$scope.gridOptions =
{
enableGridMenu: true,
enableSorting: true,
enableRowSelection: true,
selectionRowHeaderWidth: 35,
enableSelectAll: true,
multiSelect: true,
columnDefs: [
{ name: 'Trial', field: 'Trial' },
{ name: 'Date', field: 'Date', type: "date", cellFilter: "date:'dd-MM-yyyy HH:mm'" },
{ name: 'Result', field: 'Result'}
],
data: [
//example of row entities
{
"Trial": "Weight",
"Date": "2015-02-03T05:49:54.850Z",
"Result": 60
}
]
};
$scope.pushAll = function () {
//this line returns all three entities
vm.chartData.push($scope.gridApi.selection.getSelectedRows());
};
$scope.gridOptions.onRegisterApi = function (gridApi) {
$scope.gridApi = gridApi;
};
}

Angular UI Grid How to bind value from variable

is it possible to bind value from the $scope variable in Angular UI Grid?
There is a $scope.number value which I want to bind and show in Number column:
app.controller('MainCtrl', function ($scope, $http) {
$scope.gridOptions = { enableRowSelection: true, enableRowHeaderSelection: false };
$scope.number = 01234567;
$scope.gridOptions.columnDefs = [
{ field: "contactId", displayName: "CID", width: 60 },
{ field: "name", displayName: "Contact Name" },
{ field: "number", displayName: "Number", cellTemplates: '<div class="ui-grid-cell-contents"> {{row.entity.number}} </div>'}
];
$scope.contacts = [];
$http.get('contacts.json').success(function (data) {
data.forEach( function( row, index ) {
$scope.contacts.push(row);
});
$scope.gridOptions.data = data;
console.log('data from contacts.json', data);
});
});
I'm trying to define row.entity in CellTemplate property, but it doesn't work. plunker
You would have to add the $scope variable to your data array:
var app = angular.module('app', ['ui.grid']);
app.controller('MainCtrl', function ($scope, $http) {
$scope.gridOptions = { enableRowSelection: true, enableRowHeaderSelection: false };
$scope.number = 01234567;
$scope.gridOptions.columnDefs = [
{ field: "contactId", displayName: "CID", width: 60 },
{ field: "name", displayName: "Contact Name" },
{ field: "number", displayName: "Number", cellTemplates: '<div class="ui-grid-cell-contents"> {{row.entity.number}} </div>'}
];
$scope.contacts = [];
$http.get('contacts.json').success(function (data) {
data.forEach( function( row, index ) {
row.number = $scope.number;
$scope.contacts.push(row);
});
$scope.gridOptions.data = data;
console.log('data from contacts.json', data);
});
});

DropDown not render in UI Grid angular js

My view have below code section but when user click on cell edit option is avilable but in textbox instead-of dropdown list:
<div id="grdAreaDetails" ui-grid="gridOptions" class="grid" ui-grid-edit ui-grid-row-edit></div>
And controller is below
$scope.cellSelectEditableTemplate = '<select ng-model="COL_FIELD" ><option value="volvo">Volvo</option><option value="saab">Saab</option></select>';
$scope.maxLength = 200;
$scope.sites = [];
$scope.showloadingdiv = true;
$scope.isAreaAddPanelHide = false;
$scope.gridOptions = {};
$scope.gridOptions.columnDefs = [
{
name: 'code', field: 'code', enableCellEditOnFocus: true,
editableCellTemplate: $scope.cellSelectEditableTemplate
},
{ name: 'name',field:'name'},
{ name: 'notes',field:'notes'},
{ name: 'description', field: 'description' },
{ name: 'siteid', field: 'siteid' },
{ name: 'status',field:'status' }
];
$scope.gridOptions = {
data: $scope.AreaRecord,
multiSelect: false,
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
gridApi.rowEdit.on.saveRow($scope, $scope.UpdateArea);
}
}
Just Remove enableCellEditOnFocus: true and add " enableCellSelection: true"
And let me know what happens.
$scope.gridOptions.columnDefs = [
{
name: 'code', field: 'code' , enableCellSelection: true,
editableCellTemplate: $scope.cellSelectEditableTemplate
},
{ name: 'name',field:'name'},
{ name: 'notes',field:'notes'},
{ name: 'description', field: 'description' },
{ name: 'siteid', field: 'siteid' },
{ name: 'status',field:'status' }
];
If this doesn't work then make fiddle.

Populate ng-grid by another grid selection

I am trying to populate a ng-grid based on the JSON array returned from a selection of a first ng-grid. As of right now I can get the JSON array displayed onto the screen but I cannot navigate deeper into the JSON array or get anything to display in the second grid. I have the controller code attached and the plnkr can be found at http://plnkr.co/edit/nULoI4?p=info.
'use strict';
function ArticleDataCtrl($rootScope, $scope, articleDataService) {
articleDataService
.getArticles()
.then(
function(articles) {
$rootScope.articles = articles;
$scope.articleGridItems = articles.data.specialMerchandise.specialMerItem;
});
$scope.articleGrid = {
data: 'articleGridItems',
showGroupPanel: false,
multiSelect: true,
checkboxHeaderTemplate: '<input class="ngSelectionHeader" type="checkbox" ng-click="getDeliveryLocations()" ng-model="allSelected" ng-change="toggleSelectAll(allSelected)"/>',
showSelectionCheckbox: true,
selectWithCheckboxOnly: true,
enableColumnResize: true,
selectedItems: [],
columnDefs: [{
field: 'soMerArticleNbr',
displayName: 'Article'
}, {
field: 'soMerOrdQty',
displayName: 'Qty'
}, {
field: 'soArtDeliveryCode',
displayName: 'Delivery Code'
}, {
field: 'dsgnSysRecDesc',
displayName: 'Description'
}]
};
//This is not being called on header template click
$scope.getDeliveryLocations = function() {
$scope.deliveryLocationData = $scope.commonDeliveryLocations;
};
$scope.selections = $scope.articleGrid.selectedItems;
var jsonObject = JSON.stringify($scope.selections);
//Thought a json problem occured here...was wrong
$scope.test = jsonObject.deliveryLocations;
$scope.deliveryGrid = {
data: 'selections',
showGroupPanel: false,
multiSelect: false,
columnDefs: [{
displayName: 'Delivery Methods'
}]
};
}
myApp.controller('ArticleDataCtrl', ['$rootScope', '$scope',
'articleDataService', ArticleDataCtrl
]);
So instead of trying to use angular's built in checkboxes I used my own with a custom method on ng-click. Here is the code and the plunker demonstrating the functionality is http://plnkr.co/edit/nULoI4?p=info.
'use strict';
function ArticleDataCtrl($rootScope, $scope, articleDataService) {
articleDataService
.getArticles()
.then(
function(articles) {
$rootScope.articles = articles;
$scope.articleGridItems = articles.data.specialMerchandise.specialMerItem;
});
$scope.articleGrid = {
data: 'articleGridItems',
showGroupPanel: false,
multiSelect: false,
enableColumnResize: true,
selectWithCheckboxOnly: true,
columnDefs: [{
/*
headerCellTemplate: myHeaderCellTemplate,
*/
cellTemplate: '<input id="checkSlave" name="articleCheckBox" ng-checked="master" type="checkbox" ng-click="getDeliveryLocation(row.entity)" />'
}, {
field: 'soMerArticleNbr',
displayName: 'Article'
}, {
field: 'soMerOrdQty',
displayName: 'Qty'
}, {
field: 'soMerUOIDesc',
displayName: 'UOM'
}, {
field: 'soArtDeliveryCode',
displayName: 'Delivery Code'
}, {
field: 'soMerShrtMerDesc',
displayName: 'Description'
}, {
field: 'soMerDesc',
displayName: 'Vendor'
}]
};
$scope.getDeliveryLocation = function(deliveryLocation) {
$scope.deliveryLocationData = deliveryLocation.deliveryLocation;
for (var i = 0; i < $scope.deliveryLocationData.length; i++) {
var locationId = $scope.deliveryLocationData[i].dlvryLocId;
var locationDesc = $scope.deliveryLocationData[i].dlveryLocDesc;
$scope.deliveryLocationData[i].dlvryLocId = locationId + locationDesc;
}
return $scope.deliveryLocationData;
};
return $scope.deliveryLocationData;
};
$scope.deliveryGrid = {
data: 'deliveryLocationData',
showGroupPanel: false,
multiSelect: false,
columnDefs: [{
field: 'dlvryLocId',
displayName: 'Delivery Methods'
}]
};
$scope.customerGroup = {
value: 'DIY'
};
}
myApp.controller('ArticleDataCtrl', ['$rootScope', '$scope',
'articleDataService', ArticleDataCtrl
]);

Resources