Introduction

This article is about how to create a simple ASP.NET Web API and implement keyboard navigation in Shield UI Grid.

Requirements

Basic knowledge of ASP.NET MVC, ASP.NET Web API, and Shield UI framework.

Process Outline

  1. Creating new ASP.NET Web API Project
  2. Creating Model class
  3. Creating the API Controller
  4. Testing the REST API using Postman
  5. Editing an existing View to implement the Shield UI Grid with the keyboard navigation using the REST API.
Creating new ASP.NET Web API Project

Open Visual Studio (in my case, I’m using VS 2015 Community) and select File -> New -> Project. In the popup window, select ASP.NET Web Application (.NET Framework), name it as you like, and click OK. I named it ShieldUIGridNavigation.


In the next window, select Web API.


Creating Model class

When the Visual Studio is ready for creating the project, head to the Solution Explorer (if It’s not shown, navigate to the top left corner -> View -> Solution Explorer) -> Right click on the “Models” folder -> Add -> Class and name it Car.cs.

Car.cs

  1. public class Car
  2. {
  3. public Car(int id, string manufacturer, string model, string color, int yearmade)
  4. {
  5. this.Id = id;
  6. this.Manufacturer = manufacturer;
  7. this.Model = model;
  8. this.Color = color;
  9. this.YearMade = yearmade;
  10. }
  11. public int Id { get; set; }
  12. public string Manufacturer { get; set; }
  13. public string Model { get; set; }
  14. public string Color { get; set; }
  15. public int YearMade { get; set; }
  16. }
Creating the API Controller

Head to the Solution Explorer -> Right click on the “Controller” folder -> Add -> Controller and select Web API 2 Controller – Empty as shown in the figure. Name it CarsController.cs.


CarsController.cs

  1. public class CarsController : ApiController
  2. {
  3. [HttpGet]
  4. public IHttpActionResult GetCars()
  5. {
  6. var carList = new List<Car>();
  7. carList.Add(new Car(1, "BMW", "M4", "Red", 2016));
  8. carList.Add(new Car(2, "Audi", "RS6", "Blue", 2015));
  9. carList.Add(new Car(3, "Mercedes", "GLE", "White", 2016));
  10. carList.Add(new Car(4, "Honda", "Civic", "Black", 2004));
  11. carList.Add(new Car(5, "Volkswagen", "Touareg", "Chameleon", 2008));
  12. carList.Add(new Car(6, "Koenigsegg", "Agera R", "Gray", 2007));
  13. carList.Add(new Car(7, "Ford", "Mustang", "Black", 2011));
  14. carList.Add(new Car(8, "Dodge", "Challenger", "Brown", 2017));
  15. return Ok(carList);
  16. }
  17. }

The GetCars() action returns a list of cars which will be filled in the Grid.

Testing the REST API using Postman

Using Postman, we will test if the API is returning the list without any errors by making an request of type GET to api/cars, as shown in the figure. If everything is fine, we should get the list in JSON format.


Editing the View to implement the Shield UI Grid with keyboard navigation using REST API.

Now that we got a working REST API, we need to include the Shield UI Javascript and CSS files in our _Layout.cshtml. The files are already included in the project so we simply need to add

  1. @Styles.Render("~/Content/shieldui.1.7.29-Trial/css/light/all.min.css")
  2. @Scripts.Render("~/Content/shieldui.1.7.29-Trial/js/jquery-1.11.1.min.js")
  3. @Scripts.Render("~/Content/shieldui.1.7.29-Trial/js/shieldui-all.min.js")

Now, we are good to go!

Navigate to Solution Explorer -> Open Views Folder -> Home -> Open Index.cshtml.
Delete everything there and add the following code to implement the Shield UI Grid.

Index.cshtml

  1. <div id="grid"></div>
  2. @section scripts {
  3. <script>
  4. jQuery(function ($) {
  5. $("#grid").shieldGrid({
  6. dataSource: {
  7. remote: {
  8. read: {
  9. url: "/api/cars",
  10. dataType: "json"
  11. }
  12. }
  13. },
  14. schema: {
  15. fields: {
  16. id: { path: "Id", type: Number },
  17. manufacturer: { path: "Manufacturer", type: String },
  18. model: { path: "Model", type: String },
  19. color: { path: "Color", type: String },
  20. yearmade: { path: "YearMade", type: Number }
  21. }
  22. },
  23. navigation: true,
  24. paging: {
  25. pageSize: 6,
  26. pageLinksCount: 8
  27. },
  28. editing: {
  29. enabled: true,
  30. type: "row"
  31. },
  32. selection: {
  33. type: "row",
  34. multiple: true,
  35. toggle: false
  36. },
  37. columns: [
  38. { field: "Id", width: "70px", title: "ID" },
  39. { field: "Manufacturer", title: "Manufacturer", width: "280px" },
  40. { field: "Model", width: "200px", title: "Car model" },
  41. { field: "Color", title: "Car Color", width: "100px" },
  42. { field: "YearMade", title: "Year Made" },
  43. {
  44. width: 150,
  45. title: " ",
  46. buttons: [
  47. { commandName: "edit", caption: "Edit" },
  48. { commandName: "delete", caption: "Delete" }
  49. ]
  50. }
  51. ],
  52. });
  53. });
  54. </script>
  55. }

By default, the keyboard navigation in the Grid is false (turned off); so we need to add the property navigation in the Grid code.

Result


There are different built-in key combinations to work with the Shield UI Grid.

The Grid also supports non built-in functionality. Just for example, the code below focuses the grid in the example by pressing Ctrl + G.

  1. $(document).keydown(function (e) {
  2. if (e.ctrlKey && e.keyCode == 71) {
  3. // focus the Grid by calling its focus() method
  4. // this should focus the first cell inside it
  5. $("#grid").swidget().focus();
  6. // do not propagate the event
  7. e.preventDefault();
  8. e.stopPropagation();
  9. return false;
  10. }
  11. });

References

  1. https://www.shieldui.com/documentation/grid
  2. https://demos.shieldui.com/web/grid-general/keyboard-navigation

Conclusion

We’ve seen how easy it is to implement the Shield UI Grid with Keyboard navigation with REST API. I hope you enjoyed this article. Feel free to give feedback, comment or question about this article anytime!