mercredi 15 juin 2016

How to implement an If else statement within a for loop?

I want to make an if else statement within a for loop. Like I did in the code below. But I'm not sure if I defined the condition correct, because my code isn't working at the moment. I want the option to be displayed if ['show_field']=='TRUE' else I want the option not to be visible in my select list. show_field is a column name in my database.

$("#slctTable").change(function()
        {
            $.getJSON("dropdown_code/get_fields.php?table=" + $(this).val(), success = function(data)
            {
                //fieldList.length = 0;
                optionsFields= "<option selected disabled hidden value=''> Selecteer kolom </option>"
            for(var i = 0; i < data.length; i++)
                {
                    if (data[i]['show_field']=='TRUE') {
                        optionsFields += "<option value='" + data[i]['field_name'] + "'>" + data[i]['field_alias'] + "</option>";
                    } else {
                        //optionsFields += "<option value='" + data[i]['field_name'] + "'>" + data[i]['field_alias'] + "</option>";
                        optionsFields += "<option value='" + data[i]['field_name'] + "'></option>";
                    }


                    //fieldList.push(data[i]['field_name']);
                   // if ([i]['show_field' == TRUE]){
                    //optionsFields += "<option value='" + data[i]['field_name'] + "'>" + data[i]['field_alias'] + "</option>";
                    //};
                }
            $("#slctField").html("");
            $("#slctField").append(optionsFields);
            $("#slctField").change();
            //Toevoegen om lijst/array met velden naar main.js te pushen
            fieldList.length = 0;
            data.forEach(function(field){
                fieldList.push(field);
            })
           // console.debug(fieldList);
        });   
        });

Aucun commentaire:

Enregistrer un commentaire