【问题标题】:Dragging on html table cells拖动 html 表格单元格
【发布时间】:2013-07-17 10:05:18
【问题描述】:

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 类。

【问题讨论】:

标签: javascript jquery html web


【解决方案1】:

你的桌子有几个问题,但我会更正你要求的那个。
要在鼠标移出容器时让表格滚动,请将此代码添加到 mousedown 事件处理程序中:

$('body').on('mousemove', function(e){
    div = $('#divScroll');      
    if(e.pageY > div.height() && (e.pageY - div.height()) > div.scrollTop()) {
        div.scrollTop(e.pageY - div.height());
    }
});

还有这个,在您的 mouseup 事件处理程序中:

$('body').off('mousemove');

updated Fiddle

但是现在,另一个问题出现了。这是因为您的其余代码。由于鼠标离开了列,因此未选择行。

【讨论】:

  • 感谢您的回复....单元格选择在学生姓名列上向上和向下方向..我应该怎么做
【解决方案2】:

试着去掉里面的return false;

$('#contentPlaceHolderMain_tableAppointment tr').eq(rowIndex).find('td').eq(colIndex).addClass('csstdhighlight');
    flag = true;
    return false; //Remove this line
} 

因为return false; 停止浏览器默认行为(自动滚动)。

DEMO

【讨论】:

  • Khanh 我必须在学生姓名列上向上和向下方向...而不是第一和第二列...\
【解决方案3】:

更新 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/

【讨论】:

猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
相关资源
最近更新 更多