【问题标题】:How to pass a parameter between two jQuery datatables如何在两个 jQuery 数据表之间传递参数
【发布时间】:2012-09-16 00:48:00
【问题描述】:

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

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?

【问题讨论】:

    标签: c# jquery asp.net-mvc datatable


    【解决方案1】:

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

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

    添加一个实时点击处理程序,以便您可以设置当前 parentid 并刷新文件数据表。处理程序可能看起来像这样......

    $('.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
        });
    }
    

    【讨论】:

    • 对不起,我应该指定的。这些数据表位于两个不同的页面上,但我认为这个解决方案只需切换 AJAX url 就可以正常工作,对吗?
    • 正确,更简单...您可以将 parentid 直接放入 ajax url。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多