2017-08-16 97 views
0

我试着在谷歌搜索如何改变数据表的标题,我发现这个线程Datatables API: how to change Print page title如何编辑的DataTable()打印标题

该线程最好的答案是

var table = $('table').DataTable({ 
    buttons: [ 
     extend: 'print', 
     title: function(){ 
     return foo.title 
     } 
    ] 
}); 

我不明白它是如何工作使用这给了我在控制台错误“的indexOf ..”

这是什么,我想改变的图片 DataTable Website Customized Print Example

编辑1尝试这种代码

var table = $('table').DataTable({ 
    buttons: [ 
     extend: 'print', 
     title: function(){ 
     var printTitle = 'New Title'; 
     return printTitle.title 
     } 
    ] 
}); 

回答

0

看看:

https://datatables.net/extensions/buttons/examples/print/customisation.html

可以在按键扩展使用标题:

title: 'Datatables example: Customisation of the print view window' 

或者只是标记

<caption>Datatables example: Customisation of the print view window</caption> 

拨弄一个例子: https://jsfiddle.net/f14ykjem/1/

<table id="example" class="display" cellspacing="0" width="100%"> 
      <thead> 
       <tr> 
        <th>Name</th> 
        <th>Position</th> 
        <th>Office</th> 
        <th>Age</th> 
        <th>Start date</th> 
        <th>Salary</th> 
       </tr> 
      </thead> 
      <tfoot> 
       <tr> 
        <th>Name</th> 
        <th>Position</th> 
        <th>Office</th> 
        <th>Age</th> 
        <th>Start date</th> 
        <th>Salary</th> 
       </tr> 
      </tfoot> 
      <tbody> 
       <tr> 
        <td>Tiger Nixon</td> 
        <td>System Architect</td> 
        <td>Edinburgh</td> 
        <td>61</td> 
        <td>2011/04/25</td> 
        <td>$320,800</td> 
       </tr> 
       <tr> 
        <td>Garrett Winters</td> 
        <td>Accountant</td> 
        <td>Tokyo</td> 
        <td>63</td> 
        <td>2011/07/25</td> 
        <td>$170,750</td> 
       </tr> 
     </tbody> 
    </table> 

    $('#example').DataTable({ 
      dom: 'Bfrtip', 
      buttons: [ 
       { 
        extend: 'print', 
        text: 'ButtonLabelHere', 
        title: 'Datatables example: Customisation of the print view window', 
        customize: function (win) { 
         $(win.document.body) 
          .css('font-size', '10pt') 
          .prepend(
           '<img src="http://datatables.net/media/images/logo-fade.png" style="position:absolute; top:0; left:0;" />' 
          ); 

         $(win.document.body).find('table') 
          .addClass('compact') 
          .css('font-size', 'inherit'); 
        } 
       } 
      ] 
    }); 

**External code:** 

//code.jquery.com/jquery-1.12.4.js 
https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js 
https://cdn.datatables.net/buttons/1.4.0/js/dataTables.buttons.min.js 
//cdn.datatables.net/buttons/1.4.0/js/buttons.print.min.js 

https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css 
https://cdn.datatables.net/buttons/1.4.0/css/buttons.dataTables.min.css 
+0

感谢所有帮助:) –

+0

我很乐意帮 –

0

Datatables buttons扩展它的不是你的努力来实现,只需使用标题标签

<table> 
    <caption id="tablecaption"></caption> 
    .... 
</table> 

$('#tablecaption').text(foo.title); 
0

你的 “编辑1” 是在正确的轨道,但有一个错误。我更正了下面的代码。这样做使标题变得动态,即;它是在打印时生成的。这使您可以否则,标题被固定在当打印按钮被创建的字符串...

var table = $('table').DataTable({ 
    buttons: [ 
     extend: 'print', 
     title: function(){ 
      var printTitle = 'New Title'; 
      return printTitle 
     } 
    ] 
});