2013-05-17 31 views
0

我知道很多人問過這個問題。我嘗試過所有其他建議,但都沒有成功。我確實知道我所忽略的一些東西,並想要第二套眼睛來幫助我。提前致謝!!jqSubGrid沒有加載數據

我的問題是,主電網加載找到。它的子網格就是這個問題。

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
     jQuery("#PositionsGrid").jqGrid({ 
      url: '/Position/[email protected]', 
      datatype: 'json', 
      mtype: 'POST', 
      colNames: ['PositionID', 'ID', 'Job', 'Band', 'Start Date', 'End Date','Current Assignee','Terminated Worker','Status'], 
      colModel: [ 
      { name: 'PositionID', index: 'PositionID', width: 20, align: 'left', hidden: true, sortable: false, search: false,key:true }, 
      { name: 'DisplayID', index: 'DisplayID', width: 50, align: 'left' }, 
      { name: 'JobTitle', index: 'JobTitle', width: 100, align: 'left' }, 
      { name: 'Band', index: 'Band', width: 50, align: 'left' }, 
      { name: 'StartDate', index: 'StartDate', width: 50, align: 'left', searchoptions: { sopt: ['cn'], dataInit: datePick} }, 
      { name: 'EndDate', index: 'EndDate', width: 50, align: 'left', searchoptions: { sopt: ['cn'], dataInit: datePick} }, 
      { name: 'CurrentWorker', index: 'CurrentWorker', width: 100, align: 'left' }, 
      { name: 'TerminatedWorker', index: 'TerminatedWorker', width: 100, align: 'left' }, 
      { name: 'Status', index: 'Action', width: 50, align: 'left', search: false}], 
      pager: jQuery('#pager'), 
      rowNum: 10, 
      rowList: [5, 10, 20, 50], 
      sortname: 'PositionID', 
      sortorder: "asc", 
      viewrecords: true, 
      autowidth: true, 
      subGrid: true, 
      subGridRowExpanded: showDetails 

     }); 
     $('#PositionsGrid').jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true, defaultSearch: 'cn' }); 
     $("#PositionGrid").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false, search: false, refresh: true }); 
    }); 

    datePick = function (elem) { 
     $(elem).datepicker({ dateFormat: "mm/dd/yy", onSelect: function (dateText, inst) { $("#grid")[0].triggerToolbar(); } }); 
    }; 

    function showDetails(subgrid_id, row_id) { 

     showSubGrid_AssignmentsGrid(subgrid_id, row_id, "<br><b>Worker History</b><br><br>", "AssignmentsGrid"); 

    } 


    function showSubGrid_AssignmentsGrid(subgrid_id, row_id, message, suffix) { 
    var subgrid_table_id; 
     subgrid_table_id = subgrid_id + "_t"; 
     if (message) jQuery('#' + subgrid_id).append(message); 
     if (message) jQuery('#' + subgrid_id).append(message); 
     jQuery("#" + subgrid_id).html("<table id='" + subgrid_table_id + "' class='scroll'></table>"); 
     jQuery("#" + subgrid_table_id).jqGrid({ 
      url: "Position/GetAssignmentsByPosition?positionid=" + row_id, 
      datatype: "json", 
      colNames: ['AssignID', 'JobReqNum', 'WorkerName', 'StartDate', 'EndDate','Status'], 
      colModel: [ 
      { name: "AssignID", index: "AssignID", width: 80}, 
      { name: "JobReqNum", index: "JobReqNum", width: 130 }, 
      { name: "WorkerName", index: "WorkerName", width: 80, align: "right" }, 
      { name: "StartDate", index: "StartDate", width: 80, align: "right" }, 
      { name: "EndDate", index: "EndDate", width: 100, align: "right" }, 
      { name: "Status", index: "Status", width: 100, align: "right" } 
      ], 
      height: '100%', 
      rowNum: 20, 
      sortname: 'AssignID', 
      sortorder: "asc" 
       }); 

    } 

</script> 

加載子網格控制器動作低於

public JsonResult GetAssignmentsByPosition(int positionid) 
    { 
     ProjPosition position = uow.PositionRepository.GetPosition(positionid); 
     var jsonRows = position.ProjPositionAssignments.AsEnumerable() 
      .Select(a => new 
      { 
       cell = new string[] { a.ID.ToString(), 
             a.JobReqNum == null ? "" :a.JobReqNum, 
             a.Worker == null ? "" : a.Worker.FullName, 
             (a.StartDate == null) ? "" : ((DateTime)a.StartDate).ToString("MM/dd/yyyy"), 
             (a.EndDate == null) ? "" : ((DateTime)a.EndDate).ToString("MM/dd/yyyy"), 
             (a.Status == null ? "Active" : StatusDictionary.AssignmentStatusDictionary[a.Status]) 
            } 
      }) 
      .ToArray(); 

     var jsonData = new 
     { 
      rows = jsonRows 
     }; 

     return Json(jsonData); 

    } 

回答

0

您也需要傳遞給jqGrid的:

  page = numberOfPages 
      records = totalRecords, 

我個人使用類似:

 var jsonData = new 
     { 
      total = (totalRecords + rows - 1)/rows, 
      page = page, 
      records = totalRecords, 
      rows = (
       from item in pagedQuery     
       select new 
       { 
        cell = new string[] {  
         item.value1, 
         item.value2, .... 
        } 
       }).ToArray() 

     }; 
     return Json(jsonData, JsonRequestBehavior.AllowGet); 
+0

事實證明我沒有使用JsonRequestBehavior.AllowGet這是我的問題的原因! – superartsy