数据表TableTools在行单击时从隐藏列添加/获取json对象

时间:2015-08-14 10:19:04

标签: json datatables tabletools

我正在尝试将数据表初始化添加到隐藏列的json对象数据。如果我将json数据字符串化为隐藏列,则“json对象”将被删除。

res = JSON.parse(JSON.stringify(res).replace(/%3C/g,'%3C%20')); //res is the json object from server
var result = res.items;
var linesArray = []; //this is used further to my buildatatable function where i transform each array position to a datable row
var columnsArray = [];

columnsArray.push(
    {"sTitle": "jsonobj"},//hidden column
    {"sTitle": "Column one"},
    {"sTitle": "Column two"},
    {"sTitle": "Column tree"}, 
    {"sTitle": "Column four"}, 
    {"sTitle": "Column five"});     

//更新FIRST PROBLEM没问题:)

$.each(result, function(index, items){ //loop through to json objects to get values

linesArray.push([JSON.stringify(items),//Json object stringified that gets cut, i made this so i can access further the json on row click
                  decodeValue(items.xptoone),
                  decodeValue(items.xptotwo), 
                  decodeValue(items.xptotree), 
                  decodeValue(items.xptofour), 
                  decodeValue(items.xptofive)]); 
 });

//第二个问题如何使用oTableTools获取JSON对象

//these are options that are concatenated to other options to datatable initialization   
 var options =  { 
            "oTableTools": {
                              "sRowSelect": "single",
                              "sSelectedClass": "row_selected",
                              "fnRowSelected": function (node) {

                                   //SECOND PROBLEM

                              }
                            },
            "columnDefs": [ //Hidden column jsonobj
                              {
                                  "targets": [ 0 ],
                                  "visible": false
                              }
                            ]
            };      

//this function initializes the datatable with specific columns and rows                
buildDataTable("mytable",columnsArray,linesArray,options);

1 个答案:

答案 0 :(得分:2)

您可以按如下方式访问fnRowSelect中的JSON数据:

"fnRowSelected": function(node){
    var data = $('#mytable').DataTable().row(node).data();
    console.log(data[0]);     
}