本文介绍了jQuery DataTables fnrender 与对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery DataTable 从这个data.txt"中形成一个表格:

I'm using jQuery DataTable to form a table out of this "data.txt":

{ "aaData" : [
{
    "ftitle": "Test1",
    "link": "http://server.com/test1/",
    "fname": "test1.pdf",
    "fid": "test1_353752165.pdf"
},
{
    "ftitle": "Test2",
    "link": "http://server.com/test2/",
    "fname": "test2.pdf",
    "fid": "test2_353754257.pdf"
} ] }

这是我的 js 代码:

This is my js code:

$('#example').dataTable( {
    "bProcessing": true,
    "sAjaxSource": "data/data.txt",
    "aoColumns": [
        {   "sClass": "center",
            "fnRender": function( oObj ) {
                return oObj.aData[0]+' '+ oObj.aData[2];
            }
        },
        { "mDataProp": "fid", "sClass": "center" },
        { "mDataProp": "fname", "sClass": "center" }
    ],
} );

我只想使用 fnrender() 的 .aData 获取实际数据,但这有效仅适用于仅数组数据.我现在得到的是未定义的未定义",如果我使用仅包含数组数据的 .txt,它可以正常工作.

I just want to get the actual data with .aData of fnrender() but this works only with array-only data.What I get now is "undefined undefined", if I use a .txt with just array data it works fine.

我认为我不明白如何以探测方式使用 fnrender,尤其是在处理对象时.

I think I dont get it right how to use fnrender proberly, especially when working with objects.

推荐答案

你得到未定义",因为 oObj.aData 是一个对象,而不是一个数组,并且没有0"字段.使用如下语法:

You're getting "undefined" because oObj.aData is an object, not an array, and there is no "0" field. Use syntax like this:

oObj.aData.link

oObj.aData["link"]

完整示例(仅修改了 fnRender 返回值):

Full example (only modified fnRender return value):

$('#example').dataTable( {
    "bProcessing": true,
    "sAjaxSource": "data/data.txt",
    "aoColumns": [
        {   "sClass": "center",
            "fnRender": function( oObj ) {
                return '<a href="' + oObj.aData.link + '">' + oObj.aData.ftitle + '</a>';
            }
        },
        { "mDataProp": "fid", "sClass": "center" },
        { "mDataProp": "fname", "sClass": "center" }
    ],
} );

这篇关于jQuery DataTables fnrender 与对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 00:17