I have a ng-grid built with following JSON
[{"TankName":"Tnk1","UseFuel":"100","UnusedFuel":"200"},
{"TankName":"Tnk2","UseFuel":"150","UnusedFuel":"600"},
{"TankName":"TOTAL","UseFuel":"0","UnusedFuel":"0"}]
I have configured a NG-GRID to dispay. Grid will display as below
below is the Grid-otions
columnDefs: [
{ field: 'TankName', displayName: 'Fuel Tank', enableCellEdit: false,},
{ field: 'UseFuel', displayName: 'Use Fuel', editableCellTemplate: '<input ng-input="COL_FIELD" ng-model="COL_FIELD"/>' },
{ field: 'UnusedFuel', displayName: 'Unused Fuel', editableCellTemplate: '<input ng-input="COL_FIELD" ng-model="COL_FIELD"/>' }
]
I want to put the column totals (Bottom Row) when user edit something on the gird. I have to show them in "TOTAL" row. I cannot use FooterTemplate since its not suiting my need
Below is my code in controller
$scope.$on('ngGridEventEndCellEdit', function (data) {
var totalRow;
angular.forEach(a.gridOptions_all.ngGrid.data, function (row) {
if (row.TankName.toString().toUpperCase() != 'TOTAL') {
totalUseFuel += Number(row.UseFuel);
totalUnUseFuel += Number(row.UnusedFuel);
}
else {totalRow = row;}
});
totalRow.UseFuel= totalUseFuel ;
totalRow.UnusedFuel= totalUnUseFuel ;
});
here a plunker. Could some say whether is there a better option
Regarding the picture you placed in the question I would solve it like this:
columnDefs: [{
field: 'TankName',
displayName: 'Tank',
enableCellEdit: false,
cellEditableCondition: 'newFunc(row);'
}, {
field: 'UseFuel',
displayName: 'Ballast Fuel',
editableCellTemplate: '<input ng-input="COL_FIELD" ng-model="COL_FIELD"/>'
}, {
field: 'UnusedFuel',
displayName: 'Trapped Fuel',
cellEditableCondition: 'row.rowIndex != 2',
editableCellTemplate: '<input ng-input="COL_FIELD" ng-model="COL_FIELD"/>'
}, {
displayName: 'Total',
cellTemplate: '<div>{{getTotal(row.entity.UseFuel,row.entity.UnusedFuel)}}<div>'
}
]
};
$scope.getTotal = function(uf, uuf) {
return Number(uf) + Number(uuf);
}
Note the cellTemplate and the getTotal function.
Look at this Plunker to see this updating while you type.
Related
i have spent lots of hours finding a solution but no success every time i am finding a solution it ends up with static data not from any web API or database. i want dynamic data to be populated in drop down list in UI grid. i have read in one blog in which guy was saying for dynamic data we have to use editDropdownRowEntityOptionsArrayPath but i did not find any useful solution. any one can provide any useful information than i will be vary thankful. thanks in advance. this is what i have done.
$scope.listOptions = []; $scope.ddlist = [];
$http.get('http://localhost:26413/api/MenuVDN/GetVDNList')
.then(function (data) {
$scope.listOptions = data;
$scope.ddlist = $scope.listOptions.data.Table;
console.log($scope.ddlist);
})
$scope.gridOptions = {
enableColumnResizing: true,
enableSorting: true,
enableCellSelection: true,
canSelectRows: true,
// enableCellEdit: true,
columnDefs: [
{ field: 'NameEn', displayName: ' Menu Name', grouping: { groupPriority: 0 }, sort: { priority: 0, direction: 'asc' }, width: '25%' },
{ field: 'id', displayName: 'ID' },
{ field: 'language', displayName: 'VDN Language', grouping: { groupPriority: 1 }, sort: { priority: 1, direction: 'asc' } },
{ field: 'vdnname', displayName: 'VDN Name' },
{
field: 'vdnnum', displayName: 'VDN Number',
editableCellTemplate: 'ui-grid/dropdownEditor',
// editDropdownIdLabel: 'id',
editDropdownValueLabel: 'value',
// enableFocusedCellEdit: true,
enableCellEditOnFocus :true,
enableCellEdit: true,
editType: 'dropdown',
editDropdownRowEntityOptionsArrayPath : $scope.ddlist
// , cellEditableCondition: function( $scope ) { return true; }
}
]
};
plus i am getting response from webapi in json format like this.
{"Table":[{"id":2,"value":"AR-BOOKING-NEW (7101)"},
{"id":3,"value":"EN-BOOKIN-NEW (7102)"},
{"id":4,"value":"AR-BOOKING-CANCEL (7103)"},
{"id":5,"value":"EN-BOOKING-CANCEL (7104)"},
{"id":6,"value":"AR-BOOKING-MODIFY (7105)"}]}
$scope.columns = completedFiles.columns;
$scope.rows = completedFiles.rows;
//prepare custom column for ui-grid
var customColumns = [];
angular.forEach($scope.columns, function(column) {
customColumns.push({
field : column.fieldName,
displayName : column.displayName,
editable : column.editable,
dataType : column.dataType,
});
}
});
angular.forEach(customColumns, function(customColumn) {
customColumn['width'] = 200;
if (customColumn.dataType === 'dropDown') {
customColumn['cellTemplate'] = "<div class='ui-grid-cell-contents' id='col-description'><select class="form-control" data-ng-options="item in grid.appScope.arrayName track by item" ><option value="" selected hidden />/></select></div>";
customColumn['width'] = 180;
}
});
You can create custom template like above and after that just assign in $scope.gridOptions customColumns to columnDefs rather then defining the column definition there.
Hi i need to add two buttons into a single column..How to do it with UI-Grid (AngularJS).
Thanks in advance :)
You can use cellTemplate key in columnDefs
$scope.gridOptions = {
data: 'list',
multiSelect: false,
columnDefs: [{ field: 'name', displayName: 'Name'},
{ field: 'description', displayName: 'Description'},
{ displayName: 'Actions', cellTemplate:
'<div class="grid-action-cell">'+
'<a ng-click="$event.stopPropagation(); updateThisRow(row.entity);" href="#">Update</a>'+
'<a ng-click="$event.stopPropagation(); deleteThisRow(row.entity);" href="#">Delete</a></div>'}
]
};
I want to enable cell edit based on row index
columnDefs: [
{
field: "Name",
name: "Name",
enableCellEdit: true,
cellEditableCondition: 'row.rowIndex == 2',
cellTemplate: '<div class="ui-grid-cell-contents">{{}}</div>'
}
but above code is not working. If i pass true in cellEditableCondition it works fine. I think row.rowIndex is not giving index. I was able to get row index in cell template as below,
but if i write same code in cellEditableCondition it wont work.
field: "Name",
name: "Name",
enableCellEdit: true,
cellEditableCondition: 'grid.renderContainers.body.visibleRowCache.indexOf(row) == 2', // not working
cellTemplate: '<div class="ui-grid-cell-contents">{{grid.renderContainers.body.visibleRowCache.indexOf(row)}}</div>' //working
Please help me to get rowindex in cellEditableCondition
You should pass a function like this -
cellEditableCondition:
function($scope) {
return $scope.grid.renderContainers.body.visibleRowCache.indexOf($scope.row) == 2;
}
here's how i defined one of my col in Ui grid :
getDeviceTypeList = function () {
BaseInfoService.getDeviceTypeList().then(function (data) {
debugger;
$scope.deviceTypeList = data;
$scope.gridOptions.columnDefs.push(
{
headerCellTemplate: '<div>{{"common.DeviceType"|translate}}</div>',
cellTemplate: '<div>{{getExternalScopes().deviceTypeFormatter(row.entity.DeviceTypeCode)}}</div>',
field: 'DeviceTypeCode',
enableCellEdit: true,
editType: 'dropdown',
editDropdownOptionsArray: $scope.deviceTypeList,
editableCellTemplate: 'ui-grid/dropdownEditor',
editDropdownIdLabel: 'SubCode',
editDropdownValueLabel: 'ParamDesc',
editModelField: 'DeviceTypeCode'
}
);
})
}
but other col defined like :
$scope.gridOptions = {
columnDefs: [
{
name:"soheil"},
{ name: 'توضیحات', field: 'Command', headerCellTemplate: '<div>{{"common.Comment"|translate}}</div>', editModelField: 'Command' },
{ name: 'تعداد', field: 'NeededDeviceNumber', headerCellTemplate: '<div>{{"common.NeededDeviceNumber"|translate}}</div>', type: 'number', editModelField: 'NeededDeviceNumber' },
//{ name: 'نوع وسایل', field: 'DeviceType', headerCellTemplate: '<div>{{"common.DeviceType"|translate}}</div>', cellTemplate: '<select ng-model="neededDiviceViewModel.templateCode" ng-options="item.SubCode as item.ParamDesc for item in dastourKarViewModelList"><option value="" style="display:none" selected="selected">انتخاب</option></select>' }
],
}
how can i defined custom position for column ? Now It's like :
1 3 5 2 4
and that's not what i want , Like : 1 2 3 4
Any Idea ?
Thanks
Use the splice function to insert new column definitions at the correct place
$scope.columns.splice(1, 0, { field: 'company', enableSorting: false });
Above code adds a new column at the second position. See the example from the uioo-grid tutorial
UPDATE: For your this requirement "assume the column should be like this : col1 col2 dynamic-col col3 but now it's like : dynamic-col col1 col2 col 3" you need to use following code
$scope.gridOptions.columnDefs.splice(2, 0, { headerCellTemplate: '<div>{{"common.DeviceType"|translate}}</div>',
cellTemplate: '<div>{{getExternalScopes().deviceTypeFormatter(row.entity.DeviceTypeCode)}}</div>',
field: 'DeviceTypeCode',
enableCellEdit: true,
editType: 'dropdown',
editDropdownOptionsArray: $scope.deviceTypeList,
editableCellTemplate: 'ui-grid/dropdownEditor',
editDropdownIdLabel: 'SubCode',
editDropdownValueLabel: 'ParamDesc',
editModelField: 'DeviceTypeCode'
});
columnDef is an array and you need to just push the columns in same sequence in which you want to show. For push dynamically columns, you need to handle these cases through data and code.
first I got a Filter "fromMSDate" that I use to transform json dates not normal dates, if this filter is place then refresh my input that is bound to filterOptions.filterText I get 'Circular dependency' and 'Unknown provider: fromMSDate | dateFilterProvider <- fromMSDate '
//Module
var mainApp = angular.module('mainApp', ['ngGrid']);
//Controller
mainApp.controller('MandateListController', function MandateListController($scope) {
$scope.filterOptions = { filterText: '' };
$scope.mandates = data;
$scope.gridOptions = {
data: "mandates",
filterOptions: $scope.filterOptions,
sortInfo: { fields: ['ExpectedDate', 'ProjectName'], directions: ['desc', 'asc'], columns: ['ExpectedDate', 'ProjectName'] },
columnDefs: [
{ field: 'ProjectName', displayName: 'Project Name', width: '30%', cellClass: 'text-center' },
{ field: 'Amount', displayName: 'Size', cellFilter: 'number:2', cellClass: 'text-right' },
{ field: 'RatingId', displayName: 'Rating', cellClass: 'text-center' },
{ field: 'CurrencyId', displayName: 'Currency', cellClass: 'text-center' },
{ field: 'MaturityId', displayName: 'Maturity', cellClass: 'text-center' },
{ field: 'EstimatedPl', displayName: 'Estimated P/L', cellFilter: 'number:2', cellClass: 'text-right' },
{ field: 'ExpectedDate', displayName: 'Expected Date', cellClass: 'text-center', cellFilter: "fromMSDate | date:'mediumDate'" }
]
};
});
//filter
mainApp.filter("fromMSDate", [function () {
var result = function(date, formatstring) {
if (formatstring === null || formatstring === undefined) {
formatstring = "DD MMM YYYY";
}
return moment(date).format(formatstring);
};
return result;
}]);
If I correctly understand you somehow include into the HTML page the contents of $scope.gridOptions.columnDefs[].cellFilter. In the last columnDef you have the following filter:
fromMSDate | date:'mediumDate'
I think you expect that date will be passed as the first argument and 'mediumDate' as the second, but filters in angular.js has another syntax and you need to write this way:
date | fromMSDate:'mediumDate'
Filters are added to the expression with | character and get the prior expression as the first argument. Other arguments could be specified after :.
So in your example angular.js recognizes 'date' as filter name and fails to find DateFilter or DateFilterProvider for it.