How to create row group in json file
How to create row group in json file
Here is the code for me to make a datatable. i already successfully call the data into 5 columns but I would like to group the data by Group ID into one row, I try to use drawcallback function but it doesn't work at all. I have a data table which is name by 'tblprofile'. Does anyone know what's wrong with my code?
<
table id="tblProfile
class="data-table table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>GroupID</th>
<th>Num</th>
<th>Name</th>
<th>Favourite Food</th>
<th>Address</th>
</tr>
</thead>
</table>
function Print() {
if ($.fn.dataTable.isDataTable('#tblProfile')) {
$("#tblProfile").DataTable().destroy();
}
$.ajax({
type: "post",
url: "/Profile/Resume",
contentType: 'application/json',
data: JSON.stringify(searchInfo),
success: function (result) {
console.log(result);
var groupColumn = 0;
tblDataProfile = $('#tblProfile').DataTable({
"data": result.List,
"columns": [
{ "data": "GroupID"},
{ "data": "Number"},
{"data": "Name"},
{ "data": "Profile.Details[0].Favourite"},
{ "data": "Profile.Details[0].Address" },
],
drawCallback: function (settings) {
var api = this.api();
var rows = api.rows({ page: 'current' }).nodes();
var last = null;
api.column(groupColumn, { page: 'current' })
.data().each(function (group, i) {
if (last !== group) {
$(rows).eq(i).before('<tr class="group"><td colspan="8">' + 'Group ID ' + group + '</td></tr>');
last = group;
}
});
},
});
$('#tblProfile_wrapper div.dt-buttons a.buttons-print').trigger('click');
},
});
}
Answers
Any reason to not just use the RowGroup extension?
Allan
Does it function for printing if using RowGroup extension?
No. But your own above won't work with
print
either I'm afraid.Allan