Angularjs ng-class with ng-grid / swapping css style - angularjs

I have this very simple angular app using ng-grid. I for some reason don't know how to apply style based on data. I need to be able to display different status image based on data status Code below:
controller:
$scope.myData2 = [
{service: "aaa", status: "ok"},
{service: "ccc", status: "warning"}];
$scope.gridOptions2 = {
data: 'myData2',
headerRowHeight:0,
rowHeight: 39,
enableRowSelection: false,
enableHighlighting:true,
columnDefs: [
{field: 'service', enableCellEdit: true},
{field:'status', cellTemplate : 'stateTemplate.html'}]
};
directive:
angular.module('myApp')
.directive("statusIcon", function() {
return {
restrict: 'E',
template: "<div ng-class=status-{{row.getProperty('status')}}></div>"
};
});
template:
<status-icon></status-icon>
css:
.status-ok{
width: 24px;
height: 24px;
background: url("../images/status_ok.png") no-repeat;
}
.status-warning{
width: 24px;
height: 24px;
background: url("../images/status_ok.png") no-repeat;
}
when I render the page this is the output:
<status-icon class="ng-scope"><div ng-class="status-ok"></div></status-icon>
I'm missing a classic class="status-ok"
What am I doing wrong?
PLUNKR http://plnkr.co/edit/4xTC4pKIR0AnzD89iIp2?p=preview

have a look at cell templating article here.
columnDefs: [{ field: 'firstName', displayName: 'First Name', width: 90, cellTemplate: '<div>{{row.entity[col.field]}}</div>' },
{ field: 'lastName', displayName: 'Last Name', width: 80 },
{ field: 'age', cellClass: 'ageCell', headerClass: 'ageHeader' } ]
you can define cellClass or headerClass or completely different cellTemplate
EDIT
below is example on how to use ng-class with cell template
<div ng-class="row.entity[col.field] == 'success' ? 'successClass' : 'errorClass'">{{row.entity[col.field]}}</div>
EDIT
working plunker
you dont need ng-class you just need class

Related

Read the sum value from the column footer in UI - Grid?

I'm currently trying to read my column footer to make sure that the total sum from the aggregate is exactly 100. However I can not find a way to get that value from the grid functions. Maybe I missed a function in the docs. Is there a way to accomplish this or will I have to manually loop through my grid and sum the values?
You can use aggregates on the column footer to automatically show the sum of all the values in the column.
you can have your grid config like below
$scope.gridOptions = {
showGridFooter: true,
showColumnFooter: true,
enableFiltering: true,
columnDefs: [
{ field: 'name', width: '13%' },
{ field: 'address.street',aggregationType: uiGridConstants.aggregationTypes.sum, width: '13%' },
{ field: 'age', aggregationType: uiGridConstants.aggregationTypes.avg, aggregationHideLabel: true, width: '13%' },
{ name: 'ageMin', field: 'age', aggregationType: uiGridConstants.aggregationTypes.min, width: '13%', displayName: 'Age for min' },
{ name: 'ageMax', field: 'age', aggregationType: uiGridConstants.aggregationTypes.max, width: '13%', displayName: 'Age for max' },
{ name: 'customCellTemplate', field: 'age', width: '14%', footerCellTemplate: '<div class="ui-grid-cell-contents" style="background-color: Red;color: White">custom template</div>' },
{ name: 'registered', field: 'registered', width: '20%', cellFilter: 'date', footerCellFilter: 'date', aggregationType: uiGridConstants.aggregationTypes.max }
],
data: data,
onRegisterApi: function(gridApi) {
$scope.gridApi = gridApi;
}
};
http://plnkr.co/edit/nv1eJAIyD5xNDn8XI6L9?p=preview
To get the value from the footer, provided you know the column index and have a reference to the gridApi.
$scope.getFooterValue = function()
{
console.log($scope.gridApi);
alert($scope.gridApi.grid.columns[2].getAggregationValue());
}

ui-grid Data display issue

