jQuery嵌套可排序 - 无法移动嵌套的LI元素

时间:2011-08-28 11:58:09

标签: javascript jquery jquery-plugins nested-sortable interspire-shopping-cart

我很难过。我一直试图解决这个问题2天,并在网上查看,包括这个网站。这非常令人沮丧,如果你能解决它,我会非常感动。

我使用的是Interspire购物车,它使用jQuery插件nestedsortables(http://code.google.com/p/nestedsortables/wiki/NestedSortableDocumentation)通过拖放操作来安排管理面板中页面的顺序。下面是一个DIFFERENT脚本的演示,它执行相同的操作:http://mjsarfatti.com/sandbox/nestedSortable/

问题是父页面(在列表中,而不是实际页面)能够被拖动到任何顺序,包括嵌入的子位置和退出(直到刷新,然后它被卡在子位置)。无法拖动/移动子页面,光标会像父页面一样变为4向箭头。

我已经超过了HTML这么多次我失去了数量。对于父母和孩子来说,HTML看起来几乎相同,我可以弄清楚为什么一个人会工作,而一个人不会。我想也许它与ul或li标签有关,但我只是没有看到问题。

我将我的代码与Interspire演示(http://shoppingcart.interspire-demo.com/demos)的代码进行了比较,但我没有看到任何差异。他们的管理面板工作,我的没有。我尝试重新上传控制此功能的2个JavaScript文件,但问题仍然存在。

以下是HTML代码段:

<table class="GridPanel SortablePanel" cellspacing="0" cellpadding="0" border="0" style="width: 100%; margin-top: 10px;">
<tbody>
<tr class="Heading3">
<td width="1" style="padding-left: 5px;">
<input type="checkbox" style="vertical-align: middle;" onclick="ToggleDeleteBoxes(this.checked)">
</td>
<td> Page Name &nbsp; </td>
<td width="120"> Page Type &nbsp; </td>
<td width="80" align="center"> Visible &nbsp; </td>
<td width="80"> Action </td>
</tr>
</tbody>
</table>
<ul id="PageList" class="SortableList">
<li id="ele-5" class="SortableRow"></li> << **collapsed**
<li id="ele-3" class="SortableRow"></li> << **collapsed**
<li id="ele-11" class="SortableRow">
<table class="GridPanel" cellspacing="0" cellpadding="0" border="0" style="width: 100%;">
<tbody>
<tr class="GridRow" onmouseout="this.className='GridRow'" onmouseover="this.className='GridRowOver'">
<td width="1">
<input type="checkbox" value="11" name="page[]">
</td>
<td width="150" style="display: none;"> </td>
<td class="DragMouseDown sort-handle " style="-moz-user-select: none;"> PARENT </td>
<td class="HideOnDrag " width="120">Normal Page</td>
<td class="HideOnDrag " width="80" align="center">
<a href="index.php?ToDo=editPageVisibility&pageId=11&visible=1" title="Click here to make this page visible on your web site">
<img border="0" src="images/cross.gif">
</a>
</td>
<td class="HideOnDrag" width="80">
<a href="javascript:PreviewPage(11)" title="Preview Page">Preview</a>
<a href="index.php?ToDo=editPage&pageId=11" title="Edit this page">Edit</a>
</td>
</tr>
</tbody>
</table>
<ul class="SortableList" style="padding-left: 30px; padding-right: 0px;">
<li id="ele-12" class="SortableRow">
<table class="GridPanel" cellspacing="0" cellpadding="0" border="0" style="width: 100%;">
<tbody>
<tr class="GridRow" onmouseout="this.className='GridRow'" onmouseover="this.className='GridRowOver'">
<td width="1">
<input type="checkbox" value="12" name="page[]">
</td>
<td width="150" style="display: none;"> </td>
<td class="DragMouseDown sort-handle " style="-moz-user-select: none;"> CHILD ONE </td>
<td class="HideOnDrag " width="120">Normal Page</td>
<td class="HideOnDrag " width="80" align="center">
<a href="index.php?ToDo=editPageVisibility&pageId=12&visible=1" title="Click here to make this page visible on your web site">
<img border="0" src="images/cross.gif">
</a>
</td>
<td class="HideOnDrag" width="80">
<a href="javascript:PreviewPage(12)" title="Preview Page">Preview</a>
<a href="index.php?ToDo=editPage&pageId=12" title="Edit this page">Edit</a>
</td>
</tr>
</tbody>
</table>
</li>
<li id="ele-13" class="SortableRow">
<table class="GridPanel" cellspacing="0" cellpadding="0" border="0" style="width: 100%;">
<tbody>
<tr class="GridRow" onmouseout="this.className='GridRow'" onmouseover="this.className='GridRowOver'">
<td width="1">
<input type="checkbox" value="13" name="page[]">
</td>
<td width="150" style="display: none;"> </td>
<td class="DragMouseDown sort-handle " style="-moz-user-select: none;"> CHILD TWO </td>
<td class="HideOnDrag " width="120">Normal Page</td>
<td class="HideOnDrag " width="80" align="center">
<a href="index.php?ToDo=editPageVisibility&pageId=13&visible=1" title="Click here to make this page visible on your web site">
<img border="0" src="images/cross.gif">
</a>
</td>
<td class="HideOnDrag" width="80">
<a href="javascript:PreviewPage(13)" title="Preview Page">Preview</a>
<a href="index.php?ToDo=editPage&pageId=13" title="Edit this page">Edit</a>
</td>
</tr>
</tbody>
</table>
</li>
</ul>
</li>
</ul>
</div>
<div id="div1" style="display: none;">
</div>
</td>
</tr>
</tbody>
</table>

我将页面命名为PARENT,CHILD ONE和CHILD TWO。这两个子页面嵌套在父级中。父是可拖动的,但是2个嵌套页面不是。

以下是NestedSortable jQuery插件代码供您参考:

jQuery.iNestedSortable = {
    checkHover: function (e, o) {
        if (e.isNestedSortable) {
            jQuery.iNestedSortable.scroll(e);
            return jQuery.iNestedSortable.newCheckHover(e)
        } else {
            return jQuery.iNestedSortable.oldCheckHover(e, o)
        }
    },
    oldCheckHover: jQuery.iSort.checkhover,
    newCheckHover: function (e) {
        if (!jQuery.iDrag.dragged) {
            return
        }
        if (!(e.dropCfg.el.size() > 0)) {
            return
        }
        if (!e.nestedSortCfg.remeasured) {
            jQuery.iSort.measure(e);
            e.nestedSortCfg.remeasured = true
        }
        var a = jQuery.iNestedSortable.findPrecedingItem(e);
        var b = jQuery.iNestedSortable.shouldNestItem(e, a);
        var c = (!a) ? jQuery.iNestedSortable.isTouchingFirstItem(e) : false;
        var d = false;
        if (a) {
            if (e.nestedSortCfg.lastPrecedingItem === a && e.nestedSortCfg.lastShouldNest === b) {
                d = true
            }
        } else if (e.nestedSortCfg.lastPrecedingItem === a && e.nestedSortCfg.lastTouchingFirst === c) {
            d = true
        }
        e.nestedSortCfg.lastPrecedingItem = a;
        e.nestedSortCfg.lastShouldNest = b;
        e.nestedSortCfg.lastTouchingFirst = c;
        if (d) {
            return
        }
        if (a !== null) {
            if (b) {
                jQuery.iNestedSortable.nestItem(e, a)
            } else {
                jQuery.iNestedSortable.appendItem(e, a)
            }
        } else if (c) {
            jQuery.iNestedSortable.insertOnTop(e)
        }
    },
    scroll: function (e) {
        if (!e.nestedSortCfg.autoScroll) {
            return false
        }
        var a = e.nestedSortCfg.scrollSensitivity;
        var b = e.nestedSortCfg.scrollSpeed;
        var c = jQuery.iDrag.dragged.dragCfg.currentPointer;
        var d = jQuery.iUtil.getScroll();
        if ((c.y - d.ih) - d.t > -a) {
            window.scrollBy(0, b)
        }
        if (c.y - d.t < a) {
            window.scrollBy(0, -b)
        }
    },
    check: function (a) {
        jQuery.iNestedSortable.newCheck(a);
        return jQuery.iNestedSortable.oldCheck(a)
    },
    oldCheck: jQuery.iSort.check,
    newCheck: function (a) {
        if (jQuery.iNestedSortable.latestNestingClass && jQuery.iNestedSortable.currentNesting) {
            jQuery.iNestedSortable.currentNesting.removeClass(jQuery.iNestedSortable.latestNestingClass);
            jQuery.iNestedSortable.currentNesting = null;
            jQuery.iNestedSortable.latestNestingClass = ""
        }
        if (jQuery.iDrop.overzone.isNestedSortable) {
            jQuery.iDrop.overzone.nestedSortCfg.remeasured = false
        }
    },
    serialize: function (s) {
        if (jQuery('#' + s).get(0).isNestedSortable) {
            return jQuery.iNestedSortable.newSerialize(s)
        } else {
            return jQuery.iNestedSortable.oldSerialize(s)
        }
    },
    oldSerialize: jQuery.iSort.serialize,
    newSerialize: function (s) {
        var i;
        var h = '';
        var j = '';
        var o = {};
        var e;
        var k = function (f) {
                var g = [];
                thisChildren = jQuery(f).children('.' + jQuery.iSort.collected[s]);
                thisChildren.each(function (i) {
                    var a = jQuery.attr(this, 'id');
                    if (a && a.match) {
                        a = a.match(e.nestedSortCfg.serializeRegExp)[0]
                    }
                    if (h.length > 0) {
                        h += '&'
                    }
                    h += s + j + '[' + i + '][id]=' + a;
                    g[i] = {
                        id: a
                    };
                    var b = jQuery(this).children(e.nestedSortCfg.nestingTag + "." + e.nestedSortCfg.nestingTagClass.split(" ").join(".")).get(0);
                    var c = j;
                    j += '[' + i + '][children]';
                    var d = k(b);
                    if (d.length > 0) {
                        g[i].children = d
                    }
                    j = c
                });
                return g
            };
        if (s) {
            if (jQuery.iSort.collected[s]) {
                e = jQuery('#' + s).get(0);
                o[s] = k(e)
            } else {
                for (a in s) {
                    if (jQuery.iSort.collected[s[a]]) {
                        e = jQuery('#' + s[a]).get(0);
                        o[s[a]] = k(e)
                    }
                }
            }
        } else {
            for (i in jQuery.iSort.collected) {
                e = jQuery('#' + i).get(0);
                o[i] = k(e)
            }
        }
        return {
            hash: h,
            o: o
        }
    },
    findPrecedingItem: function (e) {
        var d = 0;
        var f = jQuery.grep(e.dropCfg.el, function (i) {
            var a = (i.pos.y < jQuery.iDrag.dragged.dragCfg.ny) && (i.pos.y > d);
            if (!a) {
                return false
            }
            var b;
            if (e.nestedSortCfg.rightToLeft) {
                b = (i.pos.x + i.pos.wb + e.nestedSortCfg.snapTolerance > jQuery.iDrag.dragged.dragCfg.nx + jQuery.iDrag.dragged.dragCfg.oC.wb)
            } else {
                b = (i.pos.x - e.nestedSortCfg.snapTolerance < jQuery.iDrag.dragged.dragCfg.nx)
            }
            if (!b) {
                return false
            }
            var c = jQuery.iNestedSortable.isBeingDragged(e, i);
            if (c) {
                return false
            }
            d = i.pos.y;
            return true
        });
        if (f.length > 0) {
            return f[(f.length - 1)]
        } else {
            return null
        }
    },
    isTouchingFirstItem: function (e) {
        var c;
        var d = jQuery.grep(e.dropCfg.el, function (i) {
            var a = (c === undefined || i.pos.y < c);
            if (!a) {
                return false
            }
            var b = jQuery.iNestedSortable.isBeingDragged(e, i);
            if (b) {
                return false
            }
            c = i.pos.y;
            return true
        });
        if (d.length > 0) {
            d = d[(d.length - 1)];
            return d.pos.y < jQuery.iDrag.dragged.dragCfg.ny + jQuery.iDrag.dragged.dragCfg.oC.hb && d.pos.y > jQuery.iDrag.dragged.dragCfg.ny
        } else {
            return false
        }
    },
    isBeingDragged: function (e, a) {
        var b = jQuery.iDrag.dragged;
        if (!b) {
            return false
        }
        if (a == b) {
            return true
        }
        if (jQuery(a).parents("." + e.sortCfg.accept.split(" ").join(".")).filter(function () {
            return this == b
        }).length !== 0) {
            return true
        } else {
            return false
        }
    },
    shouldNestItem: function (e, a) {
        if (!a) {
            return false
        }
        if (e.nestedSortCfg.noNestingClass && jQuery(a).filter("." + e.nestedSortCfg.noNestingClass).get(0) === a) {
            return false
        }
        if (e.nestedSortCfg.rightToLeft) {
            return a.pos.x + a.pos.wb - (e.nestedSortCfg.nestingPxSpace - e.nestedSortCfg.snapTolerance) > jQuery.iDrag.dragged.dragCfg.nx + jQuery.iDrag.dragged.dragCfg.oC.wb
        } else {
            return a.pos.x + (e.nestedSortCfg.nestingPxSpace - e.nestedSortCfg.snapTolerance) < jQuery.iDrag.dragged.dragCfg.nx
        }
    },
    nestItem: function (e, a) {
        var b = jQuery(a).children(e.nestedSortCfg.nestingTag + "." + e.nestedSortCfg.nestingTagClass.split(" ").join("."));
        var c = jQuery.iSort.helper;
        styleHelper = c.get(0).style;
        styleHelper.width = 'auto';
        if (!b.size()) {
            var d = "<" + e.nestedSortCfg.nestingTag + " class='" + e.nestedSortCfg.nestingTagClass + "'></" + e.nestedSortCfg.nestingTag + ">";
            b = jQuery(a).append(d).children(e.nestedSortCfg.nestingTag).css(e.nestedSortCfg.styleToAttach)
        }
        jQuery.iNestedSortable.updateCurrentNestingClass(e, b);
        jQuery.iNestedSortable.beforeHelperRemove(e);
        b.prepend(c.get(0));
        jQuery.iNestedSortable.afterHelperInsert(e)
    },
    appendItem: function (e, a) {
        jQuery.iNestedSortable.updateCurrentNestingClass(e, jQuery(a).parent());
        jQuery.iNestedSortable.beforeHelperRemove(e);
        jQuery(a).after(jQuery.iSort.helper.get(0));
        jQuery.iNestedSortable.afterHelperInsert(e)
    },
    insertOnTop: function (e) {
        jQuery.iNestedSortable.updateCurrentNestingClass(e, e);
        jQuery.iNestedSortable.beforeHelperRemove(e);
        jQuery(e).prepend(jQuery.iSort.helper.get(0));
        jQuery.iNestedSortable.afterHelperInsert(e)
    },
    beforeHelperRemove: function (e) {
        var a = jQuery.iSort.helper.parent(e.nestedSortCfg.nestingTag + "." + e.nestedSortCfg.nestingTagClass.split(" ").join("."));
        var b = a.children("." + e.sortCfg.accept.split(" ").join(".") + ":visible").size();
        if (b === 0 && a.get(0) !== e) {
            a.hide()
        }
    },
    afterHelperInsert: function (e) {
        var a = jQuery.iSort.helper.parent();
        if (a.get(0) !== e) {
            a.show()
        }
        e.nestedSortCfg.remeasured = false
    },
    updateCurrentNestingClass: function (e, a) {
        var b = jQuery(a);
        if ((e.nestedSortCfg.currentNestingClass) && (!jQuery.iNestedSortable.currentNesting || b.get(0) != jQuery.iNestedSortable.currentNesting.get(0))) {
            if (jQuery.iNestedSortable.currentNesting) {
                jQuery.iNestedSortable.currentNesting.removeClass(e.nestedSortCfg.currentNestingClass)
            }
            if (b.get(0) != e) {
                jQuery.iNestedSortable.currentNesting = b;
                b.addClass(e.nestedSortCfg.currentNestingClass);
                jQuery.iNestedSortable.latestNestingClass = e.nestedSortCfg.currentNestingClass
            } else {
                jQuery.iNestedSortable.currentNesting = null;
                jQuery.iNestedSortable.latestNestingClass = ""
            }
        }
    },
    destroy: function () {
        return this.each(function () {
            if (this.isNestedSortable) {
                this.nestedSortCfg = null;
                this.isNestedSortable = null;
                jQuery(this).SortableDestroy()
            }
        })
    },
    build: function (a) {
        if (a.accept && jQuery.iUtil && jQuery.iDrag && jQuery.iDrop && jQuery.iSort) {
            this.each(function () {
                this.isNestedSortable = true;
                this.nestedSortCfg = {
                    noNestingClass: a.noNestingClass ? a.noNestingClass : false,
                    rightToLeft: a.rightToLeft ? true : false,
                    nestingPxSpace: parseInt(a.nestingPxSpace, 10) || 30,
                    currentNestingClass: a.currentNestingClass ? a.currentNestingClass : "",
                    nestingLimit: a.nestingLimit ? a.nestingLimit : false,
                    autoScroll: a.autoScroll !== undefined ? a.autoScroll == true : true,
                    scrollSensitivity: a.scrollSensitivity ? a.scrollSensitivity : 20,
                    scrollSpeed: a.scrollSpeed ? a.scrollSpeed : 20,
                    serializeRegExp: a.serializeRegExp ? a.serializeRegExp : /[^\-]*$/
                };
                this.nestedSortCfg.snapTolerance = parseInt(this.nestedSortCfg.nestingPxSpace * 0.4, 10);
                this.nestedSortCfg.nestingTag = this.tagName;
                this.nestedSortCfg.nestingTagClass = this.className;
                this.nestedSortCfg.styleToAttach = (this.nestedSortCfg.rightToLeft) ? {
                    "padding-left": 0,
                    "padding-right": this.nestedSortCfg.nestingPxSpace + 'px'
                } : {
                    "padding-left": this.nestedSortCfg.nestingPxSpace + 'px',
                    "padding-right": 0
                };
                jQuery(this.nestedSortCfg.nestingTag, this).css(this.nestedSortCfg.styleToAttach)
            });
            jQuery.iSort.checkhover = jQuery.iNestedSortable.checkHover;
            jQuery.iSort.check = jQuery.iNestedSortable.check;
            jQuery.iSort.serialize = jQuery.iNestedSortable.serialize
        }
        return this.Sortable(a)
    }
};
jQuery.fn.extend({
    NestedSortable: jQuery.iNestedSortable.build,
    NestedSortableDestroy: jQuery.iNestedSortable.destroy
});
jQuery.iUtil.getScroll = function (e) {
    var t, l, w, h, iw, ih;
    if (e && e.nodeName.toLowerCase() != 'body') {
        t = e.scrollTop;
        l = e.scrollLeft;
        w = e.scrollWidth;
        h = e.scrollHeight;
        iw = 0;
        ih = 0
    } else {
        if (document.documentElement && document.documentElement.scrollTop) {
            t = document.documentElement.scrollTop;
            l = document.documentElement.scrollLeft;
            w = document.documentElement.scrollWidth;
            h = document.documentElement.scrollHeight
        } else if (document.body) {
            t = document.body.scrollTop;
            l = document.body.scrollLeft;
            w = document.body.scrollWidth;
            h = document.body.scrollHeight
        }
        iw = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0;
        ih = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0
    }
    return {
        t: t,
        l: l,
        w: w,
        h: h,
        iw: iw,
        ih: ih
    }

请帮帮我。我相信其他Interspire和NestedSortable用户也会觉得这很有用。感谢您的关注,我非常感谢任何想要解决这个问题的人,无论您是否成功。     };

1 个答案:

答案 0 :(得分:1)

在花了3天半的时间尝试调试这个没有成功之后,我决定尝试使用mjsarfatti.com中的jQuery插件而不是来自code.google.com的插件。

使用Misarfatti的插件无法立即使用,因为他确实有必须配置的设置才能使其正常工作,否则会冻结您的浏览器。我认为这是值得一试的,然后花几个小时从私人管理面板获取所有代码并将其设置为在jsfiddle中工作。即使我这样做了,这个问题在这里几乎没有兴趣,也许永远不会得到解决。

这是我一年多来最大的编码噩梦,所以我想我会在这里发布解决方案以防其他人遇到同样的问题。我讨厌有人经历我用它做的事情。我可以根据要求发布完整的代码,虽然这很简单。