简体   繁体   English

我可以在jQuery json响应中添加if or else条件吗

[英]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. 我的Jquery JSON响应正在添加到数据表中,但我必须使用if条件。 So how I can use.Please suggest an example. 那么我该如何使用。请举一个例子。 Here I have to use condition in data.hours.day field. 在这里,我必须在data.hours.day字段中使用condition。 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();

                    }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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