我写了过滤日期的项目列表的JavaScript/jQuery UI的。
一个<li class="list-group-item listItem" id="0001">
- 一个项目。
每个<input type="hidden" id="01_0001" class="form-control dateItem dataField_0001" value="11/23/2012">
为每个项目日期。
任务是显示项目,如果它至少有一个日期是在datepicker的范围内。 如果没有一个项目的日期 - 它需要隐藏。
因此,一切正常,但速度很慢。
大约有300个项目和2000个以上的日期,当我的脚本解析数据需要30秒以上。
问题:有什么方法可以优化我的代码,或者我应该使用其他任何方法来完成此任务吗?
请在开机后看小提琴。
最好的问候, 亚历克斯
想到的/* Create dates +/- 7 for each "from" and "to" fields */
var prevWeek = new Date();
prevWeek.setDate(prevWeek.getDate() - 7);
var prevMonth = (prevWeek.getMonth() + 1);
var prevDay = prevWeek.getDate();
var prevYear = prevWeek.getFullYear();
var prevWeekDate = ((prevMonth < 10 ? '0' : '') + prevMonth + "/" + (prevDay < 10 ? '0' : '') + prevDay + "/" + prevYear);
/*console.log('prevWeekDate =' + prevWeekDate);*/
var nextWeek = new Date();
nextWeek.setDate(nextWeek.getDate() + 7);
var nextMonth = (nextWeek.getMonth() + 1);
var nextDay = nextWeek.getDate();
var nextYear = nextWeek.getFullYear();
var nextWeekDate = ((nextMonth < 10 ? '0' : '') + nextMonth + "/" + (nextDay < 10 ? '0' : '') + nextDay + "/" + nextYear);
/*console.log('nextWeekDate =' + nextWeekDate); */
/*function - datepicker setup*/
$(function() {
$("#from").datepicker({
defaultDate: "-1w",
changeMonth: true,
numberOfMonths: 1,
onClose: function(selectedDate) {
$("#to").datepicker("option", "minDate", selectedDate);
//console.log (selectedDate);
filterDates();
} });
$("#to").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
onClose: function(selectedDate) {
$("#from").datepicker("option", "maxDate", selectedDate);
//console.log (selectedDate);
filterDates();
} });
});
/*parse date string to array*/
function dateToArray(date) {
var dateArray = date.split('/');
return dateArray;
}
function filterDates() {
var from = dateToArray($('#from').val());
//console.log (from);
var from = new Date(parseInt(from[2], 10),
parseInt(from[0], 10) - 1,
parseInt(from[1], 10));
//console.log (from);
var to = dateToArray($('#to').val());
//console.log (to);
var to = new Date( parseInt(to[2], 10),
parseInt(to[0], 10) - 1,
parseInt(to[1], 10));
//console.log (to);
$('.block').each(function() {
var itemId = $(this).attr('class');
var itemId = itemId.split('_');
var itemId = itemId[1];
var displayBlock = false;
$('.dataField_'+ itemId).each(function() {
var inputValue = $(this).attr('value');
var inputId = $(this).attr('id');
//console.log (inputValue + ' ' +inputId);
var testField = ($(this).attr('value')).split('/');
console.log (testField);
var testField = new Date( parseInt(testField[2], 10),
parseInt(testField[0], 10) - 1,
parseInt(testField[1], 10));
// console.log (testField);
var result = (testField < from || testField > to);
if (!result) { displayBlock = true; }
});
if (displayBlock) {
$('.listItem#' + itemId).removeClass('hideItem');
} else {
$('.listItem#' + itemId).addClass('hideItem');
}
})
}
$(document).ready(function(){
$("#checkAllBox").click(function() {
if ($("#checkAllBox").prop('checked')) {
$(".checkBoxItem").prop("checked", true);
} else {
$(".checkBoxItem").prop("checked", false);
}
});
$("#from").val(prevWeekDate);
$("#to").val(nextWeekDate);
$("#dialog").dialog({
autoOpen: false,
width: 600,
position:['middle',120],
});
$(".startDialog").click(function() {
$("#dialog").dialog("open");
});
filterDates();
$('.listItem').click(function(){
var itemId = $(this).attr('id');
console.log (itemId);
});
})
请在此处发布您的代码,而不仅仅是小提琴的链接。 – Barmar
经验法则是,如果您在函数中多次发现$(this),请缓存该对象以减少额外的函数调用,以创建一个您已经创建并扔掉的新jQuery对象。与任何重复选择符一样var $ this = $(this)'...'$ this.doSomething()'..also ....'this.id&this.value'比'attr() '因为它们是本地属性。这些不会对你的代码有很大的改进,但是有点帮助 – charlietfl
感谢这一点。我将更多地了解缓存。 – A1exandr