I'm using Datatables to create a grouped by data grid. I was able to group my table using my first column.
I want to accomplish getting the count on each grouped row and writing out the count on my grouped row, how would I go about doing that?
If you see the link below, they have the shaded grouped row. I would like the count to appear right after the text of the grouped text.
http://www.datatables.net/examples/advanced_init/row_grouping.html
Thank you.
This can be accomplished by adding a for loop inside drawCallBack function. Referring to http://www.datatables.net/examples/advanced_init/row_grouping.html for drawCallBack function. Please note I am using legacy dataTable function i.e fnDrawCallBack replace that with latest equivalent.
fnDrawCallback: function(settings) {
var api = this.api();
var rows = api.rows({ page: "current" }).nodes();
var last = null;
var storedIndexArray = [];
api.column(0, { page: "current" })
.data()
.each((group, i) => {
if (last !== group) {
storedIndexArray.push(i);
$(rows)
.eq(i)
.before(
'<tr class="group"><td colspan="4">' +
group +
"<span class='group-count'></span></td></tr>"
);
last = group;
}
});
storedIndexArray.push(
api.column(0, { page: "current" }).data().length
);
for (let i = 0; i < storedIndexArray.length - 1; i++) {
let element = $(".group-count")[i];
$(element).text(
storedIndexArray[i + 1] - storedIndexArray[i]
);
}
}
function CountRows(){
debugger;
var abc = $('.abc');
var arr = [];
$( abc ).each(function( index, value ) {
var dynamicId = (this.id.split('_')[1]);
var groupRowCount = $('.group_'+dynamicId).length;
this.textContent = this.textContent + " (" + groupRowCount + ")"
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With