ng grid how to change style of individual cell in nggrid? - angularjs

Just starting out with the ng-grid , how can I change the css of an individual cell in a grid.
Things like backgroundcolor but also disable and enabling of just 1 cell in the current selected row?
plunkr:http://plnkr.co/edit/zQTRd7hOR6vpVZlgfHq0?p=preview

If you define your columns with columnDefs, then you have much more flexibility. You can then add a property to the definition called cellClass that you can define in your stylesheet.
All of the column definition properties are displayed on the ng-grid wiki. If you want you can redefine the cell or header template and write custom code for anything you want.
Here is an updated plunker.
And the changed code:
$scope.gridOptions = {
data: 'myData',
selectedItems: $scope.mySelections,
enableCellEditOnFocus: true,
multiSelect: true,
afterSelectionChange: function() {
$scope.selectedIDs = [];
angular.forEach($scope.mySelections, function(item) {
$scope.selectedIDs.push(item.name)
});
},
columnDefs: [
{field:'id', displayName:'Id', cellClass: 'red'},
{field:'name', displayName:'Name', cellClass: 'blue'}
]
};

If you want you can also put a template:
$scope.gridOptions = {
data: 'myData',
selectedItems: $scope.mySelections,
enableCellEditOnFocus: true,
multiSelect: true,
afterSelectionChange: function() {
$scope.selectedIDs = [];
angular.forEach($scope.mySelections, function(item) {
$scope.selectedIDs.push(item.name)
});
},
columnDefs: [
{field:'id', displayName:'Id', cellTemplate: '<div class="ui-grid-cell-contents v-align-middle padding-20"><p>{{COL_FIELD}}</p></div>'},
{field:'name', displayName:'Name', cellTemplate: '<div class="ui-grid-cell-contents v-align-middle padding-20"><p>{{COL_FIELD}}</p></div>'}
]
};

Related

how to apply sorting in ui-grid?

