jQuery Datatable使每个单元格成为一个链接

时间:2016-06-14 22:52:13

标签: javascript jquery asp.net-mvc datatable datatables

我有四个实体,让我们说A,B,C,D互相连接(B取决于A,C取决于B,D取决于C)。我想在一个表中显示所有可以轻松搜索和过滤的信息。

所以我创建了一个表单的视图模型:

public class MyViewModel {
    public Aname {get; set;}
    public Alink {get; set;}
    public Bname {get; set;}
    public Blink {get; set;}
    public Cname {get; set;}
    public Dname {get; set;}
    public Dlink {get; set;}
}

我希望该表有四列显示每个实体的名称,单元格中的每个数据都是一个超链接,指向所选实体的详细信息页面(实体C除外)。

这是javascript

$('#myDataTable').DataTable({
    'bDestroy': true,
    "bInfo": true,
    "bProcessing": true,
    "bDeferRender": true,
    'iDisplayLength': 10,
    'sPaginationType': 'full_numbers',
    'sPageButtonActive': "paginate_active",
    'sPageButtonStaticDisabled': "paginate_button",
    'data': OptionsHandler.Data,
    "columnDefs": [
        {
            "render": function (data, type, row) {
                return "<a href=" + row[1] + "'>" + row[0] + "</a>";
            },
        },
    ]
});

但它抱怨

  

请求第0行的未知参数0.有关的更多信息   此错误,请参阅http://datatables.net/tn/4

数据采用Json格式:

data = [
       {"Aname":"PatriceBoyle",
        "Alink":"/A/Details/00014",
        "Bname":"Software Engineering",
        "Blink":"/B/Details/2",
        "Cname":"info",
        "Dname":"Database Design",
        "Dlink":"/D/Details/1"
       }, etc.]

我怎么说:return "<a href=" + link + "'>" + name + "</a>";每个细胞?

3 个答案:

答案 0 :(得分:1)

row[1]表示数组索引或对象属性名称'1',但您没有。{/ p>

您需要以下内容:

return "<a href=" + row.Alink + "'>" + row.Aname + "</a>";

答案 1 :(得分:1)

我将代码剥离到最小值并得到相同的错误,直到我将columndef更改为列代替:

columns: [
            { title: "Aname", data: "Aname", render: function (data, type, row) { return "<a href=" + row[1] + "'>" + row[0] + "</a>"; } },
            { title: "Alink", data: "Alink", render: function (data, type, row) { return "<a href=" + row[1] + "'>" + row[1] + "</a>"; } },
            { title: "Bname", data: "Bname", render: function (data, type, row) { return "<a href=" + row[1] + "'>" + row[2] + "</a>"; } },
            { title: "Blink", data: "Blink", render: function (data, type, row) { return "<a href=" + row[1] + "'>" + row[3] + "</a>"; } },
            { title: "Cname", data: "Cname", render: function (data, type, row) { return "<a href=" + row[1] + "'>" + row[4] + "</a>"; } },
            { title: "Dname", data: "Dname", render: function (data, type, row) { return "<a href=" + row[1] + "'>" + row[5] + "</a>"; } },
            { title: "Dlink", data: "Dlink", render: function (data, type, row) { return "<a href=" + row[1] + "'>" + row[6] + "</a>"; } }
        ]

答案 2 :(得分:0)

我设法将两个答案结合起来:

columns: [
        { title: "Column A", data: "Aname", render: function (data, type, row) { return "<a href=" + row.Alink + "'>" + row.Aname + "</a>"; } },
        { title: "Column B", data: "Bname", render: function (data, type, row) { return "<a href=" + row.Blink + "'>" + row.Bname + "</a>"; } },
        { title: "Column C", data: "Cname", render: function (data, type, row) { return "<a href=" + row.Clink + "'>" + row.Cname + "</a>"; } },
        { title: "Column D", data: "Dname", render: function (data, type, row) { return "<a href=" + row.Dlink + "'>" + row.Dname + "</a>"; } },
    ]