Datatables.net: субгриды для таблицы
Задача: при щелчке по одной из ячеек, разворачивать «subgrid» для таблицы. Примерно так:
Решение:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
<table id="ownersgrid" class="display" style="width:100%"> <thead> <tr> <th>#</th> <th></th> <th>Наименование</th> <th>Полное наименование</th> <th>Телефон</th> <th>Email</th> <th>Договора</th> </tr> </thead> </table> <script> ownerstable=$('#ownersgrid').DataTable( { dom: 'frtipB', destroy: true, select: true, lengthMenu: [ [ 10, 25, 50, -1 ], [ '10 строк', '25 строк', '50 строк', 'Все строки' ] ], buttons: [ { text: 'Редактировать', action: function ( e, dt, node, config ) { ConnectorEdit("edit"); } }, { text: 'Добавить', action: function ( e, dt, node, config ) { ConnectorEdit("add"); } }, 'excel','print','pageLength', ], paging: true, keys: true, //scrollY: 400, //scrollCollapse: true, //scroller: true, "processing": true, "serverSide": true, "ajax": { "url": "/server/owners/OwnersGrid", "type": "POST", "data": { } }, "language": { url: '/js/i18n/datatable.json' }, "columns": [ { "data": "id"}, { "data": "deleted" }, { "data": "name"}, { "data": "full_name" }, { "data": "phone" }, { "data": "email" }, { className: 'dt-control',orderable: false,data: null,defaultContent: '',width: '10%'}, ] } ); $('#ownersgrid').unbind(); $('#ownersgrid').on('dblclick', 'tbody td', function () { OwnerEdit("edit"); }); $('#ownersgrid tbody').on('click', 'td.dt-control', function () { var tr = $(this).closest('tr'); var row = ownerstable.row( tr ); if ( row.child.isShown() ) { // This row is already open - close it destroyChild(row); tr.removeClass('shown'); } else { // Open this row createChild(row); tr.addClass('shown'); } } ); function createChild ( row ) { // This is the table we'll convert into a DataTable var table = $('<table class="display" style="width:100%">'+ '<thead>'+ ' <tr> '+ ' <th>#</th>'+ ' <th></th>'+ ' <th>Наименование</th>'+ ' <th>Полное наименование</th>'+ ' <th>Телефон</th> '+ ' <th>Email</th> '+ ' <th>Договора</th>'+ ' </tr>'+ ' </thead>'+ '</table>'); // Display it the child row row.child( table ).show(); // Initialise as a DataTable var usersTable = table.DataTable( { dom: 't', destroy: true, select: true, lengthMenu: [ [ 10, 25, 50, -1 ], [ '10 строк', '25 строк', '50 строк', 'Все строки' ] ], buttons: [ { text: 'Редактировать', action: function ( e, dt, node, config ) { ConnectorEdit("edit"); } }, { text: 'Добавить', action: function ( e, dt, node, config ) { ConnectorEdit("add"); } }, 'excel','print','pageLength', ], paging: true, keys: true, //scrollY: 400, //scrollCollapse: true, //scroller: true, "processing": true, "serverSide": true, "ajax": { "url": "/server/owners/OwnersGrid", "type": "POST", "data": { } }, "language": { url: '/js/i18n/datatable.json' }, "columns": [ { "data": "id"}, { "data": "deleted" }, { "data": "name"}, { "data": "full_name" }, { "data": "phone" }, { "data": "email" }, { className: 'dt-control',orderable: false,data: null,defaultContent: '',width: '10%'}, ] } ); }; function destroyChild(row) { var table = $("table", row.child()); table.detach(); table.DataTable().destroy(); // And then hide the row row.child.hide(); } </script> |