0

我正在开发一个 MVC3/razor 应用程序,我正在尝试使用 subGridRowExpanded 将 jqGrid 嵌套在另一个内部(因此我可以使用格式化程序等),如何将父网格的 id 值传递给子网格网格?

下面的代码运行主网格,并使用“搜索/客户”URL 中的数据填充它,但我不知道如何选择我在“搜索/银行链接”URL 控制器中选择的记录

谁能告诉我该怎么做?

在此先感谢安德鲁。

我的索引.cshtml

@{
    ViewBag.Title = "Index";
}

<h2>@ViewBag.Message</h2>
<div id="content">
    <div id="content-left">
        @Html.Partial("SearchPanel")
    </div>
    <div id="content-main">
        <table id="jpgCustomers" cellpadding="0" cellspacing="0"></table>
        <div id="jpgpCustomers" style="text-align:center;"></div>
    </div>
</div>
@section JavaScript
{
<script type="text/javascript">
    $(document).ready(function ()
    {
        $('#jpgCustomers').jqGrid({
            //url from wich data should be requested
            url: '@Url.Action("Customers")',
            datatype: 'json',
            mtype: 'POST',
            colNames: ['Name', 'FullName', 'SFTP Enabled', 'IsTranbase'],
            colModel: [
                { name: 'LogonName', index: 'LogonName', align: 'left' },
                { name: 'FullName', index: 'FullName', align: 'left' },
                { name: 'Enabled', index: 'Enabled', align: 'left' },
                { name: 'IsTran', index: 'IsTranbase', align: 'left' }
            ],
            pager: $('#jpgpCustomers'),
            rowNum: 10,
            sortname: 'FullName',
            sortorder: 'asc',
            viewrecords: true,
            height: '100%',
            subGrid: true,
            subGridRowExpanded: function(subgrid_id, row_id) {
                var subgrid_table_id, pager_id;
                subgrid_table_id = subgrid_id+"_t";
                pager_id = "p_"+subgrid_table_id;
                $("#"+subgrid_id).html("<table id='"+subgrid_table_id+"'class='scroll'></table><div id='"+pager_id+"'class='scroll'></div>");
                $("#"+subgrid_table_id).jqGrid({
                    url: '@Url.Action("BankLinks")',
                    datatype: 'json',
                    mtype: 'POST',
                    colNames: ['Bank', 'Folder', 'Enabled'],
                    colModel:[
                        {name:"Bank",index:"Bank",width:80,key:true},
                        {name:"Folder",index:"Folder",width:130},
                        {name:"Enabled",index:"Enabled",width:70,align:"left"}
                    ],
                    rowNum:20,
                    pager: pager_id,
                    sortname: 'Bank',
                    sortorder: "asc",
                    viewrecords: true,
                    height: '100%'
                });
                $("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false})
            },
        });
    });
</script>
}

我的控制器操作:

顾客

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Customers(JqGridRequest request)
{
    ISession session = NHibernateHelper.GetCurrentSession();
    IEnumerable<Customer> customers = session.QueryOver<Customer>().List().Skip<Customer>(0).Take<Customer>(request.RecordsCount);

    int totalRecords = customers.Count();

    //Prepare JqGridData instance
    JqGridResponse response = new JqGridResponse()
    {
        //Total pages count
        TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
        //Page number
        PageIndex = request.PageIndex,
        //Total records count
        TotalRecordsCount = totalRecords
    };
    //Table with rows data
    foreach (Customer customer in customers)
    {
        response.Records.Add(new JqGridRecord(Convert.ToString(customer.Id), new List<object>()
        {
            customer.FtpDetails.LogonName,
            customer.FtpDetails.FullName,
            customer.FtpDetails.Enabled,
            customer.IsTran
        }));
    }

    //Return data as json
    return new JqGridJsonResult() { Data = response };
}

银行链接

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult BankLinks(JqGridRequest request)
{
    ISession session = NHibernateHelper.GetCurrentSession();

    //IN THIS LINE I HAVE HARDCODED value 14 - I need this value to be passed from the 
    //Parent Grid!
    Customer customer = session.Get<Customer>(14);

    int totalRecords = customer.Banks.Count();

    //Prepare JqGridData instance
    JqGridResponse response = new JqGridResponse()
    {
        //Total pages count
        TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
        //Page number
        PageIndex = request.PageIndex,
        //Total records count
        TotalRecordsCount = totalRecords
    };

    foreach (Bank bank in customer.Banks.ToList<Bank>())
    {
        CustomerBank bankLink = session.QueryOver<CustomerBank>().Where(x => x.BankId == bank.Id).Where(y => y.CustomerId == customer.Id).List<CustomerBank>().FirstOrDefault();

        response.Records.Add(new JqGridRecord(null, new List<object>()
        {
            bank.BankCode,
            bank.Folder,
            bankLink.Enabled 
        }));
    }
     return new JqGridJsonResult() { Data = response };
 }
4

1 回答 1

1

首先你应该subGridRowExpanded像这样修改你的回调:

subGridRowExpanded: function(subgrid_id, row_id) {
    var subgrid_table_id, pager_id;
    subgrid_table_id = subgrid_id + '_t';
    pager_id = "p_"+subgrid_table_id;
    $('#' + subgrid_id).html('<table id="' + subgrid_table_id + '" class="scroll"></table><div id="' + pager_id + '" class="scroll"></div>');
    $('#' + subgrid_table_id).jqGrid({
        url: encodeURI('@Url.Action("BankLinks")' + '?id=' + row_id),
        datatype: 'json',
        mtype: 'POST',
        colNames: ['Bank', 'Folder', 'Enabled'],
        colModel: [
            {name: 'Bank', index: 'Bank', width:80, key:true },
            {name: 'Folder', index:'Folder', width:130 },
            {name: 'Enabled', index: 'Enabled', width:70, align: 'left' }
        ],
        rowNum: 20,
        pager: pager_id,
        sortname: 'Bank',
        sortorder: 'asc',
        viewrecords: true,
        height: '100%'
    });
    $('#' + subgrid_table_id).jqGrid('navGrid', '#' + pager_id, {edit: false, add: false, del: false })
}

请注意使用encodeURI以确保生成的 URL 有效。

现在您可以像这样修改您的操作方法:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult BankLinks(int id, JqGridRequest request)
{
    //You can sue id parameter to get the data for selected client.
    ...
}

这应该可以解决问题。

于 2012-10-08T07:29:49.817 回答