JQuery Datatable - Checkbox selection

Checkbox selection

A selected row is typically shown in a DataTable by using a highlight background colour - however, it can also be useful to use other styling options to convey the selected state of items in a table to the end user.

A common option is to use a checkbox which can be clicked on to toggle row selection, which can be particularly useful if you wish to restrict row selection activation to a particular column, so other actions can be performed on the other cells in the table (such as inline editing).

A column can be shown with a checkbox that reflects the row's selected status simply through use of the select-checkboxCSS class for that column (columns.className). Row selection can be restricted to that column using the select.selector option.

The checkbox is not an <input type="checkbox"> element, but rather a CSS that uses the :before and :after pseudo elements of the cell to draw a box and the tick. This can therefore be easily modified to suit the style of your site / app.

  Name Position Office Age Salary
  Name Position Office Age Salary
  Airi SatouAccountantTokyo33$162,700
  Angelica RamosChief Executive Officer (CEO)London47$1,200,000
  Ashton CoxJunior Technical AuthorSan Francisco66$86,000
  Bradley GreerSoftware EngineerLondon41$132,000
  Brenden WagnerSoftware EngineerSan Francisco28$206,850
  Brielle WilliamsonIntegration SpecialistNew York61$372,000
  Bruno NashSoftware EngineerLondon38$163,500
  Caesar VancePre-Sales SupportNew York21$106,450
  Cara StevensSales AssistantNew York46$145,600
  Cedric KellySenior Javascript DeveloperEdinburgh22$433,060
Showing 1 to 10 of 57 entries 1 row selected
  • Javascript
  • HTML
  • CSS
  • Comments (0)

The Javascript shown below is used to initialise the table shown in this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$(document).ready( function () {
     $( '#example' ).DataTable( {
         columnDefs: [ {
             orderable:  false ,
             className:  'select-checkbox' ,
             targets:   0
         } ],
         select: {
             style:     'os' ,
             selector:  'td:first-child'
         },
         order: [[ 1,  'asc'  ]]
     } );
} );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LarryHai6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值