2011-11-21 114 views
1

我有這樣的選擇:jQuery的區分多個相同的選擇情況下

$list = $('ul.sortable'); 

爲jQueryUI的的排序插件。

頁面上有多個可排序的列表。因此,雖然每次只顯示1個實際顯示,但其中每個實體都顯示爲摺疊狀態,其餘顯示爲摺疊狀態。

當拖動之後偶發生火災時,即使Im排序最後一個,我也會得到第一個ul的排序順序。

我怎樣才能,當我實際上在後面的函數中處理排序,挑出特定的ul:not(.collapsed)?目前,它看起來像這樣:

$order = $item_list.nestedSortable('toHierarchy'); 

編輯:

下面是我們在頁面加載:

$item_list  = $('ul.sortable'); 
$url   = 'admin/navigation/order'; // CI Controller to process stuff 
$cookie  = 'open_links'; 
$data_callback = function(event, ui) { 
    // Grab the group id so we can update the right links 
    return { 'group' : ui.item.parents('section.box').attr('rel') }; 
} 

// Do sort 
sort_tree($item_list, $url, $cookie, $data_callback); 

sort_tree最終只是沒有上面的代碼來獲得訂單,並傳遞阿賈克斯控制器..

如果我更改上面的選擇器,所有ul的都沒有得到應用程序的ui插件,無法排序。所以我想,如果有一種方法可以從sort_tree函數內部區分這一點,我可以正確地做到這一點。

這裏是排序樹功能我一起工作:

sort_tree = function($item_list, $url, $cookie, data_callback, post_sort_callback) 
{ 
    // collapse all ordered lists but the top level 
    $item_list.find('ul').children().hide(); 

    // this gets ran again after drop 
    var refresh_tree = function() { 

     // add the minus icon to all parent items that now have visible children 
     $item_list.parent().find('ul li:has(li:visible)').removeClass().addClass('minus'); 

     // add the plus icon to all parent items with hidden children 
     $item_list.parent().find('ul li:has(li:hidden)').removeClass().addClass('plus'); 

     // remove the class if the child was removed 
     $item_list.parent().find('ul li:not(:has(ul))').removeClass(); 

     // call the post sort callback 
     post_sort_callback && post_sort_callback(); 
    } 
    refresh_tree(); 

    // set the icons properly on parents restored from cookie 
    $($.cookie($cookie)).has('ul').toggleClass('minus plus'); 

    // show the parents that were open on last visit 
    $($.cookie($cookie)).children('ul').children().show(); 

    // show/hide the children when clicking on an <li> 
    $item_list.find('li').live('click', function() 
    { 
     $(this).children('ul').children().slideToggle('fast'); 

     $(this).has('ul').toggleClass('minus plus'); 

     var items = []; 

     // get all of the open parents 
     $item_list.find('li.minus:visible').each(function(){ items.push('#' + this.id) }); 

     // save open parents in the cookie 
     $.cookie($cookie, items.join(', '), { expires: 1 }); 

     return false; 
    }); 

    $item_list.nestedSortable({ 
     disableNesting: 'no-nest', 
     forcePlaceholderSize: true, 
     handle: 'div', 
     helper: 'clone', 
     items: 'li', 
     opacity: .4, 
     placeholder: 'placeholder', 
     tabSize: 25, 
     listType: 'ul', 
     tolerance: 'pointer', 
     toleranceElement: '> div', 
     stop: function(event, ui) { 

      post = {}; 
      // create the array using the toHierarchy method 
      post.order = $item_list.nestedSortable('toHierarchy'); 

      // pass to third-party devs and let them return data to send along 
      if (data_callback) { 
       post.data = data_callback(event, ui); 
      } 

      // refresh the tree icons - needs a timeout to allow nestedSort 
      // to remove unused elements before we check for their existence 
      setTimeout(refresh_tree, 5); 

      $.post(SITE_URL + $url, post); 
     } 
    }); 
+0

OR有沒有一種方法可以確保被排序的ul是用於排序順序的那個? –

+0

我們能否看到您的事件處理程序以確保您正在使用該事件訪問正確的對象? UI將當前對象交給它的所有自定義事件;這應該按照您期望的方式工作。 – Mathletics

+0

PS - 這是來自稱爲PyroCMS的AWESOME cms的2.0 Beta。我提交了一個錯誤,只是試圖幫助找出修復! Mathletics - 我也這麼認爲。儘管我猜測我不太瞭解jquery的內涵。 –

回答

2

對不起,你的問題是有點難以明白,所以我就已經猜出了一點。

你在一個頁面上有幾個ul,你讓它們都可排序並確保只有一個可見。在稍後的時間點,您想請求可見UL的排序順序。我想第一次嘗試獲得可見的UL將是:

$('ul.sortable:visible') 

您可以從中請求排序順序。 但是,如果你處理這種排序,你可能知道李是ul的一個孩子。所以你也可以去

$(li).closest('ul.sortable'); 

其中li是排序的元素之一,這也應該得到你的根元素。不知道如果您使用拖放功能,這是否正常工作。可能這個可排序的東西本身提供了第三種方法(這種排序方法可能?)。

+0

對不起,我更新了我的問題。希望它有幫助。我也可以提供文件。 –

相關問題