动态 jquery ui 可排序块的索引/位置

Index/position of dynamic jquery ui sortable blocks?

本文关键字:索引 位置 排序 jquery ui 动态      更新时间:2023-09-26

不幸的是,我无法在jsfiddle中使用它,但也许我忽略了一些东西(http://jsfiddle.net/bJpyU/46/)。我动态创建了块,需要保存它们的下单。由于设置的动态方式,我无法获取索引(它始终为 0,并且无论拖动到何处,首先开始的块始终显示为第一个索引)。toArray 和序列化显示为空白。我什至尝试过数第n个孩子。知道如何获得订单吗?

.HTML

<div class="tab-pane active col-lg-12" id="portlet_tab_Graphs">
    <div class="row padLR sortable_portlets" id="sortable_portlet_Graphs">
        <div class="col-lg-4 sortable sortable_portlet_Graphs_column ui-sortable" id="102">
            <div class="portlet box yellow">
                <div class="portlet-title">Monthly License Revenue</div>
            </div>
            <div class="portlet-body clearfix pad">
                <div id="h1_sortable_portlet_Graphs_102"></div>
                <div id="sortable_portlet_Graphs_102">
                    <div class="col-lg-12 nPad"> 
                        <div class="btn-group chartToggle inline">Graph Data</div>
                    </div>
                </div>
            </div>
        </div>        
        <div class="col-lg-4 sortable sortable_portlet_Graphs_column ui-sortable" id="103">
            <div class="portlet box blue">
                <div class="portlet-title">Yearly License Revenue</div>
            </div>
            <div class="portlet-body clearfix pad">
                <div id="h1_sortable_portlet_Graphs_102"></div>
                <div id="sortable_portlet_Graphs_102">
                    <div class="col-lg-12 nPad"> 
                        <div class="btn-group chartToggle inline">Graph Data</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

简讯

var sortdiv = "Graphs"
var blockClasses = "col-lg-4";
    $(".sortable_portlet_" + sortdiv[2] + "_column").sortable({
        connectWith: ".sortable_portlet_" + sortdiv[2] + "_column",
        handle: ".portlet-title",
        //placeholder: "dragColumn",
        forceHelperSize: true,
        forcePlaceholderSize: true,
        start: function (e, ui) {
            ui.placeholder.height(ui.helper.outerHeight());
            ui.placeholder.width(ui.helper.outerWidth());
            // get original block size
            blockClasses = ui.item.parent().attr('class');
        },
        stop: function (e, ui) {
            var parent = ui.item.parent();
            blockWidth = blockClasses.split(" ");
            parent.attr('class', function (i, c) {
                return c.replace(/(^|'s)col-lg-'S+/g, blockWidth[0]);
            });
            //console.log(ui.position)
            SavePortletDrop(sortdiv[2]);
        }
    });

我认为错误实际上是将sortable附加到您正在排序的每个单独项目。它应该附加到容器div。

因此,即使它看起来有效,您实际上也在一堆 1 项列表中移动,这些列表始终位于索引 0 处。

我通过将sortable附加到#sortable_portlet_Graphs,让它像我认为您想要的那样工作.这使得ui.item.index()返回您在stop函数中期望的数字。

http://jsfiddle.net/bJpyU/47/