Populating two nested grid in one call - angularjs

I am using kendo-ui with AngularJS. I have a parent grid and on the expansion of the row I have to show two sub grids, for that I don't want to call server separately. I have developed a service which returns data of both grids, now I want to bind the data to the grid. How to do that? Here is the code:
$scope.mainGridOptions = {
columns: [
{ field: "id", title:"ID",width: "50px" },
{ field: "name", title:"Name", width:"200px" },
],
dataSource: {
transport: {
read: function (e) {
var params = {};
var dataObj = ServerAPIs.getStudents(params, function(){
e.success(dataObj.result);
},function(){
alert('something went wrong');
});
}
},
pageSize: 20,
},
sortable: true,
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 5
},
filterable: true,
reorderable: true,
resizable: true,
detailInit: function(e) {
(function(e){
var params = {
"student-id" : e.data.id
};
var dataObj = ServerAPIs.getStudentDetails(params, function(data){
if(data.result.dataset1.length > 0){
//what to do here? how to access child grid1?
}
if(data.result.dataset2.length > 0){
//what to do here? how to access child grid2?
}
},function(){
alert('something went wrong');
});
})(e);
}
};
Here is the html:
<kendo-grid id='maingrid' options="mainGridOptions">
<div k-detail-template >
<div class='grid_loader'>
<br/>
<span class="spinner_forms"> </span>
<br/>
</div>
<div class='details' style='display:none'>
<div class="center-block" style="width: 800px;" kendo-grid k-options="grid1Options(dataItem)"></div>
<br />
<div class="center-block" style="width: 800px;" kendo-grid k-options="grid2Options(dataItem)"></div>
</div>
</div>

