How to count the number of combination of rows with a drop down list selection in datatables


How to count the number of combination of rows with a drop down list selection in datatables



I would like to count the number of rows in a table after making a selection in a drop down list (Name). but the only problem here is i would like to count number of rows basing on an other attribute in my table (position).
for example:
if i make a selection in the Name: Tiger Nixon
i would like to get
total rows: 3
Position 1 : 1 rows Position 2 : 1 rows Position 3 : 1 rows



Here the code I use to make a drop down list selection with attribute (Name) and it worked perfectly.


$(document).ready(function () {
$('#example').DataTable({
initComplete: function () {
this.api().columns(0).every(function () {
var column = this;
var select = $('<select class="Search"><option value=""></option></select>')
.appendTo($(column.header()))
.on('change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search(val ? '^' + val + '$' : '', true, false)
.draw();
});
column.data().unique().sort().each(function (d, j) {
select.append('<option value="' + d + '">' + d + '</option>')
});
});
}
});

$('.Search').change(function () {
if ($("#example > tbody > tr > td").length == 1) {
$('#Count').empty();

$('#Count').append('Count: 0' );;
} else {
$('#Count').empty();
$('#Count').append('Count: ' + $("#example > tbody > tr").length);
}
});
});



this pict for counting number of rows using just selection on Name





Possible duplicate of How to get filtered row count
– Peter B
Jul 3 at 9:50





Yep, it's a duplicate of that thread @PeterB mentioned above - just use $('#table_id').DataTable().page.info().recordsDisplay
– colin0117
Jul 4 at 9:44


$('#table_id').DataTable().page.info().recordsDisplay





thanks for ur ansers guys. i found a half solution. could you please see how can solve this. i edited the question a litte bit thanks.
– Mohammed Ajeddig
Jul 4 at 10:48





@colin0117 is it possible to $('#table_id').DataTable().page.info().recordsDisplay to count number of rows with a specific value? thanks
– Mohammed Ajeddig
Jul 5 at 10:25





No, page.info() returns information about the current dataset. You could use filter() to determine that - see datatables.net/reference/api/filter()
– colin0117
Jul 5 at 14:25









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages