拖放子节点
我已在 TreeView 控件中填充了数据。如何使用 JQUERY 从父节点拖动特定子节点并将拖动的子节点拖放到 DIV 或任何部分?我知道在 jquery 中,有“draggable”和“droppable”方法可以实现这一点。但我想拖动一个特定的子节点并将其放下。
或者至少如何使用 jquery 获取特定子节点的文本/id?我希望我可以拖动,如果我可以获取子节点
I had populated data in TreeView control. How will I drag a particular child node from a parent node and drop the dragged one to a DIV or any portion, using JQUERY ? I know in jquery, there are methods "draggable" and "droppable" to make this possible. But I want to drag a particular child node and drop it.
Or atleast How to fetch the text/id of a particular child node using jquery ? I hope I can drag,if I can fetch the child node
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有以下 aspx
添加
以显示删除的节点的文本。
不过,您将需要 jquery.ui 库。
如果您计划拖放一个树节点及其子节点,您可能会从中获得一些乐趣,因为显然树节点被渲染为一个表(对于父节点)和一个包含子节点的 div。没有 div 包装两者。
也许您可以重写 TreeView 派生类中的 RenderContents 并自己递归地处理树节点的渲染,但这听起来只是包装两个元素需要大量工作。
也许更好的选择是使用 JQuery 插件来呈现树列表。
If you have the following aspx
Add
To display the text of the dropped node.
You will need the jquery.ui libraries though.
If you plan to drag and drop a treenode with its child nodes you might be in for some fun as apparently a tree node is rendered as a table (for the parent) and a div with the children in. There's no div wrapping both.
Maybe you could override the RenderContents in a TreeView derived class and recursivly handle the rendering of the treenodes yourself but sounds like a lot of work just to wrap two elements.
Maybe a better choice would be using a JQuery plugin to render your treelist.
好的,我将其添加为另一个答案,因为评论框太小,
要在拖动时隐藏/显示项目,可以使用开始和停止事件。
此示例在拖动时显示/隐藏树节点文本旁边的 +/-(折叠/取消折叠)图标,但您可以轻松修改它以隐藏/显示复选框(如果存在)。
我会尝试看看克隆问题。
-- 编辑 --
显然,克隆问题仅出现在 IE 中,并且是由 与
结合
引起的。 这使得 treeNode 类不仅位于“a”标签上,而且还位于“a”标签上在周围的“td”标签上。因此,通过使用 .treeNode 选择器,draggable 方法会执行两次...... FF 中的情况并非如此。
您可以通过更改“#<%= TreeView1.ClientID%> a.treeNode”中的选择器来解决此问题
因此,使用前面提到的 aspx,您将得到以下脚本。
-- 编辑 --
回答您的评论:
要获取拖动节点的值,您可以使用 javascript 字符串操作将其从 href 属性中过滤出来(treenode 呈现href 属性中的值)。
drop 函数中的 javascript 可能如下所示。 (当然仍然需要对空值进行一些检查)
或者您可以通过继承 Treenode 并通过带有自定义属性的 div 包装每个 Treenode 来使其在客户端更干净一些,您可以在其中放置客户端 id。
你的自定义树节点可能看起来像这样
然后在你的aspx中使用(首先注册你的自定义Web控件程序集)
而不是
你的javascript保持干净整洁
Ok I add this as another answer because the comment box is to small,
To hide/show an item while dragging you can use the start and stop events.
This example shows/hides the +/- (collapse/uncollapse) icon next to the treenode text while dragging but you can easily modify it to hide/show the checkboxes if present.
I'll try to have a look at the clone problem.
-- edit --
Apparently the clone problem is only in IE and is caused by the
In conjunction with
This puts the treeNode class not only on the "a" tag but also on the surrounding "td" tag. So by using the .treeNode selector the draggable method is executed twice ... This is not the case in FF.
You could solve this by just changing the selector in "#<%= TreeView1.ClientID%> a.treeNode"
So with the afore mentioned aspx you would get the following script.
-- edit --
In answer to your comment:
To get the value of the dragged node you can use javascript string manipulation to filter it out of the href attribute (treenode renders the value in the href attr).
Your javascript in the drop function could look like the following. (still need to do some checking for null values ofcourse)
Or you can make it a little cleaner client-side by inheriting Treenode and wrap every Treenode by a div with a custom attribut in which you put your clientside id.
Your custom treenode could look like this
Then in your aspx use (register you custom web control assembly first)
instead of
And your javascript stays nice and clean