I need to select all td’s within my table with a drag event. What I am trying to achieve is to create a date range based on my first selected td, until the last selected td, but they can span over multiple rows.
At the moment, I am using nextUntil() with andSelf() to include the last selected, but it only selects td’s within the current tr.
Below is a sample of my code.
Any help please.
this.BindCalendarMouseDrag = function () {
var isMouseDown = false;
var isHighlighted;
var selectedDays = [];
$(".tabCalendarContainer tr.trCalWeek td")
.mousedown(function () {
isMouseDown = true;
$(this).addClass("highlighted");
isHighlighted = $(this).hasClass("highlighted");
selectedDays.push($(this));
return false; // prevent text selection
})
.mouseover(function () {
if (isMouseDown) {
$(this).addClass("highlighted", isHighlighted);
var firstSelectedDay = selectedDays[0];
firstSelectedDay.nextUntil($(this)).andSelf().add($(this)).addClass("highlighted", isHighlighted);
selectedDays.push($(this));
}
})
.bind("selectstart", function () {
return false;
});
$(document).mouseup(function () {
isMouseDown = false;
//alert(selectedDays.length);
});
};
I took the liberty of constructing a fiddle that illustrates a calendar with draggable weekdays. It doesn’t cater for BACKWARDS dragging yet, but you should be able to easily adjust the code to accommodate it.