1

I have a piece of code that I am working with where the user can choose to download a "Slice" of a pie chart or the whole thing.

When they click on a slice, I send a variable called source which tells me which slice it came from.

Right now I am working on the "Download All" button. The source for this will be simply all.

This is what my loop looks like if the user selects slice:

// Loop over our data
$.each(seed.dataset,function(key, value){
   if(this[col] == slice){
      table += '<tr>';
      table += '<td>'+this.escalationID+'</td>';
      table += '<td>'+this.escReasonID+'</td>';
      table += '<td>'+this.reasonText+'</td>';
      table += '<td>'+this.escCreatedBy+'</td>';
      table += '<td>'+this.escCreatorFirst+'</td>';
      table += '<td>'+this.escCreatorLast+'</td>';
      table += '</tr>';
  }
});

The IF statement within the loop is what controls the data its including in the table variable. My question is, how can I choose when I want to enforce this IF statement or ignore it (allowing all data to be added to the table var)?

The obvious would be wrapping it in another IF statement but I feel there might be a cleaner way without having to duplicate all of the table data.

End result would be a bool saying if source=All include all the rows else if source=slice enforce the IF statement to include only the data we are looking for.

UPDATE

Essentially I am looking for a more clean way to do this:

// Loop over our data
    $.each(seed.dataset,function(key, value){

      if(source == 'slice'){

        if(this[col] == slice){
           table += '<tr>';
           table += '<td>'+this.escalationID+'</td>';
           table += '<td>'+this.escReasonID+'</td>';
           table += '<td>'+this.reasonText+'</td>';
           table += '<td>'+this.escCreatedBy+'</td>';
           table += '<td>'+this.escCreatorFirst+'</td>';
           table += '<td>'+this.escCreatorLast+'</td>';
           table += '</tr>';
       }

      // We are downloading all so ignore the constraint 
      }else{
           table += '<tr>';
           table += '<td>'+this.escalationID+'</td>';
           table += '<td>'+this.escReasonID+'</td>';
           table += '<td>'+this.reasonText+'</td>';
           table += '<td>'+this.escCreatedBy+'</td>';
           table += '<td>'+this.escCreatorFirst+'</td>';
           table += '<td>'+this.escCreatorLast+'</td>';
           table += '</tr>';
      }
    });

3 Answers 3

1

You can use another boolean say ignoreAll as "OR" condition with existing if condition as shown below

// Loop over our data
var ignoreAll = true; // change its value as per your requirement
$.each(seed.dataset,function(key, value){
   if(ignoreAll || this[col] == slice){
      table += '<tr>';
      table += '<td>'+this.escalationID+'</td>';
      table += '<td>'+this.escReasonID+'</td>';
      table += '<td>'+this.reasonText+'</td>';
      table += '<td>'+this.escCreatedBy+'</td>';
      table += '<td>'+this.escCreatorFirst+'</td>';
      table += '<td>'+this.escCreatorLast+'</td>';
      table += '</tr>';
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

I'm adding this in right now to test it but from what I can tell, if ignoreAll is false, none of the data would be included?
if ignoreAll is false it will check this[col] == slice condition because it is in OR mode and not AND mode. if second conditon is true then it will add row to table.
0
$.each(seed.dataset,function(key, value){
      if (source != 'slice'  ||  this[col] == slice){
           table += '<tr>';
           // etc.

Alternatively:

$.each(seed.dataset,function(key, value){
      if (source == 'all'  ||  this[col] == slice){
           table += '<tr>';
           // etc.

Comments

0

Check out the below code.

If the current item is a slice, it's checked to make sure it is the correct slice. If it is, getSlice() is called and the $.each() loop breaks to avoid unnecessary iterations.

function getSlice(that) {
    var table = '';
    table += '<tr>';
    table += '<td>' + that.escalationID + '</td>';
    table += '<td>' + that.escReasonID + '</td>';
    table += '<td>' + that.reasonText + '</td>';
    table += '<td>' + that.escCreatedBy + '</td>';
    table += '<td>' + that.escCreatorFirst + '</td>';
    table += '<td>' + that.escCreatorLast + '</td>';
    table += '</tr>';
    return table;
}

// Loop over our data
$.each(seed.dataset, function (key, value) {
    table += source == 'slice' ? (this[col] == slice ? true & getSlice(this) : false) : getSlice(this);
    if (table === true) return false; // break out of loop
});

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.