|
| 1 | +<article class="main-content"> |
| 2 | + <header class="article-header"> |
| 3 | + <h1><i class="fa fa-play"></i> Row click event</h1> |
| 4 | + </header> |
| 5 | + <section class="article-content"> |
| 6 | + <p> |
| 7 | + Simple example to bind a click event on each row of the DataTable with the help of <a href="http://datatables.net/reference/option/rowCallback">rowCallback</a>. |
| 8 | + </p> |
| 9 | + </section> |
| 10 | + <section class="showcase"> |
| 11 | + <tabset> |
| 12 | + <tab heading="Preview"> |
| 13 | + <article class="preview"> |
| 14 | + <div ng-controller="rowClickEventCtrl"> |
| 15 | + <blockquote>Please click on a row</blockquote> |
| 16 | + <p class="text-danger">You clicked on: <strong>{{ message }}</strong></p> |
| 17 | + <br /> |
| 18 | + <table datatable dt-options="dtOptions" dt-columns="dtColumns" class="row-border hover"></table> |
| 19 | + </div> |
| 20 | + </article> |
| 21 | + </tab> |
| 22 | + <tab heading="HTML"> |
| 23 | +<div hljs> |
| 24 | +<div ng-controller="rowClickEventCtrl"> |
| 25 | + <blockquote>Please click on a row</blockquote> |
| 26 | + <p class="text-danger">You clicked on: <strong>{{ message }}</strong></p> |
| 27 | + <br /> |
| 28 | + <table datatable dt-options="dtOptions" dt-columns="dtColumns" class="row-border hover"></table> |
| 29 | +</div> |
| 30 | +</div> |
| 31 | + </tab> |
| 32 | + <tab heading="JS"> |
| 33 | +<div hljs language="js"> |
| 34 | +angular.module('datatablesSampleApp'). |
| 35 | +controller('rowClickEventCtrl', function ($scope, DTOptionsBuilder, DTColumnBuilder) { |
| 36 | + $scope.message = ''; |
| 37 | + $scope.someClickHandler = function(info) { |
| 38 | + $scope.message = info.id + ' - ' + info.firstName; |
| 39 | + }; |
| 40 | + |
| 41 | + $scope.dtOptions = DTOptionsBuilder.fromSource('data.json') |
| 42 | + .withPaginationType('full_numbers') |
| 43 | + .withOption('rowCallback', function(nRow, aData, iDisplayIndex, iDisplayIndexFull) { |
| 44 | + $('td', nRow).bind('click', function() { |
| 45 | + $scope.$apply(function() { |
| 46 | + $scope.someClickHandler(aData); |
| 47 | + }); |
| 48 | + }); |
| 49 | + return nRow; |
| 50 | + }); |
| 51 | + $scope.dtColumns = [ |
| 52 | + DTColumnBuilder.newColumn('id').withTitle('ID'), |
| 53 | + DTColumnBuilder.newColumn('firstName').withTitle('First name'), |
| 54 | + DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible() |
| 55 | + ]; |
| 56 | +}); |
| 57 | +</div> |
| 58 | + </tab> |
| 59 | + </tabset> |
| 60 | + </section> |
| 61 | +</article> |
0 commit comments