但是当我进入我的容器页面并将内容页面通过ajax加载到一个div中时,拖放功能停止工作。内容页面内的所有其他JavaScript功能仍然有效。如何将jquery dnd插件绑定到通过ajax加载的表格上?
我正在使用这个教程进行拖放:http://isocra.com/2008/02/table-drag-and-drop-jquery-plugin/ 我的代码看起来像这样:
$(window).load(function()
{ if(temp == 0)
{
DP("eerste keer")
load_table();
temp = 1;
}
} );
function load_table()
{
DP('load_table');
$.ajax({
//async: false,
type: "POST",
url: "/diagnose_hoofdpagina/table_diagnose/" + DosierID, // <== loads requested page
success: function (data) {
$("#diagnoses_zelf").html(''); //<== clears current content of div
$("#diagnoses_zelf").append(data).trigger('create'); // <== appends requested page
},
error: function(){
alert('error');
}
}).done(function() {
update_table();
initialize_table(); // <== calls jquery plug in
});
return false;
}
function initialize_table()
{
var tableid = $('#diagnoses_zelf table').attr('id'); //< this finds the correct table thanks to Fábio Batista => this option worked, rest didn't
alert(tableid);
$(tableid).tableDnD({
onDrop: function(table, row) {
alert(table + " " + row);
},
onDragStart: function(table,row){
var tette = $(row).index;
alert(tette);
},
dragHandle: ".dragHandle"
});
}
这怎么可能?我该怎么办呢? 请有人帮帮我。
简短地说:
我想要访问通过ajax加载到我的容器页面中的表格的ID,并在其上使用jquery拖放插件。
编辑
发现:
一些奇怪的事情发生了,我的容器页面中的表格被重命名为pSqlaTable而不是我在控制器页面中给它指定的ID。
<table id="tableDiagnose" class="table table-hover">
这就是代码无法再找到表的原因。感谢 Fábio Batista 写的这段代码,问题已经得到解决:
$('#diagnoses_zelf table').tableDnD( ... );
但是我现在该如何使用dnd插件呢?
它现在可以找到表格了,但我仍然无法将dnd插件绑定到它上面,我能否将jquery插件绑定到ajax加载的表格上?
编辑
//drag & drop http://isocra.com/2008/02/table-drag-and-drop-jquery-plugin/
function initialize_table()
{
var tableid = $('#diagnoses_zelf table').attr('id');
alert(tableid);
$('#' + tableid).tableDnD({
onDrop: function(table, row) {
alert(table + " " + row);
},
onDragStart: function(table,row){
alert('issemer?');
},
dragHandle: ".dragHandle"
});
}
这是我仍然困扰的代码。tableid是正确的,但jquery的初始化不正确。我无法拖动表格中的行。我的语法错了吗?
编辑
可能是因为我使用ZPT(或JavaScript)在其他页面上动态生成表格,所以无法将jquery绑定到表格上?
console.log(data)
并展示给我们看呢? - Kevin Schmid