I'm using ui-grid with my Cordova application. When I try to populate the ui-grid, sometimes data is displayed on the left like in the picture below:
Any help?
HTML
<div ui-grid="{data: gridOptions, columnDefs: gridColumns, paginationPageSize: 10, enableColumnMenus: false, enableHorizontalScrollbar : 0,
enableVerticalScrollbar : 0}" ui-grid-auto-resize ui-grid-pagination class="grid_transmiss"> </div>
JS
$scope.gridColumns = [{
field: 'ref',
displayName: 'Référence'
}, {
field: 'Emq',
displayName: 'Nombre de plots empilés'
}, {
field: 'charge',
displayName: 'Charge nominale (daN)'
}, {
field: 'fp',
displayName: 'Fréquence propre(Hz)'
}, {
field: 'attenuation',
displayName: 'Atténuation(%)'
}, {
field: 'flechereel',
displayName: 'Flèche réelle statique (mm)'
}, {
name: 'Courbe',
displayName: 'Courbe',
cellTemplate: '<i ng-click="grid.appScope.goToChart()"><img src="img/chart.png" style="width=20px;height:20px" alt="Voir courbe" /></i>'
}];
Try defining your grid in your controller like this:
$scope.gridOptions = {
columnDefs: [
{
field: 'ref', displayName: 'Référence', width: "*"
},
{
field: 'Emq', displayName: 'Nombre de plots empilés', width: "*"
},
{
field: 'charge', width: 110, displayName: 'Charge nominale (daN)'
},
{ field: 'fp', displayName: 'Fréquence propre(Hz)', width: "*"
},
{
field: 'attenuation', displayName: 'Atténuation(%)', width: "*"
},
{
field: 'flechereel', displayName: 'Flèche réelle statique (mm', width: "*"
},
{
field: 'Courbe', displayName: 'Release Courbe', width: "*",
cellTemplate: '<i ng-click="grid.appScope.goToChart()"><img src="img/chart.png" style="width=20px;height:20px" alt="Voir courbe" /></i>'
},
],
showGridFooter: true,
enableFiltering: true,
enableSorting: false,
enableColumnMenus: false,
paginationPageSizes: [100, 120, 140],
paginationPageSize: 100,
enableHorizontalScrollbar: uiGridConstants.scrollbars.NEVER,
enableGridMenu: false,
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
}
};
Then in your HTML:
<div ui-grid="gridOptions" class="grid" ui-grid-pagination ui-grid-exporter ui-grid-auto-resize></div>
Also, make sure you include 'uiGridConstants' in our controller definition like so:
ContractorCtrl.$inject = ['$scope', '$interval', '$window', '$filter', 'uiGridConstants', '$q', '$timeout'];
function ContractorCtrl($scope, $interval, $window, $filter, uiGridConstants, $q, $timeout)
Let me know if this solves your issue.
change your column definition according to below format. It will work.
$scope.gridOptions = {
columnDefs: [
{name:'clumnName', fields: 'DisplayValue', width: '20%'},
{name:'clumnName', fields: 'DisplayValue', width: '20%'},
{name:'clumnName', fields: 'DisplayValue', width: '20%'},
],
data: $scope.DisplayDataSet,
}

ng-grid wrap-text rows with vertical scrolling

