微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何在两个jQuery数据表之间传递参数

我有两个数据表,一个列出了foldes,另一个列出了父文件夹中的文件.以下是我的脚本查找文件夹表的方式:

var oTable = $('#folderTable').dataTable({
            "bServerSide": true,"sAjaxSource": "AJAXViewFolders","bProcessing": true,"bFilter": false,"aoColumns": [
                    { "sName": "folder_id","bSearchable": false,"bSortable": false,"fnRender": function (oObj) {
                            return '<a href=\"ViewFiles?parentid=' + oObj.aData[0] + '\">View</a>';
                        }
                    },{ "sName": "folder_name" },{ "sName": "create_date" }
                ]
        });
    });

现在,当用户单击链接时,我需要能够将该parentid传递给文件数据表.到目前为止,我没有运气.以下是JSON结果在我的控制器中对于数据文件的查找方式:

public JsonResult AJAXViewFiles(DataTableParamModel dtParams,int parentid)
    {
        var repo = new TrusteeDocumentRepository();
        var allDocuments = repo.FindAllFiles().Where(c=>c.folder_id == parentid);
        IEnumerable<Files> filteredFiles;
        filteredFiles = allDocuments;


        var displayedFiles = filteredFiles.Skip(dtParams.idisplayStart).Take(dtParams.idisplayLength);
        var result = from c in displayedFiles select new[] { Convert.ToString(c.folder_id),c.file_name,c.upload_date.ToString()  };

        return Json(new
        {
            sEcho = dtParams.sEcho,iTotalRecords = allDocuments.Count(),iTotaldisplayRecords = filteredFiles.Count(),aaData = result
        },JsonRequestBehavior.AllowGet);
    }

如何获取文件夹表中的链接以将parentid成功传递给文件数据表的jsonresult?

解决方法

我假设dataTables在同一页面上,所以我将它切换到一个按钮……

"fnRender": function (oObj) {
    return '<button type="button" class="folder-view" data-id="' + oObj.aData[0] + '">View</button>';
}

添加实时单击处理程序,以便您可以设置当前的parentid并刷新文件dataTable.处理程序可能看起来像这样……

$('.folder-view').on('click',function() {
    var $filesTable = $('#filesTable');
    $filesTable.attr('data-parentid',$(this).attr('data-id'));
    //refresh the files table
    $filesTable.dataTable().fnDraw(false);
});

最后,文件dataTable将需要覆盖fnServerData函数以合并额外的parentid数据……

"fnServerData": function (sSource,aoData,fnCallback) {

    var exTradata = [ { parentid: $('#filesTable').attr('data-parentid') } ];

    $.ajax({
        "dataType": "json","type": "POST","url": sSource,"data": $.merge(exTradata,aoData),"success": fnCallback
    });
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