If you want to know how to get started with UI-Grid and how to set up a project in AngularJS and Web API, read the articles given below first.

The custom scrolling feature allows you to provide your own scroller function to replace default element, On('scroll') function. It is particularly helpful if you are facing issues with scrolling on devices or slow machines, because it allows you to use third-party scrollers.

After creating all basic and adding mandatory script, let’s implement custom scrolling on ui-grid. Thus, after adding data in Entity Model, create a Web API to get the data from Entity model.

Web API

Here, my Web API class code is given below.

  1. public class EmployeesAPIController: ApiController {
  2. private NORTHWNDEntities db = new NORTHWNDEntities();
  3. // GET: api/Employees
  4. public IQueryable < Employee > GetEmployees() {
  5. return db.Employees;
  6. }
  7. // GET: api/Employees/5
  8. [ResponseType(typeof(Employee))]
  9. public async Task < IHttpActionResult > GetEmployee(int id) {
  10. Employee employee = await db.Employees.FindAsync(id);
  11. if (employee == null) {
  12. return NotFound();
  13. }
  14. return Ok(employee);
  15. }
  16. protected override void Dispose(bool disposing) {
  17. if (disposing) {
  18. db.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22. private bool EmployeeExists(int id) {
  23. return db.Employees.Count(e => e.EmployeeID == id) > 0;
  24. }
  25. }

Module
  1. var employeeapp = angular.module('employeeapp', ['ui.grid', 'ui.grid.pagination']);

Service

  1. employeeapp.service("employeeservice", function($http) {
  2. //Function to call get genre web api call
  3. this.GetEmployee = function() {
  4. var req = $http.get('api/EmployeesAPI');
  5. return req;
  6. }
  7. });

Controller
  1. //Controller
  2. employeeapp.controller("employeecontroller", function($scope, employeeservice, $filter, $window, $interval) {
  3. GetEmployee();
  4. function GetEmployee() {
  5. employeeservice.GetEmployee().then(function(result) {
  6. $scope.Employees = result.data;
  7. console.log($scope.Employees);
  8. }, function(error) {
  9. $window.alert('Oops! Something went wrong while fetching genre data.');
  10. })
  11. }
  12. //Columns
  13. $scope.columnDefs = [{
  14. name: 'photo',
  15. enableSorting: false,
  16. field: 'PhotoPath',
  17. cellTemplate: "<img width=\"50px\" ng-src=\"{{grid.getCellValue(row, col)}}\" lazy-src>",
  18. enableCellEdit: false,
  19. enableFiltering: false,
  20. width: 100
  21. }, {
  22. name: 'First Name',
  23. field: 'FirstName',
  24. headerCellClass: 'tablesorter-header-inner',
  25. enableCellEdit: true,
  26. enableFiltering: true,
  27. width: 150
  28. }, {
  29. name: 'Last Name',
  30. field: 'LastName',
  31. headerCellClass: 'tablesorter-header-inner',
  32. enableCellEdit: true,
  33. enableFiltering: true,
  34. width: 150
  35. }, {
  36. name: 'Title',
  37. field: 'Title',
  38. headerCellClass: 'tablesorter-header-inner',
  39. enableCellEdit: true,
  40. enableFiltering: false,
  41. width: 200
  42. }, {
  43. name: 'City',
  44. field: 'City',
  45. headerCellClass: 'tablesorter-header-inner',
  46. enableCellEdit: true,
  47. enableFiltering: true,
  48. width: 150
  49. }, {
  50. name: 'Country',
  51. field: 'Country',
  52. headerCellClass: 'tablesorter-header-inner',
  53. enableCellEdit: true,
  54. enableFiltering: true,
  55. width: 150
  56. }, {
  57. name: 'Notes',
  58. field: 'Notes',
  59. headerCellClass: 'tablesorter-header-inner',
  60. enableCellEdit: true,
  61. enableFiltering: false,
  62. width: 800
  63. }];
  64. //Used to bind ui-grid
  65. $scope.gridOptions = {
  66. customScroller: function myScrolling(uiGridViewport, scrollHandler) {
  67. uiGridViewport.on('scroll', function myScrollingOverride(event) {
  68. $scope.scroll.top = uiGridViewport[0].scrollTop;
  69. $scope.scroll.left = uiGridViewport[0].scrollLeft;
  70. // You should always pass the event to the callback since ui-grid needs it
  71. scrollHandler(event);
  72. });
  73. },
  74. enableFiltering: false,
  75. onRegisterApi: function(gridApi) {
  76. $scope.gridApi = gridApi;
  77. },
  78. columnDefs: $scope.columnDefs,
  79. data: 'Employees',
  80. };
  81. });
Global

Now, add a few lines in Global.asax in Application_Start event.

  1. GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
  2. .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
  3. GlobalConfiguration.Configuration.Formatters
  4. .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);

Now, add the mandatory packages given below, using NuGet Package Manager.

Bundling

Bundle the required styles and scripts.

Add the bundles given below in BundleConfig.cs

  1. bundles.Add(new StyleBundle("~/Content/css").Include(
  2. "~/Content/bootstrap.css",
  3. "~/Content/site.css",
  4. "~/Content/ui-grid.css"));
  5. bundles.Add(new ScriptBundle("~/bundles/uigrid").Include(
  6. "~/Scripts/ui-grid.min.js"));
  7. bundles.Add(new ScriptBundle("~/bundles/angular").Include(
  8. "~/Scripts/angular.min.js",
  9. "~/Angular/Services/employees.js"));

Render all the scripts and styles in _Loyout.cshtml
  1. @Styles.Render("~/Content/css")
  2. @Scripts.Render("~/bundles/modernizr")
  3. @Scripts.Render("~/bundles/jquery")
  4. @Scripts.Render("~/bundles/bootstrap")
  5. @Scripts.Render("~/bundles/angular")
  6. @Scripts.Render("~/bundles/uigrid")
  7. @RenderSection("scripts", required: false)

View
  1. @ {
  2. ViewBag.Title = "Index";
  3. Layout = "~/Views/Shared/_Layout.cshtml";
  4. } < h2 > Employees < /h2> < div ng - app = "employeeapp"
  5. ng - controller = "employeecontroller" > < div class = "row" > < label class = "col-sm-2" > Scroll Top: < /label> < div class = "col-sm-4" > {
  6. {
  7. scroll.top
  8. }
  9. } < /div> < label class = "col-sm-2" > Scroll Left: < /label> < div class = "col-sm-4" > {
  10. {
  11. scroll.left
  12. }
  13. } < /div> < /div> < div ui - grid = "gridOptions"
  14. ui - grid - pagination ui - grid - selection class = "grid"
  15. ui - grid - auto - resize > < /div> < /div> < style > .grid {
  16. width: 100 % ;
  17. height: 400 px;
  18. } < /style>

Output

Conclusion

In this article, we have seen how to implement custom scroll bar in angular ui-grid with Web API with an Entity Framework in MVC. If you have any question or comments, drop me a line in the comments section.