@page @model NorthwindRazor.Pages.Products_ListByCategoryIDModel @{ ViewData["Title"] = "List of Products By Categories"; } @section AdditionalCss { <link rel="stylesheet" href="~/css/ui.jqgrid.min.css" /> } @section AdditionalJavaScript { <script src="~/js/jqgrid-i18n/grid.locale-en.min.js" asp-append-version="true"></script> <script src="~/js/jquery-jqgrid-4.13.2.min.js" asp-append-version="true"></script> <script type="text/javascript"> // formats the SupplierID in the jqgrid as a hyperlink // so that the link redirects to the record details page when clicked function supplierIDLink(cellvalue, options, rowObject) { return "<a href='/Suppliers/Suppliers_Details?id=" + cellvalue + "&returnUrl=/Products/Products_ListByCategoryID'>" + cellvalue + "</a>"; } // formats the CategoryID in the jqgrid as a hyperlink // so that the link redirects to the record details page when clicked function categoryIDLink(cellvalue, options, rowObject) { return "<a href='/Categories/Categories_Details?id=" + cellvalue + "&returnUrl=/Products/Products_ListByCategoryID'>" + cellvalue + "</a>"; } // used by the selCategoryID select tag. // when selection changes, new data is loaded from the database // based on the selected CategoryID and resets the page number to 1 function selectProductsByCategoryID(selectObject) { $('#list-grid').jqGrid('setGridParam',{ url: '/Products/Products_ListByCategoryID?handler=GridDataByCategoryID&categoryID=' + selectObject.value }).trigger('reloadGrid', [{ page: 1 }]); } $(function () { $('#list-grid').jqGrid({ url: '/Products/Products_ListBycategoryID?handler=GridDataByCategoryID&categoryID=' + $('#selCategoryID').val(), datatype: 'json', mtype: 'GET', colNames: ['Product ID','Product Name','Supplier ID','Category ID','Quantity Per Unit','Unit Price','Units In Stock','Units On Order','Reorder Level','Discontinued'], colModel: [ { name: 'ProductID', index: 'ProductID', align: 'right' }, { name: 'ProductName', index: 'ProductName', align: 'left' }, { name: 'SupplierID', index: 'SupplierID', align: 'right', formatter: supplierIDLink }, { name: 'CategoryID', index: 'CategoryID', align: 'right', formatter: categoryIDLink }, { name: 'QuantityPerUnit', index: 'QuantityPerUnit', align: 'left' }, { name: 'UnitPrice', index: 'UnitPrice', align: 'right', formatter: 'currency', formatoptions: { decimalPlaces: 2, prefix: "$"} }, { name: 'UnitsInStock', index: 'UnitsInStock', align: 'right', formatter: 'integer' }, { name: 'UnitsOnOrder', index: 'UnitsOnOrder', align: 'right', formatter: 'integer' }, { name: 'ReorderLevel', index: 'ReorderLevel', align: 'right', formatter: 'integer' }, { name: 'Discontinued', index: 'Discontinued', align: 'center', formatter: 'checkbox' }, ], pager: $('#list-pager'), rowNum: 10, pageable: true, jsonReader: { page: "d.page" }, rowList: [5, 10, 20, 50], sortname: 'ProductID', sortorder: "asc", viewrecords: true, caption: 'List of Products By Categories', height: '100%', width: '1200' }); }); // rename the page parameter to _page because asp.net core razor's page model // does not recognize the page parameter when passed $.extend(jQuery.jgrid.defaults, { prmNames: { page: "_page" } }); </script> } <h2>@ViewData["Title"]</h2> <br /><br /> Categories: <select id="selCategoryID" asp-for="ProductsModel.CategoryID" asp-items="@(new SelectList(Model.CategoriesDropDownListData, "CategoryID", "CategoryName"))" onchange="selectProductsByCategoryID(this); return false; "><option value="">Null (no value)</option></select> <br /><br /> <table id="list-grid"></table> <div id="list-pager"></div>