If I need to reuse the following controller(placed in App.js), I need to remove the view specific columnDefs and place them separately(View, html file).
Here I have ui-grid's gridOptions
set in the controller itself. Even if I manage to place that in UI(script tag), uiGridConstants is not available in the view. So how do I go about with this so I may reuse the file for multiple views.
I am pulling my hair for quite some time now, and am a newbie on angular, so please help.
(function () {
gridFactory = function ($http) {
return {
callWebApi: function () {
return $http({
method: 'GET',
url: '/api/PatientCategoryApi/PatCat',
params: this.callParams.paginationOptions,
headers: { 'Content-Type': 'application/Json' }
})
},
callParams: {}
}
};
patientCategoryController = function ($scope, $attrs, uiGridConstants, gridFactory) {
$scope.gridOptions = {
paginationPageSizes: [5, 10, 20, 25, 50],
paginationPageSize: 10,
useExternalPagination: true,
useExternalSorting: true,
enableSorting: true,
rowHeight: 30,
columnDefs: [
{ field: 'Id', sortDirectionCycle:[uiGridConstants.ASC, uiGridConstants.DESC] },
{ field: 'Code' },
{ field: 'Name' },
{ field: 'Description' },
{ field: 'ModifiedTime', cellFilter: 'date:"dd-MMM-yy h:mm:ss a"' },
{ field: 'History', enableSorting: false }
],
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
}
};
var callData = {};
var paginationOptions = {};
paginationOptions.Page = 1;
paginationOptions.Take = 10;
paginationOptions.SortOrder = 'Asc';
paginationOptions.PropName = 'Id';
callData.paginationOptions = paginationOptions;
gridFactory.callParams = callData;
var promise = gridFactory.callWebApi();
promise.then(
function successCallback(response) {
$scope.gridOptions.totalItems = response.data.TotalCount;
$scope.gridOptions.data = response.data.Collection;
$scope.gridHeight = gridFactory.getGridHeight($scope.gridOptions);
}, function errorCallback(response) {
alert('Some problem while fetching data!!');
});
}
patientCategoryController.$inject = ['$scope', '$attrs', 'uiGridConstants', 'gridFactory'];
gridFactory.$inject = ['$http'];
angular.module('abvhHisApp', ['ui.grid', 'ui.grid.autoResize', 'ui.grid.pagination', 'ui.grid.resizeColumns']);
angular.module('abvhHisApp').controller('patientCategoryController', patientCategoryController);
angular.module('abvhHisApp').factory('gridFactory', gridFactory);
}());