0

I have a datatable that is using standard features (pagination, sorting, searching, date range, etc.), but I also have a portion at the bottom of the table that displays the total by office. What I would like to implement, however, is a means of hiding any search results that would display as "0" for an office. For instance, if you search my table for "assistant" then Edinburgh, London, Singapore and Tokyo all display a result of "0" (since there are no assistants for any of those offices). Instead of showing those empty results how could I instead hide them?

Here is a link to my jsfiddle: https://jsfiddle.net/l337method/vhoupanz/

Here is my script:

var offices = api.column(2).data().sort().unique().toArray();
var totals = [];
for (var i = 0; i < offices.length; i++) totals.push(0);

api.rows({filter:'applied'}).every(function() {
    var data = this.data();
    totals[offices.indexOf(data[2])] += intVal(data[5]);
    });

html = '';
    for (var i = 0; i < offices.length; i++) {
    html += '<br>' + offices[i] + ': ' + totals[i];
    }
    html += '<br'

$(api.column(4).footer()).html(html);
2
  • 2
    in the for loop, you can just add an if (totals[i] > 0) Commented Sep 24, 2018 at 20:20
  • 1
    Or simply if(totals[i]). Considering that there will be no negative values. Commented Sep 24, 2018 at 20:25

2 Answers 2

1

Try this:

html = '';
for (var i = 0; i < offices.length; i++) {
    if(totals[i] > 0){
         html += '<br>' + offices[i] + ': ' + totals[i];
    }
}
html += '<br'
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thank you so much. I am not sure why the syntax was throwing me off so badly.
1

How about this:

html = [];
for (var i = 0; i < offices.length; i++) {
  if (totals[i] > 0) html.push(offices[i] + ': ' + totals[i]);
}

$(api.column(4).footer()).html(html.length == 0?"":html.join('</br>'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.