5.10.2. Views for Invoices

As with the Customer controller, only one view, View/Invoice/Index.cshtml is needed. The others can be deleted from this folder. The layout of the view is very simple, but the JavaScript code is quite extensive. We will examine the js code piece-by-piece.

  1. @{
  2. ViewBag.Title = "Index";
  3. }
  4. <h2>Invoices</h2>
  5. <table id="jqg"></table>
  6. <div id="jpager"></div>
  7. <script type="text/javascript">
  8. /**
  9. * The code to work with jqGrid
  10. */
  11. </script>

To begin with, we will take the code for working with the main grid. All we have to write into it is the properties of the model (field types and sizes, search, sorting, visibility parameters, etc.).

  1. // invoice grid
  2. var dbGrid = $("#jqg").jqGrid({
  3. url: '@Url.Action("GetData")', URL to retrieve data
  4. datatype: "json", // format data
  5. mtype: "GET", // type of http request
  6. // model description
  7. colModel: [
  8. {
  9. label: 'Id',
  10. name: 'INVOICE_ID',
  11. key: true,
  12. hidden: true
  13. },
  14. {
  15. label: 'CUSTOMER_ID',
  16. name: 'CUSTOMER_ID',
  17. hidden: true,
  18. editrules: { edithidden: true, required: true },
  19. editable: true,
  20. edittype:'custom', // own type
  21. editoptions: {
  22. custom_element: function (value, options) {
  23. // add hidden input
  24. return $("<input>")
  25. .attr('type', 'hidden')
  26. .attr('rowid', options.rowId)
  27. .addClass("FormElement")
  28. .addClass("form-control")
  29. .val(value)
  30. .get(0);
  31. }
  32. }
  33. },
  34. {
  35. label: 'Date',
  36. name: 'INVOICE_DATE',
  37. width: 60,
  38. sortable: true,
  39. editable: true,
  40. search: true,
  41. edittype: "text", // type of input
  42. align: "right",
  43. formatter: 'date', // formatted as date
  44. sorttype: 'date', // sorted as date
  45. formatoptions: { // date format
  46. srcformat: 'd.m.Y H:i:s',
  47. newformat: 'd.m.Y H:i:s'
  48. },
  49. editoptions: {
  50. // initializing the form element for editing
  51. dataInit: function (element) {
  52. // create datepicker
  53. $(element).datepicker({
  54. id: 'invoiceDate_datePicker',
  55. dateFormat: 'dd.mm.yy',
  56. minDate: new Date(2000, 0, 1),
  57. maxDate: new Date(2030, 0, 1)
  58. });
  59. }
  60. },
  61. searchoptions: {
  62. // initializing the form element for searching
  63. dataInit: function (element) {
  64. // create datepicker
  65. $(element).datepicker({
  66. id: 'invoiceDate_datePicker',
  67. dateFormat: 'dd.mm.yy',
  68. minDate: new Date(2000, 0, 1),
  69. maxDate: new Date(2030, 0, 1)
  70. });
  71. },
  72. searchoptions: { // searching types
  73. sopt: ['eq', 'lt', 'le', 'gt', 'ge']
  74. },
  75. }
  76. },
  77. {
  78. label: 'Customer',
  79. name: 'CUSTOMER_NAME',
  80. width: 250,
  81. editable: true,
  82. edittype: "text",
  83. editoptions: {
  84. size: 50,
  85. maxlength: 60,
  86. readonly: true
  87. },
  88. editrules: { required: true },
  89. search: true,
  90. searchoptions: {
  91. sopt: ['eq', 'bw', 'cn']
  92. },
  93. },
  94. {
  95. label: 'Amount',
  96. name: 'TOTAL_SALE',
  97. width: 60,
  98. sortable: false,
  99. editable: false,
  100. search: false,
  101. align: "right",
  102. formatter: 'currency', // format as currency
  103. sorttype: 'number',
  104. searchrules: {
  105. "required": true,
  106. "number": true,
  107. "minValue": 0
  108. }
  109. },
  110. {
  111. label: 'Paid',
  112. name: 'PAID',
  113. width: 30,
  114. sortable: false,
  115. editable: true,
  116. search: true,
  117. searchoptions: {
  118. sopt: ['eq']
  119. },
  120. edittype: "checkbox",
  121. formatter: "checkbox",
  122. stype: "checkbox",
  123. align: "center",
  124. editoptions: {
  125. value: "1",
  126. offval: "0"
  127. }
  128. }
  129. ],
  130. rowNum: 500, // number of rows displayed
  131. loadonce: false,
  132. sortname: 'INVOICE_DATE', // sort by default by NAME column
  133. sortorder: "desc",
  134. width: window.innerWidth - 80, // grid width
  135. height: 500, // grid height
  136. viewrecords: true, // display the number of records
  137. caption: "Invoices", // grid caption
  138. pager: '#jpager', // pagination element
  139. subGrid: true, // show subgrid
  140. // javascript function for displaying the parent grid
  141. subGridRowExpanded: showChildGrid,
  142. subGridOptions: {
  143. // upload data only once
  144. reloadOnExpand: false,
  145. // load the subgrid rows only when you click on the icon "+"
  146. selectOnExpand: true
  147. },
  148. });
  149. // display the navigation bar
  150. dbGrid.jqGrid('navGrid', '#jpager',
  151. {
  152. search: true,
  153. add: true,
  154. edit: true,
  155. del: true,
  156. view: false,
  157. refresh: true,
  158. searchtext: "Search",
  159. addtext: "Add",
  160. edittext: "Edit",
  161. deltext: "Delete",
  162. viewtext: "View",
  163. viewtitle: "Selected record",
  164. refreshtext: "Refresh"
  165. },
  166. update("edit"),
  167. update("add"),
  168. update("del")
  169. );

We’ll add one more “custom” button to the main grid, for paying the invoice.

  1. // Add a button to pay the invoice
  2. dbGrid.navButtonAdd('#jpager',
  3. {
  4. buttonicon: "glyphicon-usd",
  5. title: "Pay",
  6. caption: "Pay",
  7. position: "last",
  8. onClickButton: function () {
  9. // get the current record ID
  10. var id = dbGrid.getGridParam("selrow");
  11. if (id) {
  12. var url = '@Url.Action("Pay")';
  13. $.ajax({
  14. url: url,
  15. type: 'POST',
  16. data: { id: id },
  17. success: function (data) {
  18. // check if an error has occurred
  19. if (data.hasOwnProperty("error")) {
  20. alertDialog('Error', data.error);
  21. }
  22. else {
  23. // refresh grid
  24. $("#jqg").jqGrid(
  25. 'setGridParam',
  26. {
  27. datatype: 'json'
  28. }
  29. ).trigger('reloadGrid');
  30. }
  31. }
  32. });
  33. }
  34. }
  35. });