I have one angularjs project in which I am using ui-grid.
As you can see in my code, I have some attribute in cell-template. I have to perform sorting based on "rating-percentage".
So I am trying to write a sorting algorithm which required row.entity property.
My question is that how can I add rating percentage in row.entity.
What is the right way to perform sorting using "rating-percentage"?
Below is my code:
define([
'angular'
], function (angular) {
function SelectToolController($scope, $timeout, $filter) {
var vm = this,
_gridApi,
_cellTemplate,
_columnDefs,
_starRatingTemplate,
_starRatingColumn;
_starRatingTemplate =[
'<div class="ui-grid-cell-contents ">',
'<div class="opr-star-rating" >',
'<opr-star-rating rating-percentage="{{rowRenderIndex}}"',
'total-count="23"',
'suffix-display="true"',
'rating-display="true"',
'count-display="22">',
'</opr-star-rating>',
'</div>',
'</div>'
].join('');
_starRatingColumn = {
name: 'starRating',
cellTemplate: _starRatingTemplate,
displayName: 'Rating',
enableFiltering: false,
enableSorting: true,
cellClass: 'star-column',
enableHiding: false,
enableRowSelection: true,
}
_columnDefs.push(_starRatingColumn);
})();
You can define your custom sorting function then link this function to column definition.
$scope.sortFun=function(a, b) {
///.... your logic
}
_starRatingColumn = {
name: 'starRating',
cellTemplate: _starRatingTemplate,
displayName: 'Rating',
enableFiltering: false,
enableSorting: true,
cellClass: 'star-column',
enableHiding: false,
enableRowSelection: true,
'sortingAlgorithm': $scope.sortFun
}
change your template.
'<div class="ui-grid-cell-contents ">',
'<div class="opr-star-rating" ng-class="col.colIndex() >',
'<opr-star-rating rating-percentage="{{row.entity}}"',
'total-count="23"',
'suffix-display="true"',
'rating-display="true"',
'count-display="22">',
'</opr-star-rating>',
'</div>',
'</div>'

Angular ui-grid disable cell focus for row selection checkboxes

I have a ui-grid with several columns. Two of those columns are editable. I have also enabled row selection which makes the checkboxes appear in the left most column of the row.
I have disabled cell focus for non-editable columns. What I want to achieve is when I edit one of the editable column cell and hit tab, it should navigate only between the editable columns. The problem with the current setup is that the focus shifts from the editable cells in a row to the selection checkbox in the next row instead of going to next editable cell. I want to disable focus for the selection checkboxes.
vm.tableOptions = {
appScopeProvider: vm,
data: [],
enableSorting: true,
enableFiltering: true,
enableRowSelection: true,
enableColumnResizing: true,
enableSelectAll: true,
multiSelect: true,
enableGridMenu: true,
virtualizationThreshold: 18,
fastWatch: true,
scrollDebounce: 500,
wheelScrollThrottle: 500,
rowHeight: 40,
headerRowHeight: 40,
minRowsToShow: 15,
paginationPageSize: 25,
paginationPageSizes: [],
treeRowHeaderAlwaysVisible: false,
saveScroll: true,
saveGroupingExpandedStates: true,
enableCellEditOnFocus: true,
onRegisterApi: function (gridApi) {
vm.gridApi = gridApi;
/**
* Checks if any rows are selected in the tasks grid.
* #returns True if any tasks are selected; otherwise, false.
*/
function anyRowsSelected() {
var selectedRows = vm.gridApi.selection.getSelectedRows();
return selectedRows ? selectedRows.length > 0 : false;
};
// Check if any tasks are selected when the selection changes
gridApi.selection.on.rowSelectionChanged(null, function (row) {
vm.isRowsSelected = anyRowsSelected();
});
// Check if any tasks are selected when batch selection changes
gridApi.selection.on.rowSelectionChangedBatch(null, function (rows) {
vm.isRowsSelected = anyRowsSelected();
});
// This is a hack to manually move the focus to next editable cell
// I want to avoid this since this is causing some issues.
gridApi.cellNav.on.navigate($scope, function (newRowCol, oldRowCol) {
var assetColIndex = 4;
if (newRowCol.col.name === "selectionRowHeaderCol" &&
oldRowCol.col.name === "SerialNumber") {
vm.gridApi.cellNav.scrollToFocus(newRowCol.row.entity, vm.tableOptions.columnDefs[assetColIndex]);
}
});
$timeout(vm.restoreGridState, 50);
},
columnDefs: [
{ name: "DiscreteSkuId", enableCellEdit: false, allowCellFocus: false },
{
name: "SlotBin",
cellTemplate: slotBinCellTemplate,
enableCellEdit: false,
allowCellFocus: false,
sort: {
direction: 'desc',
priority: 0
}
},
{ name: "Model", enableCellEdit: false, allowCellFocus: false },
{ name: "AssetType", enableCellEdit: false, allowCellFocus: false },
{ name: "AssetTag" , cellTemplate: editableCellTemplate, cellEditableCondition: allowTableEdit },
{ name: "SerialNumber" , cellTemplate: editableCellTemplate, cellEditableCondition: allowTableEdit },
{ name: "ValidationFailureReasons", cellTemplate: errorMessageCellTemplate, enableCellEdit: false, allowCellFocus: false }
]
};
This is in the html page.
<div id="table" ui-grid="rackItemDetailsCtrl.tableOptions" ng-disabled="rackItemDetailsCtrl.disableUserInteraction" class="grid tasks-table ui-grid-row-hover" ui-grid-save-state ui-grid-resize-columns ui-grid-selection ui-grid-grouping ui-grid-move-columns ui-grid-auto-resize ui-grid-scrolling-fix ui-grid-edit ui-grid-cellNav></div>
Angular ui-grid version: 3.1.0.1
Angularjs version: 1.4.9
You can disable cell focus on a column with the allowCellFocus flag. However, the row selection column is created automatically and you have no way to set this flag. The solution to to manually access the column after it is created and set allowCellFocus to false. Here is how you can do that:
gridApi.grid.getColumn('selectionRowHeaderCol').colDef.allowCellFocus = false;

AngularJS cell navigation not working while using with celltemplate

I am having following code in my controller:
app.controller("homeController", homeController);
homeController.$inject = ["ui.grid.cellNav", "$scope"];
$scope.gridOptions = {
enableRowSelection: true,
enableSelectAll: true,
modifierKeysToMultiSelect: false,
enableRowHeaderSelection: false,
};
vm.objectViewGridOptions = {
enableColumnMenus: false,
enableSorting: true,
enableGridMenu: true,
angularCompileRows: true,
multiSelect: false,
modifierKeysToMultiSelect: false,
enableRowSelection: true,
enableRowHeaderSelection: false,
exporterMenuPdf: false,
onRegisterApi: function (gridApi) {
//set gridApi on scope
vm.objectViewGridOptions.gridApi = gridApi;
gridApi.cellNav.on.navigate($scope, function (newRowCol, oldRowCol){
vm.objectViewGridOptions.gridApi.selection.selectRow(newRowCol.row.entity);
vm.objectViewGridOptions.gridApi.core.notifyDataChange($scope.gridApi.grid, uiGridConstants.dataChange.COLUMN);
});
}
};
var colDef = [];
for (var i = 0; i < columnNames.length; i++) {
if (i < 4) {
colDef.push(
{
field: columnNames[i],
displayName: columnNames[i],
width: '100',
// Highlighting first row in ui-grid
cellTemplate: '<div ui-grid-selection class="ui-grid-cell-contents" ng-style="{\'background-color\':grid.appScope.getBackgroundColor(row,true)}">{{ COL_FIELD }}</div>',
sortingAlgorithm: sort
});
}
else
colDef.push(
{
field: columnNames[i],
displayName: columnNames[i],
width: '100',
cellTemplate: '<div ui-grid-selection class="ui-grid-cell-contents" ng-style="{\'background-color\':grid.appScope.getBackgroundColor(row,false)}" >{{ COL_FIELD }}</div>',
sortingAlgorithm: sort
});
}
vm.objectViewGridOptions.columnDefs = colDef;
And in my HTML:
<div style="clear:both;" ui-grid="vm.objectViewGridOptions" ng-if="vm.objectViewGridOptions.data" ui-grid-resize-columns ui-grid-cellnav ui-grid-move-columns ui-grid-exporter class="objectviewgrid"></div>
I have used cellTemplate to give background color to the grid cells and ui-grid-cellNav to navigate between the grid cells.
If I use ui-grid-cellNav along with cellTemplate, cell navigation is not working. If I comment out the cellTemplate code, then the cell navigation is working fine.
Where am I getting struck? Forget about the loop logic and all. I cannot place the entire code here. This is already looking clumsy.
I have used cellClass and cellStyle instead of cellTemplate. Here is my code in my controller:
for (var i = 0; i < columnNames.length; i++) {
if (i < 4) {
colDef.push(
{
field: columnNames[i],
displayName: columnNames[i],
width: '100',
// fix for highlighting first row in ui-grid
//cellTemplate: '<div ui-grid-selection class="ui-grid-cell-contents" ng-style="{\'background-color\':grid.appScope.getBackgroundColor(row,true)}">{{ COL_FIELD }}</div>',
cellClass: 'ui-grid-cell-contents',
cellStyle: { 'background-color': 'backgroundColor(row, true)' },
sortingAlgorithm: sort
});
}
else
colDef.push(
{
field: columnNames[i],
displayName: columnNames[i],
width: '100',
//cellTemplate: '<div ui-grid-selection class="ui-grid-cell-contents" ng-style="{\'background-color\':grid.appScope.getBackgroundColor(row,false)}" >{{ COL_FIELD }}</div>',
cellClass: 'ui-grid-cell-contents',
cellStyle: { 'background-color': 'backgroundColor(row, false)' },
sortingAlgorithm: sort
});
}
The above code allowed me to use both cellNav and cell custom style together.

Using Angularjs UI-Grid rowTemplate to get the plus icon in the row header

I have a group and subgroup grid using ui-grid v3.0.0-rc.20-8199eb5 - 2015-04-13. I need the plus and minus icon in the rows that contain data and not the grouping header. Right now there are none in any rows. I have been trying to use rowTemplate and have only been successful in adding another row header but not modifying the existing row header.
$scope.gridOptions = {
enableFiltering: true,
paginationPageSizes: [25, 50, 75],
paginationPageSize: 25,
onRegisterApi: function(gridApi) {
$scope.gridApi = gridApi;
var cellTemplate = '<div class="ui-grid-row-header-cell ui-grid-expandable-buttons-cell"><div class="ui-grid-cell-contents"><i ng-class="{ \'ui-grid-icon-plus-squared\' : !row.isExpanded, \'ui-grid-icon-minus-squared\' : row.isExpanded }" ng-click="grid.api.expandable.toggleRowExpansion(row.entity)"></i> </div></div>';
$scope.gridApi.core.addRowHeaderColumn({ name: 'rowHeaderCol', displayName: '', cellTemplate: cellTemplate });
},
enableRowSelection: true,
enableRowHeaderSelection: false,
enableGridMenu: true,
multiSelect: false
};
You need to inject 'ui.grid.expandable' in your module and to add 'ui-grid-expandable' to your ui-grid element like this:
<div ui-grid="gridOptions" ui-grid-expandable class="grid"></div>
Use this: http://plnkr.co/edit/0tmCvI05oVNbpfkCquCt?p=preview

UI-Bootstrap Datepicker in ng-grid not working with enableCellEditOnFocus=true

I know how to have a working UI-Bootstrap-Datepicker inside an ng-grid cell's editableCellTemplate.
My Question:
It does not work anymore when I replace the enableCellEdit=true option with enableCellEditOnFocus=true (single-click on cell to edit). Does anyone have an Idea why it breaks and how to fix that?
var editableCellTemplateUsingDatePicker = '<input ng-class="\'colt\' col.index" datepicker-popup="dd.MM.yyyy" datepicker-append-to-body=true is-open="isOpen" ng-model="COL_FIELD" my-input="COL_FIELD"/>';
$scope.gridOptions = {
data: 'myData',
//enableCellEdit: true,
enableCellEditOnFocus: true,
enableCellSelection: true,
enableRowSelection: false,
columnDefs: [{
field: 'name',
displayName: 'Name'
}, {
field:'dateOfBirth',
displayName:'Date of Birth',
editableCellTemplate: editableCellTemplateUsingDatePicker,
cellFilter: 'date:"dd.MM.yyyy"',
}]
};
Plunker
Now, I found another way to get to the desired behaviour of single-click edit with the ui-bootstrap datepicker.
You can add ng-click="editCell()" to your custom cellTemplate to enter edit-mode through single-click of the cell. Here the working setup:
var cellTemplateEditOnClick = '<div class="ngCellText" ng-click="editCell()" ng-class="col.colIndex()"><span ng-cell-text>{{row.getProperty(col.field) CUSTOM_FILTER}}</span></div>';
$scope.gridOptions = {
data: 'myData',
enableCellEdit: true,
enableCellSelection: true,
enableRowSelection: false,
columnDefs: [{
field: 'name',
displayName: 'Name',
cellTemplate: cellTemplateEditOnClick.replace('CUSTOM_FILTER', '')
}, {
field:'dateOfBirth',
displayName:'Date of Birth',
editableCellTemplate: editableCellTemplateUsingDatePicker,
cellTemplate: cellTemplateEditOnClick.replace('CUSTOM_FILTER', '| date:"dd.MM.yyyy"')
}]
};
Plunker

Resources