在html表格单元格上拖动

Joh*_*ohn 13 html javascript jquery web

小提琴

$(document).live('mouseup', function () {
    flag = false;
});

var colIndex;
var lastRow;

$(document).on('mousedown', '.csstablelisttd', function (e) {
    //This line gets the index of the first clicked row.
    lastRow = $(this).closest("tr")[0].rowIndex;

    var rowIndex = $(this).closest("tr").index();
    colIndex = $(e.target).closest('td').index();
    $(".csstdhighlight").removeClass("csstdhighlight");
    if (colIndex == 0 || colIndex == 1) //)0 FOR FULL TIME CELL AND 1 FOR TIME SLOT CELL. 
    return;
    if ($('#contentPlaceHolderMain_tableAppointment tr').eq(rowIndex).find('td').eq(colIndex).hasClass('csstdred') == false) {
        $('#contentPlaceHolderMain_tableAppointment tr').eq(rowIndex).find('td').eq(colIndex).addClass('csstdhighlight');

        flag = true;
        return false;
    }
});
Run Code Online (Sandbox Code Playgroud)

我正在拖动表格单元格.在拖动(向下移动方向)时,我也必须移动表格滚动.而且我想选择反向(向上方向)的单元格.我该怎么办.

我在tr课上做了选择.

Ben*_*ier 6

更新了jsFiddle:http://jsfiddle.net/qvxBb/2/

禁用正常选择,如下所示:

.myselect {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: moz-none;
    -ms-user-select: none;
    user-select: none;
}
Run Code Online (Sandbox Code Playgroud)

并用这样的javascript处理行选择:

// wether or not we are selecting
var selecting = false;
// the element we want to make selectable
var selectable = '.myselect tr:not(:nth-child(1)) td:nth-child(3)';

$(selectable).mousedown(function () {
    selecting = true;
}).mouseenter(function () {
    if (selecting) {
        $(this).addClass('csstdhighlight');
        fillGaps();
    }
});
$(window).mouseup(function () {
    selecting = false;
}).click(function () {
    $(selectable).removeClass('csstdhighlight');
});

// If you select too fast, js doesn't fire mousenter on all cells. 
// So we fill the missing ones by hand
function fillGaps() {
    min = $('td.csstdhighlight:first').parent().index();
    max = $('td.csstdhighlight:last').parent().index();
    $('.myselect tr:lt('+max+'):gt('+min+') td:nth-child(3)').addClass('csstdhighlight');
}
Run Code Online (Sandbox Code Playgroud)

我刚刚在HTML中添加了一个类.除了我在这里展示的内容之外,所有的HTML和CSS都保持不变.

更新了jsFiddle:http://jsfiddle.net/qvxBb/2/