0

The main grid loads fine but When I click on the plus icon the subgrid data does not load, its says loading.... But nothing happens. Don't know what I'm missing. Please help.

Below is the code I'm using:

Controller

namespace SubGrid.Controllers
{
public class HomeController : Controller
{
   public ActionResult Index()
    {
         return View("Index");
    }
    RoopaDBEntities db = new RoopaDBEntities();
    public JsonResult GetDepts(string sidx, string sord, int page, int rows, string filter)
    {
        int pageIndex = Convert.ToInt32(page) - 1;
        int pageSize = rows;
        var DeptsResults = db.Table_Dept.Select(
            a => new
            {
                a.DeptID,
                a.DeptName,
                a.HOD,
                a.Status,
            });
        int totalRecords = DeptsResults.Count();
        var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
        if (sord.ToUpper() == "Desc")
        {
            DeptsResults = DeptsResults.OrderByDescending(s => s.DeptID);
            DeptsResults = DeptsResults.Skip(pageIndex * pageSize).Take(pageSize);
        }
        else
        {
            DeptsResults = DeptsResults.OrderBy(s => s.DeptID);
            DeptsResults = DeptsResults.Skip(pageIndex * pageSize).Take(pageSize);
        }
        var jsonData = new
        {
            total = totalPages,
            page,
            records = totalRecords,
            rows = DeptsResults
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }
    public JsonResult GetEmps(string sidx, string sord, int page, int rows, string filter)
    {
        int pageIndex = Convert.ToInt32(page) - 1;
        int pageSize = rows;
        var EmpsResults = db.Table_Emp.Select(
            a => new
            {
                a.DeptID,
                a.EmpID,
                a.EmpName,
                a.Gender,
                a.Salary,
            });

        int totalRecords = EmpsResults.Count();
        var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
        if (sord.ToUpper() == "Desc")
        {
            EmpsResults = EmpsResults.OrderByDescending(s => s.EmpID);
            EmpsResults = EmpsResults.Skip(pageIndex * pageSize).Take(pageSize);
        }
        else
        {
            EmpsResults = EmpsResults.OrderBy(s => s.EmpID);
            EmpsResults = EmpsResults.Skip(pageIndex * pageSize).Take(pageSize);
        }
        var jsonData = new
        {
            total = totalPages,
            records = totalRecords,
            rows = EmpsResults
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }
}
}    

JavaScript

 $(function () {
 $("#ListGrid").jqGrid({
    url: "../Home/GetDepts/",
    datatype: 'json',
    mtype: 'Get',
    colNames: ['DeptID', 'DeptName', 'HOD', 'Status'],
    colModel: [
        { key: true, name: 'DeptID', index: 'DeptID' },
        { key: false, name: 'DeptName', index: 'DeptName' },
        { key: false, name: 'HOD', index: 'HOD' },
        { key: false, name: 'Status', index: 'Status' }
    ],
    pager: jQuery('#pager'),
    rowNum: 10,
    rowList: [10, 20, 30, 40],
    height: 225,
    sortname: 'DeptID',
    sortorder: "desc",
    viewrecords: true,
    loadonce: true,
    ignoreCase: true,
    caption: "Department List",
    autowidth: true,
    multiselect: false,
    emptyrecords: 'No records to display',
    jsonReader: {
        root: "rows",
        page: "page",
        total: "total",
        records: "records",
        repeatitems: false,
        id: "0",
    },

    subGrid: true,
     subGridOptions: {
        "plusicon": "ui-icon-triangle-1-e",
        "minusicon": "ui-icon-triangle-1-s",
        "openicon": "ui-icon-arrowreturn-1-e",
         },
    subGridRowExpanded: function (subgrid_Id, rowid) {
        var $subgrid = $("<table id='" + subgrid_Id + "_t'></table>");
        $("#" + subgrid_Id)
            .append($subgrid);
        $subgrid.jqGrid({
            url: '../Home/GetEmps/' + '?id=' + rowid,
            postData: {
                q: 2,
                id: rowid,
                DeptID: rowid
            },
            datatype: "json",
            mtype: 'Get',
            colNames: ['Dept ID', 'Emp ID', 'Emp Name', 'Gender','Salary'],
            colModel: [
                { index: 'DeptID', name: 'DeptID' },
                { index: 'EmpID', name: 'EmpID', key: true },
                { index: 'EmpName', name: 'EmpName'},
                { index: 'Gender', name: 'Gender' },
                { index: 'Salary', name: 'Salary' }
            ],
            viewrecords: true,
            height: '100%',
            autowidth: true,
            rowNum: 5,
            idPrefix: rowid + "_",
          })
    },                   
        loadComplete: function () {
            $("tr.jqgrow:odd").css("background", "LightBlue");
        }
})

.navGrid('#pager', { edit: true, add: true, del: true, search: true, refresh: true })

}); 

Index:

