简体   繁体   中英

Can I add if or else condition in jquery json response

My Jquery JSON response is adding to data table, but I have to use if conditions on day wise. So how I can use.Please suggest an example. Here I have to use condition in data.hours.day field. My code is:

if (data.hours != null) {
    var h = $('#hours').DataTable();
    $('#hours').dataTable().fnClearTable();
    for (var i = 0; i < data.hours.length; i++) {
        h.row.add([
           data.hours[i].day,
           data.hours[i].open_hr_delivery + " - " + data.hours[i].close_hr_delivery + " AND " + data.hours[i].open_hr_delivery1 + " - " + data.hours[i].close_hr_delivery1,
            "<a href=\"dspedithour?hourID=" + data.hours[i].dspbusinessmasterid + "\" class=\"btn btn-xs font-blue\"><i class=\"fa fa-edit\"></i> Update </a>", 
         ])
       .draw();

    }
}

Often it is easier to create variables that you can then pass to the array

     for (var i = 0; i < data.hours.length; i++) {
            // example variable passed to array
            var deliveryHours = data.hours[i].open_hr_delivery + 
                " - " + 
                data.hours[i].close_hr_delivery +
                " AND " + data.hours[i].open_hr_delivery1 + 
                " - " + data.hours[i].close_hr_delivery1


            h.row.add([
            data.hours[i].day,
            deliveryHours,//variable from above
                "<a href=\"dspedithour?hourID=" + data.hours[i].dspbusinessmasterid + "\" class=\"btn btn-xs font-blue\"><i class=\"fa fa-edit\"></i> Update </a>", ]).draw();

        }

Using the example variable created above you should be able to do the same thing for whatever condition you need to include

we can use variable to display day.

                if(data.hours != null){
                var h = $('#hours').DataTable();
                $('#hours').dataTable().fnClearTable();
                for ( var i = 0; i < data.hours.length; i++ ) {
                        var dayinword = null;
                        if(data.hours[i].day == 0)
                        {dayinword = "Sunday";}
                        else if(data.hours[i].day == 1)
                        {dayinword = "Monday";}
                        else if(data.hours[i].day == 2)
                        {dayinword = "Thuesday";}
                        else if(data.hours[i].day == 3)
                        {dayinword = "Wednesday";}
                        else if(data.hours[i].day == 4)
                        {dayinword = "Thursday";}
                        else if(data.hours[i].day == 5)
                        {dayinword = "Friday";}
                        else if(data.hours[i].day == 6)
                        {dayinword = "Saturday";}

                     h.row.add( [
                                    dayinword,
                                    data.hours[i].open_hr_delivery+" - "+data.hours[i].close_hr_delivery+" AND "+data.hours[i].open_hr_delivery1+" - "+data.hours[i].close_hr_delivery1,
                                    "<a href=\"dspedithour?hourID="+data.hours[i].dspbusinessmasterid+"\" class=\"btn btn-xs font-blue\"><i class=\"fa fa-edit\"></i> Update </a>",
                                ] ).draw();

                    }
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM