【发布时间】:2023-04-07 12:20:01
【问题描述】:
所以我有 2 个 html 页面。 1 用作容器,1 用作内容。 当我使用表格加载内容页面时,我可以使用拖放操作。
但是当我转到我的容器页面并将内容页面加载到带有 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">
这就是为什么代码找不到表 annymore 多亏了 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 的初始化不正确。我不能拖着桌子上的卓尔人。我的语法错了吗?
编辑
难道我无法将 jquery 绑定到表格,因为我使用 ZPT(或 javascript)在其他页面上动态生成表格?
【问题讨论】:
-
.tableDnD 行上的“卡在这里”是什么意思?是否有任何错误打印到 JS 控制台?如果是这样,请包括它。如果没有,您可能是正确的,它找不到 #tableDiagnose 元素,我会检查以确保使用该 ID 正确创建 HTML。
-
没有给出错误,我会检查表是否获得了正确的 id,但这应该不是问题。以防万一。根本不可能将工作控制器包含到容器页面中吗?还是我真的需要把我的 dnd 逻辑放在容器页面上?
-
可以在不破坏 JS 事件的情况下将 HTML(例如你的控制器)嵌入到容器中,但这涉及到使用特殊的 JS 方法(如 .detach 和 .append)after 根据您的解决方案,HTML 已在前端初始化,否则请确保您的 ID 不会更改。我认为,这将是您看到的一个单独的后端错误。 ...至于将“dnd 的逻辑放在容器页面上”,我会考虑将该 JS 模块化为一个共享的 .js 文件,如果您在多个地方需要它,该文件可以根据需要重复使用。
-
我会试试的,谢谢你的意见
-
你能
console.log(data)告诉我们吗?
标签: javascript jquery html ajax dom