Datatables Renderers

Renderers

There are occasions when working with tables that the data source for rows in the table do not contain the value that you wish to show directly in the table. You may wish to transform it to a different representation (a time stamp into a human readable format), combine data points (first and last names) or perform some computation on the value (calculating margin from turnover and expense values).

This transformation of the original data into the value that will be shown in the DataTable is called rendering in DataTables' terminology and is performed using the columns.render option.

 

Built-in helpers

DataTables has two built in rendering helpers that can be used to easily format data - more can be added using plug-ins (see below):

  • number - for formatting numbers
  • text - to securely display text from a potentially unsafe source (HTML entities are escaped).

The built in rendering helpers can be accessed under the DataTable.render object (since 1.11) or $.fn.dataTable.render (which is an alias to the same object). They are functions (allowing options to be passed into them) which should be immediately executed and their result assigned to the columns.render method. This might sound a little complicated, but it simply means you would use something like the following:

1
2
3
4
{
    data: 'price',
    render: DataTable.render.number( ... )
}

Number helper

The number helper provides the ability to easily format, you guessed it, numbers! When dealing with numbers, you may often wish to add formatting such as prefix and postfix characters (currency indicators for example), use a thousands separator and specify a precision for the number. This is all possible with the number helper.

The helper function takes up to five optional parameters:

  1. The thousands separator (required)
  2. Decimal separator (required)
  3. Floating point precision - 0 for integers, 1 for a single decimal place, etc (optional)
  4. A prefix string (optional)
  5. A postfix string (optional).

For example, to display the price data point from the data structure shown above in the format $19.99 we would use:

1
2
3
4
{
    data: 'price',
    render: DataTable.render.number( ',', '.', 2, '$' )
}

This example doesn't require the thousands separator, but for larger values such as 1000 they would be formatted as $1,000.00.

Note that if the number helper encounters a value which is not a valid number (either number or string that contains a number) it will return the value after escaping any HTML entities in it (to help protect against potential security attacks).

Text helper

The text helper will ensure that any potentially dangerous HTML in the source data will not be executed by escaping the HTML entities. This can be useful if the data being loaded may come from a potentially untrusted data source and can help mitigate XSS attacks.

The text helper doesn't take any parameters making its use simply:

1
2
3
4
{
    data: 'product',
    render: DataTable.render.text()
}

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(89)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-09-03 Resize image online 调整图片大小
2019-09-03 Resend a Request by fiddler
2019-09-03 .NET Standard
2016-09-03 git -C
2016-09-03 Common Macros for Build Commands and Properties
2016-09-03 Hearthstone-Deck-Tracker项目的编译
2014-09-03 TeeChart的网络资料
点击右上角即可分享
微信分享提示