Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

javascript - Finding the closest, previous element with specific data attribute jquery

This has been troubling me for the passed few hours now.

I have a table. Within that table i'm looking for the closest, previous table row with a specific data attribute. I'm doing this search right after a successful use of jquery sortable. I've tried almost everything and it ALWAYS comes up with the wrong thing.

Here's what i'm using

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

Essentially, the variable "newIndex" is supposed to grab the new position of the row after being sorted, menuLevel is supposed to grab the data attribute "menu-level" of that table row, and menuId is grabbing another data attribute of that table row.

It's specifically looking for the nearest, previous menu-level attribute in the table rows. So if a table row with a menu-level attribute of 2 is moved, it's looking for the nearest table row with a menu-level attribute of 1.

The full jquery sortable script i'm using if needed

$("#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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.prev only returns the immediately previous element, it doesn't keep looking for the nearest element that matches the selector. Use .prevAll to find all the elements matching a selector, and then use .first() to narrow it down to the first one, which is the nearest. And if you want to search for a specific data-menu-level attribute, you have to put that in the selector; calling .data("menu-level", 1) sets the attribute, it doesn't search for it.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...