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

时间:2021-01-14 14:25:42

I have two datatables, one which lists foldes, and another which list's files within their parent folder. Here is how my script looks for the folder table:

我有两个数据表,一个列出了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" }
                ]
        });
    });

Now, when the user clicks the link I need to be able to pass that parentid to the file datatable. I have had no luck thus far. Here is how the JSON result looks in my controller, for the files datatable:

现在,当用户单击链接时,我需要能够将该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);
    }

How would I go about getting the link in the folder table to pass the parentid to the jsonresult for the file's datatable successfully?

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

1 个解决方案

#1


1  

I assume the dataTables are on the same page so I'd switch it to a button...

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

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

Add a live click handler so you can set the current parentid and refresh the files dataTable. Handler might look like this...

添加实时单击处理程序,以便您可以设置当前的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);
});

Finally, the files dataTable will need to override the fnServerData function to merge the extra parentid data...

最后,文件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
    });
}

#1


1  

I assume the dataTables are on the same page so I'd switch it to a button...

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

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

Add a live click handler so you can set the current parentid and refresh the files dataTable. Handler might look like this...

添加实时单击处理程序,以便您可以设置当前的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);
});

Finally, the files dataTable will need to override the fnServerData function to merge the extra parentid data...

最后,文件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
    });
}