DataTable fnRowCallback链接未定义为json

时间:2012-11-06 17:03:57

标签: json datatable hyperlink undefined

我正在处理从json数据创建DataTable中的链接。事情会好,但链接名称似乎不对。无论我试图改变什么,它都会说“未定义”。这是我的代码:

<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        var oTable = $('#tablez').dataTable( {
            "bProcessing": true,
            "bJQueryUI": true,
            "sAjaxSource": 'inc/all_cars_json.php',
            "sPaginationType": "full_numbers",
            "aaSorting": [[ 0, "desc" ]],                   
             "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
                    $('td:eq(1)', nRow).html('<a href="car/=' + nRow[0] + '">' +
                        nRow[1] + '</a>');
                    return nRow;
                }
        } );
    } );
</script>

JSON数据:

{"aaData": [
["2715","Toyota","Soluna","VIOS 1.5E VVT-i","2007","\u0e14\u0e33","430,000"],
["2589","Toyota","","MIGHTY X","1995","\u0e40\u0e02\u0e35\u0e22\u0e27","159,000"],["2997","Mazda","Fighter","Freestyle CAB TURBO","2003","\u0e1a\u0e23\u0e2d\u0e19\u0e0b\u0e4c\u0e40\u0e07\u0e34\u0e19","329,000"],
["3002","Isuzu","Rodeo","LS 4WD","2000","\u0e1a\u0e23\u0e2d\u0e19\u0e0b\u0e4c\u0e17\u0e2d\u0e07","319,000"],
["3126","Toyota","Hilux","TIGER D4D CAB","2003","\u0e02\u0e32\u0e27","465,000"],["3127","Mitsubishi","Triton","DID Commonrail","2006","\u0e1a\u0e23\u0e2d\u0e19\u0e0b\u0e4c\u0e17\u0e2d\u0e07","455,000"],
["3128","Honda","City","1.5 i-VTEC","2009","\u0e14\u0e33","0"]
]}

这是预览:

enter image description here 问题是:在这种情况下,如何使用链接标题和值(nRow [0])。 的问候,

2 个答案:

答案 0 :(得分:3)

这可以使用:

完成
     "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            $('td:eq(1)', nRow).html('<a href="car/=' + nRow[0] + '">' +
                $('td:eq(1)', nRow)[1].textConent + '</a>');
            return nRow;
        }

或命名列

"aoColumns": [
              { "bSortable": false, "sWidth": "34px", "mDataProp": "Id" },
              { "mDataProp": "Brand" },
              { "mDataProp": "Type" },
              { "mDataProp": "SubType" },
              { "mDataProp": "Year" }]

然后你可以使用它:

 "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
        $('td:eq(1)', nRow).html('<a href="car/=' + nRow[0] + '">' +
            aData.Brand + '</a>');
        return nRow;
    }

答案 1 :(得分:0)

nRow功能中的aData更改为fnRowCallback

"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                    $('td:eq(1)', nRow).html('<a href="car/=' + aData[0] + '">' +
                        aData[1] + '</a>');
                }