I have a ng-grid that I am trying create a wrap-text column for a column that contains multiple lines of comment text. I have been trying everything today that I can find on the internet and still can't find a solution. I want to show you my code and if someone can tell me a better way of doing this that would be a great help.
var vm = this;
$scope.gridOptions = {
data: 'myData',
enablePaging: true,
pagingOptions: $scope.GridPagingOptions,
sortInfo: {
fields: [],
columns: [],
directions: []
},
useExternalSorting: false,
showFooter: false,
columnDefs: [{
field: 'RequestNumber',
width: 90,
displayName: 'RequestID'
}, {
field: 'ActionByName',
width: 160,
displayName: 'Action }, { field: '
ActionDate ', width: 90, displayName: Action Date',
cellFitlter: "date:'yyyy-MM-dd'"
}, {
field: 'Comments',
width: 125,
displayName: 'Comments'
}, {
field: 'ApproverComments',
displayName: 'Approver Comments',
rowHeight: 25,
width: 215,
cellClass: 'wrap-text',
cellTemplate: '<label id="approverC" name="approverC" title="{{ row.getProperty(col.field)}}">{{ row.getProperty(col.field)}}</label>'
}]
};
.gridStyle4 {
border: 1px solid #dfdfdf;
overflow-y: scroll;
}
<div style="margin-top:100px;">
<hr style="height:1px; background-color: silver; border:0px; margin-top:0px; margin-bottom:10px;" />
<div class="gridStyle4" ng-grid="gridOptions" />
</div>

Angular ui-grid: define columns in template

The ui-grid documentation tells us to define columns in the controller, like this:
$scope.gridOptions = {
columnDefs: [
{ field: 'id', displayName: 'Ref', width: "60" },
{ field: 'title', displayName: 'Title', width: "200" }
]
}
While this works fine, I feel that this really belongs in my template. Is there some way to setup the columns like this:
<div ui-grid="gridOptions" class="myGrid" style="width:800px; height: 600px">
<ui-column field="id" width="60">Ref</ui-column>
<ui-column field="title" width="200">Title</ui-column>
</div>

Show/Hide/Disable A 'Remove' Button On A NG-Grid

I have an angularjs application which has a data-ng-grid. This table automatically displays 1 row by default and the last column displays a 'Remove' button but as this table has to have 1 or more entries in it I want to remove/disable this button when only one row is present in it.
I have tried adding ng-if="propertyIncomeGrid.count > 1" but when I click my add button it doesn't display on any row.
Please note that the user can delete any row so the button needs to be displayed as soon as the table row are > 1 and removed/disabled when <= 1.
Code
$scope.propertyIncomeGrid = {
data: 'propertyIncomeData',
enableColumnReordering: false,
rowHeight: 40,
multiSelect: false,
showColumnMenu: false,
showFooter: false,
enableColumnResize: true,
filterOptions: $scope.filterOptions,
selectedItems: $scope.mySelections,
enablePaging: false,
plugins: [gridLayoutPlugin],
columnDefs:
[
{ field: 'PropertyTransactionType', displayName: 'Income type', width: '18%', enableCellEdit: false, cellTemplate: incomeDropdownTemplate },
{ field: 'leaseId', displayName: 'Property ID and lease name', width: '30%', enableCellEdit: false, cellTemplate: leaseDropdownTemplate },
{ field: 'propertyId', displayName: 'propertyId', width: '0', editableCellTemplate: '' },
{ field: 'netAmount', displayName: 'Net (£)', width: '14%', cellTemplate: AmountCellTemplate },
{ field: 'vatAmount', displayName: 'VAT (£)', width: '14%', cellTemplate: AmountCellTemplate },
{ field: 'GROSSAmount', displayName: 'Gross (£)', width: '14%', cellTemplate: AmountCellTemplate },
{
field: 'removeBtn', displayName: 'Actions',
enableCellEdit: false,
cellTemplate: '<button class="btn btn-warning ng-if="propertyIncomeGrid.count > 1" ng-click="propertyIncomeRemoveRow(row.entity)" style="margin-top: 3px; margin-right: 0px; margin-left: 0px;">Remove</button>',
width: '10%'
},
]
};
I have tried debugging and it only hits when my modal loads and comes back as undefined.
Added the below to my button and works like magic:
ng-disabled="propertyIncomeData != undefined && propertyIncomeData.length < 2"
Full button code
cellTemplate: '<button class="btn btn-warning" ng-disabled="propertyIncomeData != undefined && propertyIncomeData.length < 2" ng-click="propertyIncomeRemoveRow(row.entity)" style="margin-top: 3px; margin-right: 0px; margin-left: 0px;">Remove</button>',

Resources