简体   繁体   中英

Index/position of dynamic jquery ui sortable blocks?

Unfortunately, I am not able to get this working in jsfiddle but maybe I overlooked something ( http://jsfiddle.net/bJpyU/46/ ). I have blocks dynamically created and need to save the order that they are placed. Because of the dynamic fashion that this is set up, I am not able to get the index (it's always 0, and the block starting first always shows as first index wise regardless of where it is dragged). toArray and serialize are showing up as blank. I've even tried counting nth-child. Any idea how to get the order?

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

I think the error is actually from attaching sortable to each individual item that you're sorting. It should be attached to a container div.

So, even though it looked like it was working, you were actually moving around a bunch of 1-item lists, which are always at index 0.

I got it to work like I think you want it by attaching sortable to the #sortable_portlet_Graphs . This makes ui.item.index() return the number you'd expect in the stop function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM