datatable将变量替换为其范围之外的数据

时间:2011-10-20 20:25:42

标签: javascript jquery asp.net-mvc-3 model-view-controller datatables

我正在使用datatables.net来显示一些文件附件,现在我想在控制器中为我的动作添加描述和编辑链接,但它的表现真的很奇怪。

我有这段代码

"aoColumnDefs": [{
    "fnRender": function (oObj) {
        var id = oObj.aData[0];
        var uploaded = oObj.aData[1];
        var dosseirId = oObj.aData[2];
        var contentLenght = oObj.aData[3];
        var filename = oObj.aData[4];
        var description = oObj.aData[5];
        var contentType = oObj.aData[6];

        var retVal = '<div>' + '<div>' + '<a href="' + GetAbsolutePath('Attachment/Download?id=' + id) + '">' + filename + '</a>' + '<a href="Delete" onclick="javascript:systematic_HTMLEditor.handleDelete(' + id + '); return false;"><img src="' + GetAbsolutePath('Content/remove-upload.png') + '" /> </a>' + '</div>' + '</div>';

        return retVal;
    },
    "aTargets": [0],
    "bSortable": true
}, {
    "bVisible": false,
    "aTargets": [1]
}, {
    "bVisible": false,
    "aTargets": [2]
}, {
    "bVisible": false,
    "aTargets": [3]
}, {
    "bVisible": false,
    "aTargets": [4]
},

{
    "fnRender": function (oObj) {
        var id = oObj.aData[0];
        var uploaded = oObj.aData[1];
        var dosseirId = oObj.aData[2];
        var contentLenght = oObj.aData[3];
        var filename = oObj.aData[4];
        var description = oObj.aData[5];
        var contentType = oObj.aData[6];

        var retVal = id;

        return retVal;
    },
    "aTargets": [6],
    "bSortable": true
}

但这给了我这个结果

Attachment Description Editlink

"attachment3.pdf" "i dag" "attachment3.pdf"

所以3列应该是附件的ID,但是被转换为aTargets [0]的结果,这是attachment3.pdf,然后我尝试重命名很多变量而没有任何结果如果我改变那么我不打印ID,但contentTypecontentLength则没有问题,并显示正确的数据。我需要发布ID以创建编辑链接

这是第一部分的oObj.aDatas内容

aData: Array[7]
0: 21
1: "12-10-2011 08:06:00"
2: 1
3: 81695
4: "attachment3.pdf"
5: "i dag"
6: "application/pdf"
length: 7

在这里,在第二部分,以及ID如何突然从之前的代码中获取此值,我真的没有。

    aData: Array[7]
0: "<div><div><a href="/Attachment/Download?id=21">attachment3.pdf</a><a href="Delete" onclick="javascript:systematic_HTMLEditor.handleDelete(21); return false;"><img src="/Content/remove-upload.png" /> </a></div></div>"
1: "12-10-2011 08:06:00"
2: "1"
3: "81695"
4: "attachment3.pdf"
5: "i dag"
6: "application/pdf"
length: 7

1 个答案:

答案 0 :(得分:1)

我认为这里发生的事情是fnRender实际上改变了aData.oObj中的值。您需要的是设置bUseRendered: false

"aoColumnDefs": [{
    "fnRender": function (oObj) {
        var id = oObj.aData[0];
        var uploaded = oObj.aData[1];
        var dosseirId = oObj.aData[2];
        var contentLenght = oObj.aData[3];
        var filename = oObj.aData[4];
        var description = oObj.aData[5];
        var contentType = oObj.aData[6];

        var retVal = '<div>' + '<div>' + '<a href="' + GetAbsolutePath('Attachment/Download?id=' + id) + '">' + filename + '</a>' + '<a href="Delete" onclick="javascript:systematic_HTMLEditor.handleDelete(' + id + '); return false;"><img src="' + GetAbsolutePath('Content/remove-upload.png') + '" /> </a>' + '</div>' + '</div>';

        return retVal;
    },
    "bUseRendered": false,
    "aTargets": [0],
    "bSortable": true
}, {
    "bVisible": false,
    "aTargets": [1]
}, {
    "bVisible": false,
    "aTargets": [2]
}, {
    "bVisible": false,
    "aTargets": [3]
}, {
    "bVisible": false,
    "aTargets": [4]
},

{
    "fnRender": function (oObj) {
        var id = oObj.aData[0];
        var uploaded = oObj.aData[1];
        var dosseirId = oObj.aData[2];
        var contentLenght = oObj.aData[3];
        var filename = oObj.aData[4];
        var description = oObj.aData[5];
        var contentType = oObj.aData[6];

        var retVal = id;

        return retVal;
    },
    "aTargets": [6],
    "bSortable": true
}