2015-08-26 83 views
1

我有数据的样本JSON对象中找到一个值:填充与JSON一个DataTable,需要渲染的超链接在一列中,在不同的JSON字段

[ 
    { 
     "id": 1, 
     "title": "Fred", 
     "author": "Flintstone" 
    }, { 
     "id": 2, 
     "title": "Fred", 
     "author": "Flintstone" 
    }, { 
     "id": 3, 
     "title": "Fred", 
     "author": "Flintstone" 
    }..... 

HTML

<table class="table" id="tblRunbook"> 
              <thead> 
               <tr> 
                <th width="60px">ID</th> 
                <th width="300px">Title</th> 
                <th width="200px">Author</th> 
               </tr> 
              </thead> 
              <tbody> 

              </tbody> 
             </table> 

JavaScript的:

$(document).ready(function() { 
     $('#tblRunbook').dataTable({ 
      <!-- Retrieve a static json file, you could also have a URL to a controller method. --> 
      "sAjaxSource" : "/getRunbooks", 
      "sAjaxDataProp": "", 
      <!-- Indicate to dataTable what the field names are we want, in the order we want them in the table. --> 
      "aoColumns": [ 
          {"data": "id"}, 
          {"data": "title", 
          "render": function (data, type, row, meta) { 
           return '<a href="runbook?rbID=" + data.id + '>' + data + '</a>';} 
          }, 
          {"data": "author"} 
       ] 


     }); 
    }); 

ID列将被隐藏,但我想使链接runbook?rbID = ID。我想访问JSON对象中的前一个字段,在此例中为"data" : "id",并将其设置在函数返回'<a href="runbook?rbID=" + data.id + '>'的第二个字段中,其中data.idID

+0

您可以使用jQuery地图来创建新的数据源,从其他数据源无限制,并使用新的模型的数据表。 Datatables只能使用一个源。但是如果它分配的数据可能很慢,那么在服务器上重建模型可能是一个更好的主意。 – ppumkin

+0

我认为这个http://www.datatables.net/forums/discussion/200/getting-values-from-hidden-column-rows将有助于解决。 –

回答

0

我能找到解决办法。

var runbookID; 



     "aoColumns": [ 
         {"data": function(data, type, row, meta){ 
          runbookID = data.id; 
          return data.id;} 
         }, 
         {"data": "title", 
         "render": function (data, type, row, meta) { 
          return "<a href='runbook?rbID=" + runbookID + "'>" + data + '</a>';} 
         }, 
         {"data": "author"} 
      ] 
+0

我有同样的问题,这确实有效。但它不可能是从两个json字段创建链接的最佳方式。好奇你是否已经学会了另一种方式来做到这一点?谢谢! – Jamie