     @model SubGrid.Models.Dept
    @{
    ViewBag.Title = "Index";
     }
    <div>
    <table>
         <tr>
             <td    width="100%"><tableid="ListGrid"></table></td>                           

            <td><div id="pager"></div></td>   
        </tr>

    </table>
</div>

<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<link href="~/Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet" />

<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/i18n/grid.locale-en.js"></script>
<script src="~/Scripts/Dept.js"></script>

<script src="~/Scripts/jquery.jqGrid.min.js"></script>
<script src="~/Scripts/grid.subgrid.js"></script>
<script src="~/Scripts/jquery.jqGrid.js"></script>
Roopa
  • 5
  • 2
  • Which version of jqGrid you use and from which fork of jqGrid ([free jqGrid](https://github.com/free-jqgrid/jqGrid), commercial [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old jqGrid in version <=4.7)? You can simplify your server code if you use `loadonce: true`: `GetDepts` for example can just return `DeptsResults` or `DeptsResults` in `GetEmps`. The method `GetEmps` don't have `id` parameter, which you send to the server. Moreover other parameters, which you set via `postData` (`q`, `DeptID` and once more `id` will be ignored), are not used too by `GetEmps`. – Oleg Jan 05 '17 at 14:59
  • Your HTML code contains additional errors. One should include one JavaScript library *only once*. Instead of that you included `jquery.jqGrid.min.js`, then `grid.subgrid.js` and finally `jquery.jqGrid.js`. You should use only minimized version `jquery.jqGrid.min.js` or non-minimized version (like `jquery.jqGrid.js`), but not a mix from both. – Oleg Jan 05 '17 at 15:02
  • Thanks @Oleg ! I use the old jqGrid version 4.4.4. We want to keep loadonce: true as we need serverside sorting, paging, filtering. I have fixed the javascript reference and am using only jquery.jqGrid.js and grid.subgrid.js now. How do I add id parameter to the `GetEmps` method? – Roopa Jan 05 '17 at 17:50
  • If you want to keep `loadonce: true` then the server *have to* return **all items at once** instead of only one page of data. jqGrid makes *only one* request to the server if you use `loadonce: true` option. After that it changes `datatype` to `"local"` and work with previously loaded data. The server should just sort the data once based on `indx` and `sortd` (by sortname: 'DeptID', sortorder: "desc" in your case). Thus you should reduce your server code to `return Json(DeptsResults.OrderByDescending(s => s.DeptID), JsonRequestBehavior.AllowGet);`. You should add `gridview: true` to both grids – Oleg Jan 05 '17 at 20:32
  • I'm not sure what you mean with "How do I add id parameter to the GetEmps method?". You should just modify the code of `GetEmps` method from `public JsonResult GetEmps(string sidx, string sord, int page, int rows, string filter)` to at least `public JsonResult GetEmps(string sidx, string sord, int page, int rows, string filter, **string id**)`. I recommend you to use `loadonce: true` if you need to load less as 1000-10000 rows of data. You should modify `var EmpsResults = db.Table_Emp.Select(...)` to add filtering by `id` (which is `DeptID`). – Oleg Jan 05 '17 at 20:36
  • The usage of retro version 4.4.4 today is very bad idea. If you loaded NuGet package [jQuery.jqGrid](https://www.nuget.org/packages/jQuery.jqGrid/) then you should uninstall it and install the current (4.13.6) version of [free-jqGrid](https://www.nuget.org/packages/free-jqGrid/) package. jqGrid 4.4.4 is 4 years old. It's developed at the time of Chrome 24, Firefox 18, IE 10. Do you really believe that it should work good in the current Chrome 55, Firefox 50, Edge 38 and IE 11? Moreover you can improve the look of the grid by usage [Font Awesome 4.x](http://fontawesome.io/). – Oleg Jan 05 '17 at 20:40
  • You can read more about the usage of Font Awesome [here](http://free-jqgrid.github.io/getting-started/index.html#type_of_data) and in [the wiki article](https://github.com/free-jqgrid/jqGrid/wiki/Using-Font-Awesome-in-free-jqGrid-4.8). I recommend you additionally to trace the traffic during loading the JSON data of subgrid. You can use Network tab of Developer Tools of IE/Chrome/Firefox or to use free tool [Fiddler](http://www.telerik.com/fiddler). You can append your question with the test JSON data returned for subgrid. It allows to localize the origin of your main problem of loading subgri – Oleg Jan 05 '17 at 20:46

0 Answers0