javascript - 拖动 html 表格单元格

标签 javascript jquery html web

Fiddle

$(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;
    }
});

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

我已经选择了 tr 类。

最佳答案

更新的 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;
}

然后像这样用 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');
}

我刚刚在 HTML 中添加了一个类。除了我在此处显示的内容外,所有 HTML 和 CSS 均未更改。

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

关于javascript - 拖动 html 表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17723747/

相关文章:

javascript - 如何检测文档是否回到焦点?

javascript - 单击后 HTML 按钮不会取消选择

javascript - 根据innerText更改HTML表格单元格值

javascript - Asp.net Bower安装plotly.js不起作用

javascript - Vue v-model 在使用 v-for 时不更新数组值

javascript - jQuery/JS – 单击博客图像滚动到下一个博客元素

javascript - 尝试使文本居中时动画消失

javascript - NodeJS 和 MongoDB。 POST 请求获取 404 代码

javascript - 如何让.post打开一个url而不是生成html?

javascript - 如何防止其元素内的链接默认?