使用Jquery UI在表上拖放

时间:2013-08-01 19:47:19

标签: jquery jquery-ui

我正在尝试从table1table2制作拖放功能,并希望在用户拖动table2 itme时确保已删除的table1单元格为空。如果table2单元格为空,则我可以删除它,否则它将返回table1

 var dragging =null;

 td = $('.table td');

   $(td).draggable({
       cursor:'pointer',
       snap:$('td:empty')       
   })

  $(td).droppable({
        drop:function(event, ui){
         if($(this).text()=='') {  // $(this).is(':empty:) doesn't work either
             console.log('drop now!')
         }else{
            console.log('not empty') //revert 
          }
      }
  })

我不知道如何将拖动的项目还原回table1以及如何检测已删除的单元格是否已有数据。

有什么帮助吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

.droppable()有一个名为accept的选项,它会接受一个函数作为值。 如果函数返回true,则$(this)被视为vaild drop target。请注意,只有在接受drop时才会调用true函数。

$('td').droppable({
    accept: function(ev) {
        return $(this).is(':not(:empty)');
    },
    drop: function(ev, ui) {
        alert('Drop successful!');

        // Do your stuff
    },
    hoverClass: 'hover'
});

请参阅以下JS小提琴:http://jsfiddle.net/7Xd6n/2/