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

时间:2015-01-15 22:22:32

标签: javascript jquery jquery-ui indexing jquery-ui-sortable

不幸的是,我无法在jsfiddle中使用它,但也许我忽略了某些事情(http://jsfiddle.net/bJpyU/46/)。我有动态创建的块,需要保存它们的顺序。由于设置的动态方式,我无法获得索引(它始终为0,并且首先开始的块始终显示为第一个索引,无论它在何处被拖动)。 toArray和serialize显示为空白。我甚至试过计算n-child。知道如何获得订单吗?

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>

JQUERY

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]);
        }
    });

1 个答案:

答案 0 :(得分:1)

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

所以,即使它看起来像是在工作,你实际上是在移动一堆1项目列表,它们总是在索引0处。

通过将sortable附加到#sortable_portlet_Graphs,我认为你需要它。这使得ui.item.index()返回stop函数中您期望的数字。

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

相关问题