4

我有两个数据表,一个列出文件夹,另一个列出其父文件夹中的文件。这是我的脚本查找文件夹表的方式:

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?

4

1 回答 1

1

我假设数据表在同一页面上,所以我将它切换到一个按钮......

"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
    });
}
于 2012-09-24T15:20:35.480 回答