In Javascript, I selected child element using find method and initiated the grid.
detailInit: function(e) {
(function(e){
var params = {
"student-id" : e.data.id
};
var dataObj = ServerAPIs.getStudentDetails(params, function(data){
if(data.result.dataset1.length > 0){
e.detailRow.find(".child-grid-1").kendoGrid({
dataSource: {
data: data.result.dataset1,
},
columns: [//your columns here],
//other grid options
});
}
if(data.result.dataset2.length > 0){
e.detailRow.find(".child-grid-2").kendoGrid({
dataSource: {
data: data.result.dataset2,
},
columns: [//your columns here],
//other grid options
});
}
},function(){
alert('something went wrong');
});
})(e);
}
In html page, following modification needs to be made:
<kendo-grid id='maingrid' options="mainGridOptions">
<div k-detail-template >
<div class='grid_loader'>
<br/>
<span class="spinner_forms"> </span>
<br/>
</div>
<div class='details' style='display:none'>
<div class="child-grid-1" style="width: 800px;"></div>
<br />
<div class="child-grid-2" style="width: 800px;"></div>
</div>
</div>

Related

UI Grid won't display

Trying to follow the docs provided here on their website, but still not able to get it to work.
I am scanning a table using AWS DynamoDB, and trying to display the information in the table within this UI grid.
My controller:
angular.module('App')
.controller('GridCtrl', ['modelObjects', '$scope', 'dbCall', function(modelObjects, $scope, dbCall) {
$scope.gridOptions = {
enablesorting: true,
columndefs: [{
field: 'ref id'
}, {
field: 'group'
}, {
field: 'cost center'
}, {
field: 'cost center description',
}, {
field: 'recruitment firm',
enablesorting: false
}],
};
$scope.updateGrid = function(data) {
// for (var key in data) {
// var item = data[key];
// for (var key2 in item) {
// console.log(item[key2]);
// $scope.gridOptions.data = item[key2];
// }
// }
$scope.gridOptions.data = data;
};
$scope.scanPosition = function() {
var params = {};
return dbCall('addGrid', params, $scope.check);
};
$scope.check = function(err, data) {
if (err) {
$scope.results = "Failure: Unable To Connect To Database";
console.log(err);
}
else {
$scope.results = "Success: Position Table Scanned";
console.log(data);
$scope.updateGrid(data);
}
};
setTimeout(function() {
$scope.scanPosition();
}, 50);
}]);
My View:
<!DOCTYPE html>
<html>
<head></head>
<body ng-app="App">
<div class="col-lg-10 col-lg-offset-2 col-sm-9 col-sm-offset-3 col-xs-12">
<div class="container-fluid">
<br>
<div class="panel panel-default" style="align: center; border:1px solid #d4d4d4;">
<div class="panel-heading" style="text-align: center;">
<h3>Grid Page</h3>
</div>
</div>
<div ui-grid="gridOptions" class="myGrid"></div>
</div>
</div>
</body>
</html>
My database is working; I am able to loop and display the information in the console. It just isn't being displayed in the grid. I have no console errors.
Thanks in advance for any help!
It looks like you need to set the grid data like this:
$scope.gridOptions.data = data.Items;
Since data.Items is the array of objects.
In Addition to #GDan, there's no controller specified in the HTML file. Only ng-app="App". Try adding ng-controller="GridCtrl" in one of the parent divs surrounding the ui-grid directive.
CodePen and Snippet below:
http://codepen.io/Lethargicgeek/pen/rLbZGp?editors=1010
angular.module('App', ['ui.grid']);
(function() {
angular.module('App').controller('GridCtrl', ctrlFn);
ctrlFn.$inject = ['modelObjects', 'dbCall', '$timeout'];
function ctrlFn(modelObjects, dbCall, $timeout) {
var $ctrl = this;
// BINDING
$ctrl.gridOptions = {
enableSorting: true,
columnDefs: [{
field: 'ref id'
}, {
field: 'group'
}, {
field: 'cost center'
}, {
field: 'cost center description',
}, {
field: 'recruitment firm',
enablesorting: false
}],
};
// END BINDING
// INIT
$timeout(function() {
scanPosition();
}, 1000);
// END INIT
function updateGrid(data) {
$ctrl.gridOptions.data = data;
};
function scanPosition() {
var params = {};
return dbCall.dbCall('addGrid', params, check);
};
function check(err, data) {
if (err) {
$ctrl.results = "Failure: Unable To Connect To Database";
console.log(err);
} else {
$ctrl.results = "Success: Position Table Scanned";
console.log(data);
updateGrid(data);
}
};
}
})();
// Mocked Service modelObjects
(function() {
angular.module('App').service('modelObjects', svcFn);
function svcFn() {}
})();
// Mocked Service dbCall
(function() {
angular.module('App').service('dbCall', svcFn);
function svcFn() {
var svc = this;
svc.dbCall = dbCall;
function dbCall(action, params, callBackFn) {
// Should really use a promise rather than a callbackFn
callBackFn(null, [{
'ref id': "fooRef",
'group': "fooGroup",
"cost center": "fooCostCenter"
}]);
}
}
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
<script src="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js"></script>
<link href="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<html>
<head></head>
<body ng-app="App">
<div class="col-lg-10 col-lg-offset-2 col-sm-9 col-sm-offset-3 col-xs-12" ng-controller="GridCtrl as $ctrl">
<div class="container-fluid">
<div>$ctrl.results: {{$ctrl.results}}</div>
<div class="panel panel-default" style="align: center; border:1px solid #d4d4d4;">
<div class="panel-heading" style="text-align: center;">
<h3>Grid Page</h3>
</div>
</div>
<div ui-grid="$ctrl.gridOptions" class="myGrid"></div>
</div>
</div>
</body>
</html>
Try changing "columndefs" to "columnDefs" and "enablesorting" to "enableSorting". That's what I see off of the top, but I'm still looking. You're also missing "ng-controller="GridCtrl" after ng-app="App" in your HTML. Also, make sure you included the script for the grid and injected into your module.

Unable to set textbox value through $scope using angular UI Grid

<div data-ng-controller="OfficeController" class="container">
<div class="row col-md-12" style="margin:10px;">
<form name="Office">
<div class="col-sm-12">
<label for="Name" class="form-group col-sm-2 control-label">Name</label>
<div class="col-sm-4">
<input type="text" class="form-group col-xs-6" id="Name" placeholder="Name" ng-model="Name" required />
</div>
<label for="Org_ID" class="form-group col-sm-2 control-label">Org_ID</label>
<div class="col-sm-4">
<input type="text" class="form-group col-xs-6" id="Org_ID" placeholder="Org_ID" ng-model="Org_ID" required />
</div>
</div>
</form>
</div>
</div>
i have two textboxes in view
adding button to uigrid column for get row data and call get
$scope.gridOptions = {
data: 'Offices',
columnDefs: [
{ field: 'OFFICE_ID', displayName: 'OFFICE_ID' },
{ field: 'Name', displayName: 'Name' },
{ field: 'Org_ID', displayName: 'Org_ID' },
{
name: 'ShowScope',
cellTemplate: '<button class="btn primary" ng-click="grid.appScope.get(row)">Click Me</button>'
}
],
filterOptions: { filterText: '', useExternalFilter: false },
showFilter: true,
enableFiltering: true,
showGridFooter: true,
};
alert(res.Name) shows me correct name but i am unable to set textbox name value??
$scope.get = function (row) {
alert(row.entity.OFFICE_ID);
var promiseGetSingle = OfficeService.get(row.entity.OFFICE_ID);
promiseGetSingle.then(function (pl) {
var res = pl.data;
alert(res.Name);
$scope.OFFICE_ID = res.OFFICE_ID;
$scope.Org_ID = res.Org_ID;
$scope.Name = res.Name;
$scope.IsNewRecord = 0;
},
function (errorPl) {
console.log('failure loading Organization', errorPl);
});
}
Any idea how to set textbox value through scope,ir any method, what i want is when users click on row button i want to poulate fields by getting data from that particular row...in UI Grid Angularjs
Please try as shown below.
$scope.Name='';//define the variable globally
$scope.get = function (row) {
var promiseGetSingle = OfficeService.get(row.entity.OFFICE_ID);
promiseGetSingle.then(function (pl) {
$scope.Name = res.Name;
},
}

How to clear form after post?

I'm creating an app using Ionic. After send values of form I want to clear form but I can't do this.
How could I do this ?
form
<form name="Empresa">
<div class="list">
<label class="item item-input"
ng-class="{'has-errors':Empresa.nomeemp.$invalid, 'no-errors':Empresa.nomeemp.$valid}">
<input type="text"
placeholder="Nome"
name="nomeemp"
ng-model="Empresa.nome"
ng-required="true">
</label>
<div class="error-container"
ng-show="(Empresa.nomeemp.$dirty || !Empresa.nomeemp.$pristine)"
ng-messages="Empresa.nomeemp.$error">
<div ng-messages-include="templates/form-errors.html"></div>
</div>
</div>
<button type="button" class="button button-block button-energized"
ng-disabled="Empresa.$invalid" ng-click="addEmpresa(Empresa);">Cadastrar</button>
</form>
controller
$scope.addEmpresa = function(Empresa){
var _pathFile = document.getElementById("smallimage").src;
var _fileName = _pathFile.substr(_pathFile.lastIndexOf('/') + 1);
if(_fileName === 'no-image.png'){
$ionicLoading.show({ template: APP_MESSAGES.escolhaImagem, noBackdrop: true, duration: 2000 });
}else{
$ionicLoading.show();
EmpresaAPIService.addEmpresa(Empresa).then(function (data) {
$ionicLoading.hide();
var retorno = JSON.parse(data.response);
if(retorno.status === 0){
$ionicLoading.show({ template: retorno.msg, noBackdrop: true, duration: 2000 });
}
if(retorno.status === 1){
//clear form
delete Empresa;
Empresa.$setPristine();
Empresa.$setUntouched();
$ionicLoading.show({ template: retorno.msg, noBackdrop: true, duration: 2000 });
}
}, function (err) {
$ionicLoading.hide();
}, function (progress) {
// PROGRESS HANDLING GOES HERE
});
}
}
In your controller after you do your submission logic, set $scope.Empresa.noma = ''; as that's what is being bound to your form in your application.
edit: setPristine() should also work.

Kendo MultiSelect update of ngmodel

I'm trying to add one button for adding values to ngmodel of kendo's multi select:
<div ng-controller="MyCtrl">
<select id='my' kendo-multi-select k-options="selectOptions" k-ng-model="selectedIds"></select>
<p ng-show="selectedIds.length">Selected: {{ selectedIds }}</p>
<button ng-click="addSelectedId()">Add selected id</button>
<input ng-model="enteredId" />
</div>
Here is controller
function MyCtrl($scope) {
$scope.selectOptions = {
placeholder: "Select products...",
dataTextField: "ProductName",
dataValueField: "ProductID",
autoBind: false,
dataSource: {
type: "odata",
serverFiltering: true,
transport: {
read: {
url: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Products",
}
}
}
};
$scope.selectedIds = [ 4, 7];
$scope.addSelectedId = function() {
$scope.selectedIds.push(parseInt($scope.enteredId));
console.log($scope.selectedIds);
};
}
Plunker is here:
http://plnkr.co/edit/EH0EaMhFsV2JTdwpkqGg?p=preview
When added to selectedIds, nothing gets added to dropdown select placeholder. Any ideas?
You need to add k-rebind="selectedIds" in your html code
HTML:
<div ng-controller="MyCtrl">
<select id='my' kendo-multi-select k-options="selectOptions" k-ng-model="selectedIds" k-rebind="selectedIds"></select>
<p ng-show="selectedIds.length">Selected: {{ selectedIds }}</p>
<button ng-click="addSelectedId()">Add selected id</button>
<input ng-model="enteredId" />
</div>
Please see this updated plunker example

Open a Pop up window on the click of link on Kendo Grid column

I want to open a Pop Up window on the click of a link on a Kendo Grid column. The Pop window should contain the detailed data of the current row. Something like given in http://demos.telerik.com/kendo-ui/grid/custom-command link. But I want this to work on click of a link of an existing column rather than a new custom button.
HTML:
<div ng-app="myApp">
<div ng-controller="myCtrl">
<div id="grid" kendo-grid k-options="kendoGrid"></div>
</div>
</div>
Controller:
var myApp = angular.module('myApp',[]);
myApp.controller('myCtrl', function ($scope, myService) {
$scope.kendoGrid = myService.getKGrid();
});
Service:
myApp.service('myService', function () {
this.getKGrid = function () {
var kGrid = {
dataSource: [{"Col1":"Val1","Col2":"Val2"}],
columns: [{
field: "Col1",
title: "Col1"
},
{
field: "Col2",
title: "Col2",
template: "<a href='\\#' class='link' **ng-click='TBD(Open Pop-up window with row details)'**>#=Col2#</a>"
}
]
};
return kGrid;
};
});
Please guide how to achieve this.
Thanks in Advance.
I am able to display the pop up kendo window with row details by making below changes.
HTML:
<div ng-app="myApp">
<div ng-controller="myCtrl">
<div id="grid" kendo-grid k-options="kendoGrid"></div>
<div id="details"></div>
<div id="detailsTemplate" style="display: none;">
<div class="row">
<text>Data :</text> <text>#=Col1#</text><!--We can add more data here-->
</div>
</div>
</div>
</div>
Controller:
var myApp = angular.module('myApp',[]);
myApp.controller('myCtrl', function ($scope, myService) {
$scope.kendoGrid = myService.getKGrid();
$scope.showDetails = function (dataItem) {
myService.showWindow(dataItem);
}
});
Service:
myApp.service('myService', function () {
this.showWindow function () {
var window = angular.element(details)
.kendoWindow({
title: "Details",
modal: true,
visible: false,
resizable: false,
width: 600
}).data("kendoWindow");
var detailsTemplate = kendo.template(angular.element(detailsTemplate).html());
window.content(detailsTemplate(dataItem));
window.center().open();
};
this.getKGrid = function () {
var kGrid = {
dataSource: [{"Col1":"Val1","Col2":"Val2"}],
columns: [{
field: "Col1",
title: "Col1"
},
{
field: "Col2",
title: "Col2",
template: "<a href='\\#' class='link' ng-click='showDetails(dataItem)'>#=Col2#</a>"
}
]
};
return kGrid;
};
});

Resources