2013-07-10 121 views
2

DataTables插件似乎不允许自定义渲染。在数据表中自定义渲染

我们可以初始化使用aTargets和mRender定制单元格呈现:

"aoColumnDefs": [{ 
    "aTargets": [transaction_id_index], 
"mRender": function (data, type, row) { 
    return 'custom '+data; 
    } 
}] 

我该怎么办表头是一回事吗?

注:我使用显示和隐藏功能,所以我不能直接修改aColumns中的sTitle。

编辑

我想,以尽量减少列宽重命名列标题。我得到了这样的标题:“foo_bar”。 现在我使用的这一点,但它不是最好的办法是肯定的:

'fnInitComplete': function(oSettings, json){ 
    $(table).find("thead tr th").each(function(index) { 
     $(this).html($(this).html().split("_").join("<br>")); 
    }); 
}, 
"fnDrawCallback": function(oSettings) { 
    // TO IMPROVE 
    $(table).find("thead tr th").each(function() { 
     if($(this).text().indexOf("_") !== -1) { 
      $(this).html($(this).text().split("_").join("<br>"));           
     } 
    }); 
} 

感谢@kabstergo的提示! 我不关闭这个问题,因为我的解决方案不是“干净”的。

回答

2

是的,你可以自定义页眉和页脚,因为没有人回答你生病尝试给你一个开始。 注意:在数据表的内部工作方面,我没有专家意见。

在我们的网站这里我们有自定义标题的datatables。这是由这样

var table = $('#my-datatable'); 
table.dataTable({ 
    ... 
    'sDom': '<"dataTables_header"lfr>t<"dataTables_footer"ip>', 
    'fnInitComplete': function(oSettings){ 
    // Style length select 
    table.closest('.dataTables_wrapper').find('.dataTables_length select').addClass('select blue-gradient glossy').styleSelect(); 
    tableStyled = true; 
    } 
}); 

就像我说的做,我希望它帮你的东西开始...

+0

这是一个良好的开端和好于另一funtion修改!我的问题是:在初始化时隐藏列(使用“bVisible”:false)。调用切换列可见性的fnShowHide()函数时,修改标题的最佳方法是什么? – fdubrez

1

在数据表^ 1.10,有headerCallback钩,你可以在初始化提供选项。

$是jQuery的)

$('#example').DataTable({ 
    headerCallback: function headerCallback(thead, data, start, end, display) { 
     $(thead) 
      .find('th') 
      .first() 
      .html('Displaying ' + (end - start) + ' records'); 
    } 
}); 

注意,第一个参数实际上可能是第一个trthead里面,不一定是thead元素本身,相反的是规定在DataTables文档中。

在与多个标题行(tr S)复杂的场景中,你可能需要选择这样的:

$('#example').DataTable({ 
    headerCallback: function headerCallback(thead, data, start, end, display) { 
     $(thead) 
      .closest('thead') 
      .find('th') 
      .first() 
      .html('Displaying ' + (end - start) + ' records'); 
    } 
});