我的Jquery JSON响应正在添加到数据表中,但我必须在日常情况下使用if条件。所以我该如何使用。请提出一个例子。 这里我必须在data.hours.day字段中使用条件。 我的代码是:
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();
}
}
答案 0 :(得分:0)
通常更容易创建可以传递给数组的变量
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();
}
使用上面创建的示例变量,您应该能够针对需要包含的任何条件执行相同的操作
答案 1 :(得分:0)
我们可以使用变量来显示日期。
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();
}
}