2013-10-29 83 views
0

這讓我感到困擾了幾個小時。尋找最接近的,具有特定數據屬性的元素jquery

我有一張桌子。在該表中,我正在尋找具有特定數據屬性的最近的前一個表格行。我在成功使用jQuery排序之後正在執行此搜索。我已經嘗試了幾乎所有的東西,而且總是出現錯誤的東西。

這裏就是我使用

var newIndex = ui.item.index(); 
var menuLevel = parseInt($("#menu-table").find("[data-menu-nesting='" + newIndex + "']").attr("data-menu-level")); 
var menuId = $("#menu-table").find("[data-menu-nesting='" + newIndex + "']").attr("data-menu-id"); 

if (menuLevel == 2) { 
    var findAboveRowName = $(".menu-table-rows[data-menu-nesting='" + newIndex + "']").prev(".menu-table-rows").data("menu-level","1").attr("data-menu-name"); 
    alert(findAboveRowName);  
} 
if (menuLevel == 3) { 
    var findAboveRowName = $(".menu-table-rows[data-menu-nesting='" + newIndex + "']").prev(".menu-table-rows").data("menu-level","2").attr("data-menu-name"); 
    alert(findAboveRowName);  
} 

從本質上講,變量「newIndex」應該搶行的新位置進行排序後,menuLevel應該抓住數據屬性「菜單級「,並且menuId正在抓取該表格行的另一個數據屬性。

它專門查找表格行中最近的前一個菜單級屬性。所以,如果用2菜單級屬性錶行移動時,它看起來與1.

菜單級屬性最近的錶行,如果需要完整的jQuery排序腳本我使用

$("#sortable").sortable({ 
       update: function(event, ui) { 
        var serial = $('#sortable').sortable('serialize'); 
        var newIndex = ui.item.index(); 
        var menuLevel = parseInt($("#menu-table").find("[data-menu-nesting='" + newIndex + "']").attr("data-menu-level")); 
        var menuId = $("#menu-table").find("[data-menu-nesting='" + newIndex + "']").attr("data-menu-id"); 
        if (menuLevel == 2) { 
         var findAboveRowName = $(".menu-table-rows[data-menu-nesting='" + newIndex + "']").prev(".menu-table-rows").data("menu-level","1").attr("data-menu-name"); 
         alert(findAboveRowName); 
         // $.post("./menu-controller.php", { adjustParent: true, id: menuId, parent: findAboveRowName }); 
        } 
        if (menuLevel == 3) { 
         var findAboveRowName = $(".menu-table-rows[data-menu-nesting='" + newIndex + "']").prev(".menu-table-rows").data("menu-level","2").attr("data-menu-name"); 
         alert(findAboveRowName); 
         // $.post("./menu-controller.php", { adjustParent: true, id: menuId, parent: findAboveRowName }); 
        } 
        $.ajax({ 
        url: "./menu-controller.php", 
        type: "post", 
        data: serial, 
        success: function() { 
         $("#sortable").load("./menu-manager.php #menu-table", function() { 
          $.get('./menu-admin.js'); 
         }); 
       }, 
        error: function(){ 
         alert("A problem occurred when moving this menu item. Please try again or contact support."); 
        } 
        }); 
       }, 
      handle:'.move-item', 
      connectWith:'#menu-table', 
      placeholder: "highlight", 
      containment: "parent", 
      revert: true, 
      tolerance: "pointer", 
      items: 'tbody > *' 
}); 

JSFIDDLE

+0

不是$(「selector」)。parent(「tr [data = value]」)要走的路嗎? –

+0

你能解釋我應該在小提琴中做什麼來看問題嗎?它提醒着一些事情;如果他們錯了,我不知道正確的警報應該是什麼。 – Barmar

+0

在「子菜單3」下移動「子子菜單2」。它會提醒說「家」。這是不正確的,因爲在「子菜單3」下移動「子子菜單2」後,最近的下級菜單項將是「子菜單3」,而不是「家」。 –

回答

2

.prev只返回立即以前的元素,它不會繼續尋找,選擇器相匹配的最近的元素。使用.prevAll查找與選擇器匹配的所有元素,然後使用.first()將其縮小到最接近的第一個元素。如果你想搜索一個特定的data-menu-level屬性,你必須把它放在選擇器中;呼叫.data("menu-level", 1)設置屬性,它不搜索它。

if (menuLevel == 2) { 
    var findAboveRowName = $(".menu-table-rows[data-menu-nesting='" + newIndex + "']").prevAll(".menu-table-rows[data-menu-level=1]").first().data("menu-name"); 
    alert(findAboveRowName);  
} 
+0

出於某種原因,它甚至沒有從行中收集正確的菜單級別,我認爲這可能是實際問題。例如,如果我移動1級項目,即使它不應該執行任何操作(如果menu_level爲1),它也會通知我有關findAboveRowName變量的信息。 –

+0

難以讓我在沒有看到相關HTML的情況下知道發生了什麼問題。你可以做小提琴嗎? – Barmar

相關問題