0

I need to add

if (obj1[i][0] !== d1 && row > -1) continue;

and

if (obj1[i][1] !== d2 && row > -1) continue;

depending on the value of d1 and d2 but I'm not sure what I'm doing wrong.

if statement acts as a filter it will allow only those values which assigned to d1 or d2 e.g if d1=1 it will print only values with 1 and not all values in the table.

$("#session_id, #start_date").on('change', function() {
    var d1 = $( "#session_id" ).val();
    var d2 = $( "#start_date" ).val();

    $("#tbody").empty();


    if(d1 != null || d1 != undefined){
        kl1 = "if (obj1[i][0] !== d1 && row > -1) continue;";
        v(kl1);
    }

    if(d2 != null || d2 != undefined){
        var kl = "if (obj1[i][1] !== d2 && row > -1) continue;"
        v(kl);
    }

    function v(data){
        for (var i = 0; i < obj1.length; i++) {
                data;
            row++;
        var newTr = table.insertRow(-1);
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][1]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][2]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][3]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][4]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][5]));
    }
}
});
3
  • 3
    Why your if statement are in quotes "". And what you expect you happen if the continue is called Commented Mar 29, 2019 at 5:42
  • if statement acts as a filter it will allow only those values which assigned to d1 or d2 e.g if d1=1 it will print only 1 and not all values in table Commented Mar 29, 2019 at 5:44
  • 1
    You have a typo in your first if statement. You should have given parameter kl1 to v function not kl Commented Mar 29, 2019 at 5:46

3 Answers 3

1

I think, this is what you want. The way you're thinking to pass statement to loop actually doesn't work.

We can simplify the following code

d1 != null || d1 != undefined

by using javascript truthy & falsey

See the code below:

$("#session_id, #start_date").on('change', function() {
    var d1 = $( "#session_id" ).val();
    var d2 = $( "#start_date" ).val();

    $("#tbody").empty();

    for (var i = 0; i < obj1.length; i++) {
        if (d1 && obj1[i][0] !== d1 && row > -1) continue;
        if (d2 && obj1[i][1] !== d2 && row > -1) continue;
        row++;
        var newTr = table.insertRow(-1);
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][1]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][2]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][3]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][4]));
        newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][5]));
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Just remove statements out of the values and convert it to Boolean and pass it to the function

if(d1 != null || d1 != undefined){
        kl1 = obj1[i][0] !== d1 && row > -1
        v(kl1);
    }

    if(d2 != null || d2 != undefined){
        var kl = obj1[i][1] !== d2 && row > -1
        v(kl);
    }

    function v(data){
        for (var i = 0; i < obj1.length; i++) {
                if(data) continue;
            row++;
            var newTr = table.insertRow(-1);
            newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][1]));
            newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][2]));
            newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][3]));
            newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][4]));
            newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][5]));
        }
    }

1 Comment

if I remove "" from if statements im getting error Uncaught SyntaxError: Unexpected token if
0

The assignment that you are giving for k1 is string. Thats why instead of passing continue it is passing the whole statement. Please find below the way you can do.

if(d1 != null || d1 != undefined){
    if (obj1[i][0] !== d1 && row > -1) kl = "true";
    v(kl);
}

if(d2 != null || d2 != undefined){
    if (obj1[i][1] !== d2 && row > -1) kl =  "true"
    v(kl);
}

function v(data){
    for (var i = 0; i < obj1.length; i++) {
            if(data=="true") continue
        row++;
    var newTr = table.insertRow(-1);
    newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][1]));
    newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][2]));
    newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][3]));
    newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][4]));
    newTr.insertCell(-1).appendChild(document.createTextNode(obj1[i][5]));
}

2 Comments

if you assign if statement to a variable it give error Uncaught SyntaxError: Unexpected token if
Do it in this way if (obj1[i][0] !== d1 && row > -1) kl = "true";

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.