如何在Jquery DataTables中使用sName属性

时间:2012-10-31 14:32:16

标签: jquery jquery-datatables

从服务器我获取json格式的数据,我使用下面的代码在页面上呈现它。但是无法在列(即单元格)上设置属性“name”。 sName是否应该用于为每个Cell设置“name”属性的正确属性 这是我的Datables代码。

$('#' + self.dom.tableID).dataTable({
            "asStripeClasses": [self.ui.rowClass],
            "bPaginate": true,
            "bLengthChange": false,
            "bFilter": true,
            "iDisplayLength": 15,
            "bSort": true,
            "bInfo": false,
            "bAutoWidth": true,
            "sDom": "tip",
            "aoColumns": [
                { "mDataProp": "name", "sClass": self.ui.nameCellClass, "sWidth": "35%", "sTitle": "Name" },
                { "mDataProp": "email", "sTitle": "Email"},
                { "mDataProp": "phone", "sTitle": "Phone"},
                { "mDataProp": "organizationName", "sTitle": "Organization"},
                { "mDataProp": "organizationRenewDate", "sTitle": "Expires on", "sWidth": "100px", "sType": "date"}

            ],
            "oLanguage": {
                "sZeroRecords": "No matching members found."
            }
        });

顺便说一句,我需要在每个单元格上使用“name”属性将数据发送回服务器端代码。

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试创建输入,因此当您发布表单时,数据会被发送回服务器吗?

在这种情况下,您需要使用fnRender(旧方式)或mRender(新方式)将输入元素写入您的单元格。以下内容可以让您走上正轨:

{
    mDataProp: "name",
    sTitle: "Name",
    mRender: function(data, type, full) {
        switch(type){
            case 'display':
                return '<input type="text" name="name[]" value="' + data + '" />';
            default:
                return data;
        }
    }
}

修改

根据你的评论,我猜你需要更像这样的东西:

fnRowCallback: function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
    //replace 1 with whichever cell you need
    $('td:nth-child(1)', nRow).attr('name', "WhateverNameYouWant");
}