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.
@{ViewBag.Title = "Index";}<h2>Invoices</h2><table id="jqg"></table><div id="jpager"></div><script type="text/javascript">/*** The code to work with jqGrid*/</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.).
// invoice gridvar dbGrid = $("#jqg").jqGrid({url: '@Url.Action("GetData")', URL to retrieve datadatatype: "json", // format datamtype: "GET", // type of http request// model descriptioncolModel: [{label: 'Id',name: 'INVOICE_ID',key: true,hidden: true},{label: 'CUSTOMER_ID',name: 'CUSTOMER_ID',hidden: true,editrules: { edithidden: true, required: true },editable: true,edittype:'custom', // own typeeditoptions: {custom_element: function (value, options) {// add hidden inputreturn $("<input>").attr('type', 'hidden').attr('rowid', options.rowId).addClass("FormElement").addClass("form-control").val(value).get(0);}}},{label: 'Date',name: 'INVOICE_DATE',width: 60,sortable: true,editable: true,search: true,edittype: "text", // type of inputalign: "right",formatter: 'date', // formatted as datesorttype: 'date', // sorted as dateformatoptions: { // date formatsrcformat: 'd.m.Y H:i:s',newformat: 'd.m.Y H:i:s'},editoptions: {// initializing the form element for editingdataInit: function (element) {// create datepicker$(element).datepicker({id: 'invoiceDate_datePicker',dateFormat: 'dd.mm.yy',minDate: new Date(2000, 0, 1),maxDate: new Date(2030, 0, 1)});}},searchoptions: {// initializing the form element for searchingdataInit: function (element) {// create datepicker$(element).datepicker({id: 'invoiceDate_datePicker',dateFormat: 'dd.mm.yy',minDate: new Date(2000, 0, 1),maxDate: new Date(2030, 0, 1)});},searchoptions: { // searching typessopt: ['eq', 'lt', 'le', 'gt', 'ge']},}},{label: 'Customer',name: 'CUSTOMER_NAME',width: 250,editable: true,edittype: "text",editoptions: {size: 50,maxlength: 60,readonly: true},editrules: { required: true },search: true,searchoptions: {sopt: ['eq', 'bw', 'cn']},},{label: 'Amount',name: 'TOTAL_SALE',width: 60,sortable: false,editable: false,search: false,align: "right",formatter: 'currency', // format as currencysorttype: 'number',searchrules: {"required": true,"number": true,"minValue": 0}},{label: 'Paid',name: 'PAID',width: 30,sortable: false,editable: true,search: true,searchoptions: {sopt: ['eq']},edittype: "checkbox",formatter: "checkbox",stype: "checkbox",align: "center",editoptions: {value: "1",offval: "0"}}],rowNum: 500, // number of rows displayedloadonce: false,sortname: 'INVOICE_DATE', // sort by default by NAME columnsortorder: "desc",width: window.innerWidth - 80, // grid widthheight: 500, // grid heightviewrecords: true, // display the number of recordscaption: "Invoices", // grid captionpager: '#jpager', // pagination elementsubGrid: true, // show subgrid// javascript function for displaying the parent gridsubGridRowExpanded: showChildGrid,subGridOptions: {// upload data only oncereloadOnExpand: false,// load the subgrid rows only when you click on the icon "+"selectOnExpand: true},});// display the navigation bardbGrid.jqGrid('navGrid', '#jpager',{search: true,add: true,edit: true,del: true,view: false,refresh: true,searchtext: "Search",addtext: "Add",edittext: "Edit",deltext: "Delete",viewtext: "View",viewtitle: "Selected record",refreshtext: "Refresh"},update("edit"),update("add"),update("del"));
We’ll add one more “custom” button to the main grid, for paying the invoice.
// Add a button to pay the invoicedbGrid.navButtonAdd('#jpager',{buttonicon: "glyphicon-usd",title: "Pay",caption: "Pay",position: "last",onClickButton: function () {// get the current record IDvar id = dbGrid.getGridParam("selrow");if (id) {var url = '@Url.Action("Pay")';$.ajax({url: url,type: 'POST',data: { id: id },success: function (data) {// check if an error has occurredif (data.hasOwnProperty("error")) {alertDialog('Error', data.error);}else {// refresh grid$("#jqg").jqGrid('setGridParam',{datatype: 'json'}).trigger('reloadGrid');}}});}}});
