4

This is fiddle basically the same thing as seen on jquery

Here is fiddle of that version: http://jsfiddle.net/DHCaA/

What I am trying to do is simple. For example (using that fiddle) If I move section 1 below section 2, how can I get ID's of the sections that switched place. I need those ID's so I can do some calculations and changes on my model.

Every element in the accordion is created dynamically, and I can create whatever I desire. For example:

<div id ="first-1" class="group">

Thats first entry with ID.

Any Idea?

4

3 回答 3

2
function current_order(el){
    var order=[];
    el.children().each( function(i){      
              order[i]=this.id;
    });
    // silly test      
    for(var i=0; i<order.length; i++){
       console.log("got " + order[i]);
   }
}

// 添加到手风琴停止方法

stop: function( event, ui ) {
      // IE doesn't register the blur when sorting
      // so trigger focusout handlers to remove .ui-state-focus
      ui.item.children( "h3" ).triggerHandler( "focusout" );

      current_order($(this));

    }
于 2013-06-30T22:59:32.270 回答
2

您可以存储元素的原始索引(例如 via data()),并在排序后比较存储的索引和当前索引(当它们不相等时,位置已更改)。之后更新存储的索引。

$( "#accordion" )
      .accordion({
        header: "> div > h3",
        collapsible: true
      })
      .sortable({
        axis: "y",
        handle: "h3",
        stop: function( event, ui ) {
        var items=[];
        ui.item.siblings().andSelf().each(function(){
            //compare data('index') and the real index
            if($(this).data('index')!=$(this).index()){
              items.push(this.id);
            }
          });
          // IE doesn't register the blur when sorting
          // so trigger focusout handlers to remove .ui-state-focus
          ui.item.children( "h3" ).triggerHandler( "focusout" );
          if(items.length)alert(items.join(','));
          ui.item.parent().trigger('stop');
        }
      }).on('stop',function(){
        $(this).children().each(function(i){$(this).data('index',i)});
      }).trigger('stop');

http://jsfiddle.net/DHCaA/2/

于 2013-06-30T23:24:30.407 回答
0

看看这个:

.sortable({
    axis: "y",
    handle: "h3",
    stop: function( event, ui ) {
        var items=[];
        ui.item.siblings().andSelf().each( function(){
            //compare data('index') and the real index
            if($(this).data('index')!=$(this).index()){
                items.push(this.id);
            }
        });
        // IE doesn't register the blur when sorting
        // so trigger focusout handlers to remove .ui-state-focus
        ui.item.children( "h3" ).triggerHandler( "focusout" );
        **if(items.length) $("#sequence").text(items.join(','));**
        ui.item.parent().trigger('stop');
    } 
})

在手风琴下面我列出了序列

<div>
    Sequence:  <span id="sequence"> This is the sequence of the elements.</span>
</div>   
于 2013-09-07T15:10:03.450 回答