Getting values of the selected row in uiGrid angular - angularjs

I have a ui-grid, that allows the user selecting one row. If a row is selected I want to bind some fields of the selected row to input fields. How is this possible?
$scope.gridOptions = {
data : items,
columnDefs: [
{ name: 'ITEMNO', field: 'ITEMNO', displayName: 'Pos.', enableHiding : false, width: 75 },
{ name: 'SERIALN_REQ', field: 'SERIALN_REQ', displayName: 'Serialpflichtig', enableHiding : false, width: 175 }
],
enableScrollbars : false,
enableHorizontalScrollbar : 0,
enableVerticalScrollbar : 0,
enableFullRowSelection : true,
enableRowSelection: true,
enableSelectAll: false,
multiSelect : false,
selectionRowHeaderWidth: 0,
rowHeight: 55,
enablePaginationControls: false,
paginationPageSize: 5
};
$scope.gridOptions.onRegisterApi = function (gridApi) {
$scope.gridApi = gridApi;
$scope.gridApi.grid.modifyRows($scope.gridOptions.data);
$scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
var rows = $scope.gridApi.selection.getSelectedRows();
}
In my HTML i have the following:
<div class="large-2 columns">
<input type="text" disabled ng-model={{}}> <!-- here i want to bind a cell of the selected grid row -->
</div><div ui-grid="gridOptions" ui-grid-selection ui-grid-pagination class="myGrid"></div>

Here is your expected one(get selected row values and bind them to the input elements).
Use the below code
$scope.rowtobebinded=undefined;
$scope.gridOptions.onRegisterApi = function(gridApi) {
$scope.gridApi = gridApi;
$scope.gridApi.grid.modifyRows($scope.gridOptions.data);
$scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
var rows = $scope.gridApi.selection.getSelectedRows();
//aravind's code to get the selected row elements is as below
gridApi.selection.on.rowSelectionChanged($scope,function(row){
console.log("selected row is ",row);
//binding the row's no to the textbox
$scope.rowtobebinded=row.entity.ITEMNO;
});
});
LIVE DEMO

Related

How to create multiple ui grid dynamically with ng-repeat in html page?

I am using an ng-repeat loop inside my html page like this
<li class="list-group-item" ng-repeat="obj in Students">
<div class="grid" id="{{'grid'+$index}}" ui-grid="{{'griddata'+$index}}"
ui-grid-selection ui-grid-pagination>
</div>
</li>
And here is my script to create ui grid dynamically
$scope.CreateGrids = function () {
debugger;
for (var count = 0; count < $scope.StudentList.length; count++) {
var studentData = "griddata"+count;
$scope[studentData] = [];
// defining the grid control
$scope[studentData] = {
enableRowSelection: false,
//grid pagination
paginationPageSizes: [10, 25, 50, 75],
paginationPageSize: 10,
enableSorting: true,
//enabling filtering
enableFiltering: false,
multiSelect: false,
//column definations
columnDefs: [
{ name: 'SubjectID', displayName: 'SubjectID', visible: false },
{ name: 'SubjectName', displayName: 'Subject Name' },
{ name: 'MarksObtained', displayName: 'Marks' },
]
};
$scope[studentData].data = $scope.StudentList[count].Student;
}
};
But after so many hit and trial, I am not able to create the grid.
How can I achieve it ?
Thanks in advance.
Here is the solution please check plunkr link
HTML code
<li class="list-group-item" ng-repeat="obj in data">
<div class="grid" id="{{'grid'+$index}}" ui-grid="{data:data['griddata'+$index].columnDefs}"
ui-grid-selection ui-grid-pagination>
</div>
</li>
</
JS code
$scope.StudentList = [1, 2, 3, 4, 5];
$scope.data = {};
$scope.CreateGrids = function() {
for (var count = 0; count < $scope.StudentList.length; count++) {
var studentData = "griddata" + count;
$scope.data[studentData] = [];
// defining the grid control
$scope.data[studentData] = {
enableRowSelection: false,
//grid pagination
paginationPageSizes: [10, 25, 50, 75],
paginationPageSize: 10,
enableSorting: true,
//enabling filtering
enableFiltering: false,
multiSelect: false,
//column definations
columnDefs: [{
name: 'SubjectID',
displayName: 'SubjectID',
visible: false
}, {
name: 'SubjectName',
displayName: 'Subject Name'
}, {
name: 'MarksObtained',
displayName: 'Marks'
}, ]
};
$scope.data[studentData].data = $scope.StudentList[count].Student;
}
};
$scope.CreateGrids();
Hope this will help you

angularjs ui-grid datetime picker was not working

grid for angularjs project. I need datetime picker for the filter.
So I have used angular angular-bootstrap-datetimepicker-directive for that
Now my filter text box shows the datetimepicker.
But when i change the date it doesn't trigger filter change event
Here is my code:
$scope.gridOptions = {
paginationPageSizes : [1],
paginationPageSize : 1,
useExternalPagination: true,
showGridFooter : true,
showColumnFooter : true,
enableFiltering : true,
useExternalFiltering : true,
onRegisterApi : function(gridApi) {
$scope.gridApi = gridApi;
$scope.gridApi.core.on.filterChanged($scope, function(){
console.log("teststs");
angular.forEach(this.grid.columns, function(column){
console.log(column.filters[0].term);
if(column.filters[0].term)
$scope.searchParams[column.name] = column.filters[0].term;
else if(column.filters[0].term === null)
delete $scope.searchParams[column.name];
});
getPages(1, $scope.searchParams);
});
},
columnDefs : [
{
field: 'createdAt',
filterHeaderTemplate: '<div class="ui-grid-filter-container"><input type="text" id="dt1" class="form-control input-sm" datetimepicker ng-model="colFilter.term" placeholder="RAMA" name="time"></div>',
cellFilter: 'date: "yyyy-MM-dd"'
},
{
field: 'updatedAt',
cellFilter: 'date: "' + ENV.dateFormat + '"'
}
]
};
on the change event of the datepicker, you could add:
$scope.gridPacksApi.core.raise.filterChanged();
to force the event.

Complex logic as ToolTip on the Angular UI Grid's cellTemplate

I'm using Angular UI-Grid.
This is the grid implementation:
JS
var statusCellTemplate =
'<div class="ui-grid-cell-contents"><span ng-
if="row.entity.statusDetails.length>=2" title="My Tooltip">[Multiple Statuses]
</span></span></div>';
vm.propertyListGridOptions = {
appScopeProvider: vm,
flatEntityAccess: false,
fastWatch: true,
showHeader: false,
columnDefs: [
{
name: app.localize('ListingStatus'),
cellTemplate: statusCellTemplate,
width: 240,
},
],
data: []
};
I need to show the ToolTip by using below logic.Can you tell me how to implement such a logic with the above cellTemplate ?
_.each(row.entity.statusDetails, function (s) {
vm.statusValues += s.status.name + ', ';
});
Note : _.each can be a ng-repeat on angular.

How can I display a grid repeater using ui-grid?

I have data of messages including {id,message,date}.
I would like to display a grid for each Date with data{message} in AngularJs using ui-grid
I was thinking of something like this:
<div ng-repeat="(item in data | groupBy: 'Date'">
<div>{{ item.Date }}</div>
<div id="grid1" ui-grid="gridOptions(item.Date) " class="grid"></div>
</div>
but it's not working!
$scope.gridOptions = function (date) {
return {
enableSorting: true,
enableFiltering: true,
data: 'data',
columnDefs: $scope.filterGrid(date),
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
},
};
};
$scope.filterGrid= function (date){
return [
{ field: 'id', enableFiltering: False},
{
field: 'Date', enableFiltering: false, filter: {
noTerm: true,
condition: function (searchTerm, cellValue) {
return cellValue.match(date);
}
}
},
{ field: 'Message' , enableFiltering: false },
]
}
First of all - ui-grid has a grouping feature, but this is still in beta.
You can try to use this example to group the data and build grids accordingly.
var dataSource = {};
$scope.gridOptions = {};
var generalGridOptions = {
enableSorting: true,
columnDefs: [
{ name: 'prop1' },
{ name: 'prop2' },
],
data: null
};
// group the data
for(var i = 0; i < data.length; i++){
if(!dataSource[data[i].month]){
dataSource[data[i].month] = [];
}
var obj = {};
for(var prop in data[i]){
if(prop!='month'){
obj[prop] = data[i][prop];
}
}
dataSource[data[i].month].push(obj);
}
// build the grid options
for (var item in dataSource) {
$scope.gridOptions[item] = angular.copy(generalGridOptions);
$scope.gridOptions[item].data = dataSource[item];
}
ui-grid attribute recieves a gridOptions object containing several parameters. Two of them are:
columnDefs - defining the columns and their data-binding.
data - the message objects in your case.
Look at the the documentation for further study: http://ui-grid.info/docs/#/tutorial
Code Example
template:
<div ng-repeat='item in items track by item.id' ui-grid='getItemGridOptions($index)'></div>
Passing the item index to the controller allows you to process your data. Then you can return an object containing the data and columnDefs properties. Like this:
private getItemGridOptions(index): IGridOptions {
//get the data you need for your items array using the index...
// then return the gridOptions object (I put random values)
return {
columnDefs: this.columns,
rowHeight: 24,
rowTemplate: rowtpl,
enableHorizontalScrollbar: this.uiGridConstants.scrollbars.NEVER,
enableColumnMenus: false,
enableRowSelection: true,
enableRowHeaderSelection: false,
enableFiltering: true,
modifierKeysToMultiSelect: true,
multiSelect: true,
data: null,
};
}

