在某些情况下你可能需要自动创建内容,这时你可以使用
columns.renderDT
参数,来渲染每一行数据
对于简单的静态的数据可以使用
columns.defaultContentDT
参数
本示例演示如何使用
columns.defaultContentDT
在表的最后一列中创建按钮并添加点击事件,通过
row().data()DT
方法得到这行的数据,并alert出相关信息
此外注意到列中的
columns.dataDT
选项已被设置为 null,表示列不会获得数据源对象的信息。
Name | Position | Office | Extn. | Start date | Salary |
---|---|---|---|---|---|
Name | Position | Office | Extn. | Start date | Salary |
Airi Satou | Accountant | Tokyo | 5407 | 2008/11/28 | |
Angelica Ramos | Chief Executive Officer (CEO) | London | 5797 | 2009/10/09 | |
Ashton Cox | Junior Technical Author | San Francisco | 1562 | 2009/01/12 | |
Bradley Greer | Software Engineer | London | 2558 | 2012/10/13 | |
Brenden Wagner | Software Engineer | San Francisco | 1314 | 2011/06/07 | |
Brielle Williamson | Integration Specialist | New York | 4804 | 2012/12/02 | |
Bruno Nash | Software Engineer | London | 6222 | 2011/05/03 | |
Caesar Vance | Pre-Sales Support | New York | 8330 | 2011/12/12 | |
Cara Stevens | Sales Assistant | New York | 3990 | 2011/12/06 | |
Cedric Kelly | Senior Javascript Developer | Edinburgh | 6224 | 2012/03/29 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $(document).ready( function () { var table = $( '#example' ).DataTable( { "ajax" : "data/arrays.txt" , "columnDefs" : [ { "targets" : -1, "data" : null , "defaultContent" : "<button>Click!</button>" } ] } ); $( '#example tbody' ).on( 'click' , 'button' , function () { var data = table.row( $( this ).parents( 'tr' ) ).data(); alert( data[0] + "'s salary is: " + data[ 5 ] ); } ); } ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | < table id = "example" class = "display" cellspacing = "0" width = "100%" > < thead > < tr > < th >Name</ th > < th >Position</ th > < th >Office</ th > < th >Extn.</ th > < th >Start date</ th > < th >Salary</ th > </ tr > </ thead > < tfoot > < tr > < th >Name</ th > < th >Position</ th > < th >Office</ th > < th >Extn.</ th > < th >Start date</ th > < th >Salary</ th > </ tr > </ tfoot > </ table > |
Translation from DataTables.net, with permission