我写了这个函数来处理我表中没有数据的某些日子,但我觉得有很多if
语句不是最好的工作方式,也不是优雅的。这段代码完美无缺,我只是在找出一种更简单的方法。这是我的代码:
var daysArray = [2, 3, 4, 5, 6, 7];
var monday_bool = true;
var tuesday_bool = true;
var wednesday_bool = true;
var thursday_bool = true;
var friday_bool = true;
var saturday_bool = true;
var default_array = [];
//Build array of day #'s based upon selection
for (var p = 0; p < data.days.length; p++) {
default_array.push(data.days[p].day_of_week_Number);
}
//Specific handling for days with no data (either in database or "blackout")
if (data.days.length != daysArray.length) {
for (z = 0; z < data.days.length; z++) {
var not_default_start_time = data.days[z].default_start_time;
var not_default_end_time = data.days[z].default_end_time;
var not_select_start_time = data.days[z].start_time;
var not_select_end_time = data.days[z].end_time;
var not_time_chunk = data.days[z].time_chunk_id;
if ($.inArray(2, default_array) != 0 && monday_bool == true) {
not_select_start_time = 0;
not_select_end_time = 0;
$("#mondayDate").html("----");
$("#mondayStart").html(buid(not_default_start_time, not_default_end_time, not_select_start_time));
$("#mondayEndTime").html(buid(not_default_start_time, not_default_end_time, not_select_end_time));
$("#mondayTimeChunkID").val(not_time_chunk);
if (data.days[z].comment != null) {
var notsubMonday = data.days[z].comment.substring(0, 10);
$("#mondayCommentLink").html(notsubMonday + "...");
}
monday_bool = false;
}
if ($.inArray(3, default_array) != 0 && tuesday_bool == true) {
not_select_start_time = 0;
not_select_end_time = 0;
$("#tuesdayDate").html("----");
$("#tuesdayStart").html(buid(not_default_start_time, not_default_end_time, not_select_start_time));
$("#tuesdayEndTime").html(buid(not_default_start_time, not_default_end_time, not_select_end_time));
$("#tuesdayTimeChunkID").val(not_time_chunk);
if (data.days[z].comment != null) {
var notsubTuesday = data.days[z].comment.substring(0, 10);
$("#tuesdayCommentLink").html(notsubTuesday + "...");
}
tuesday_bool = false;
}
if ($.inArray(4, daysArray) != 0 && wednesday_bool == true) {
not_select_start_time = 0;
not_select_end_time = 0;
$("#wednesdayDate").html("----");
$("#wednesdayStart").html(buid(not_default_start_time, not_default_end_time, not_select_start_time));
$("#wednesdayEndTime").html(buid(not_default_start_time, not_default_end_time, not_select_end_time));
$("#wednesdayTimeChunkID").val(not_time_chunk);
if (data.days[z].comment != null) {
var notsubWednesday = data.days[z].comment.substring(0, 10);
$("#wednesdayCommentLink").html(notsubWednesday + "...");
}
wednesday_bool = false;
}
if ($.inArray(5, daysArray) != 0 && thursday_bool == true) {
not_select_start_time = 0;
not_select_end_time = 0;
$("#thursdayDate").html("----");
$("#thursdayStart").html(buid(not_default_start_time, not_default_end_time, not_select_start_time));
$("#thursdayEndTime").html(buid(not_default_start_time, not_default_end_time, not_select_end_time));
$("#thursdayTimeChunkID").val(not_time_chunk);
if (data.days[z].comment != null) {
var notsubThursday = data.days[z].comment.substring(0, 10);
$("#thursdayCommentLink").html(notsubThursday + "...");
}
thursday_bool = false;
}
if ($.inArray(6, daysArray) != 0 && friday_bool == true) {
not_select_start_time = 0;
not_select_end_time = 0;
$("#fridayDate").html("----");
$("#fridayStart").html(buid(not_default_start_time, not_default_end_time, not_select_start_time));
$("#fridayEndTime").html(buid(not_default_start_time, not_default_end_time, not_select_end_time));
$("#fridayTimeChunkID").val(not_time_chunk);
if (data.days[z].comment != null) {
var notsubFriday = data.days[z].comment.substring(0, 10);
$("#fridayCommentLink").html(notsubFriday + "...");
}
friday_bool = false;
}
if ($.inArray(7, daysArray) != 0 && saturday_bool == true) {
not_select_start_time = 0;
not_select_end_time = 0;
$("#saturdayDate").html("----");
$("#saturdayStart").html(buid(not_default_start_time, not_default_end_time, not_select_start_time));
$("#saturdayEndTime").html(buid(not_default_start_time, not_default_end_time, not_select_end_time));
$("#saturdayTimeChunkID").val(not_time_chunk);
if (data.days[z].comment != null) {
var notsubSaturday = data.days[z].comment.substring(0, 10);
$("#saturdayCommentLink").html(notsubSaturday + "...");
}
saturday_bool = false;
}
}
}
有什么更好的方法可以处理这些数据,并在将数据传入数据时填充正确的字段,而不是使用大量的id
语句?