ui-grid columns collapsing when width:"**" is specified

I am working on a project where I have used UI-Grid to show list of users. I have specified width:"**" for each column in column definition. In output, all the columns are collapsed in left side. When I resizes browser window or inspects element, then width is auto adjusted quickly. But for first time, all columns are displayed collapsed. Here is my code:
$scope.columns = [
{ field: 'name', name:'Name', width: "**", cellTooltip : false,enablePinning:true,
cellTemplate:'<div class="ui-grid-cell-contents">'+
'<a ng-href="#/account/profile?id={$ grid.appScope.getUserID(grid, row) $}"> {$ grid.appScope.getUserRow(grid, row,"uname") $} </a>'+
'</div>'
},
{ field: 'email', name:'Email', width: "**", cellTooltip : false,enablePinning:true },
{ field: 'role', name:'Role', width: "**", enableSorting: true,
cellTemplate:'<div class="ui-grid-cell-contents"> '+
'{$ grid.appScope.getUserRow(grid, row,"role") $}'+
'<a ng-click="grid.appScope.assignRole({$ grid.appScope.getUserID(grid, row) $})"> add </a>'+
'</div>'
},
{ field: 'isInvited', name:'Invitation status', width: "**", cellTooltip : false,enablePinning:true },
];
$scope.gridOptions = {
showGridFooter: true,
enableSorting: true,
enableGridMenu: true,
enableSelectAll: true,
enableFiltering: true,
enableHorizontalScrollbar : 1,
paginationPageSizes: [10,25, 50, 100],
useExternalPagination: true,
columnDefs: $scope.columns,
enableGridMenu: true,
enableSelectAll: true,
exporterCsvFilename: 'myFile.csv',
exporterPdfDefaultStyle: {fontSize: 9},
exporterPdfTableStyle: {margin: [10, 10, 10, 10]},
exporterPdfTableHeaderStyle: {fontSize: 10, bold: true, italics: true, color: 'red'},
exporterPdfHeader: { text: "My Header", style: 'headerStyle' },
exporterPdfFooter: function ( currentPage, pageCount ) {
return { text: currentPage.toString() + ' of ' + pageCount.toString(), style: 'footerStyle' };
},
exporterPdfCustomFormatter: function ( docDefinition ) {
docDefinition.styles.headerStyle = { fontSize: 22, bold: true };
docDefinition.styles.footerStyle = { fontSize: 10, bold: true };
return docDefinition;
},
exporterCsvLinkElement: angular.element(document.querySelectorAll(".custom-csv-link-location")),
onRegisterApi: function(gridApi){
$scope.gridApi = gridApi;
$scope.gridApi.core.on.sortChanged( $scope, function( grid, sort ) {
$scope.gridApi.core.notifyDataChange( uiGridConstants.dataChange.COLUMN );
});
$scope.gridApi.pagination.on.paginationChanged( $scope, function( currentPage, pageSize){
$scope.getAppDetails(currentPage, pageSize);
});
}
};
I just noticed if you resize the window it renders the grid properly. So a quick and dirty work around and not really intended as a solution, is watching for when the grid becomes visible and calling grid.handleWindowResize() on the gridApi.
Also a $timeout to ensure the rendering is performed after the tab has fully displayed. Here's how I watch:
$scope.test = {isActive: false};
$scope.$watch('test.isActive', function (active, oldActive) {
if(active) {
$timeout(function () {
$scope.gridApi.grid.handleWindowResize();
});
}
});

Resources