Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item order is inverted in Chrome #45

Closed
tiagof opened this issue May 11, 2012 · 12 comments
Closed

Item order is inverted in Chrome #45

tiagof opened this issue May 11, 2012 · 12 comments

Comments

@tiagof
Copy link

tiagof commented May 11, 2012

Hi,

for some reason the item order is inverted in chrome (version 18.0.1025.168 m).
Both IE and Firefox show the items in the order they are declared.

I assume there is no option/parameter to define the order (at least i couldn't find it in the api).

Is it a plugin issue or chrome's fault?

some SS:
http://dl.dropbox.com/u/6551/ssChrome.png
http://dl.dropbox.com/u/6551/ssIE.png

@rodneyrehm
Copy link
Contributor

I've tried the provided demos in Chrome Stable (18.0.1025.168) and Canary (21.0.1134.0) builds. They both show the correct order of items. If the demos show up correctly, but your code does not, please share your code in a fiddle so I can investigate.

@tiagof
Copy link
Author

tiagof commented May 11, 2012

Hi,

first of all: WOW, great response time! :)

The code:

    $.contextMenu({
        selector: ".context-menu-user", 
        trigger: "left",
        callback: function(key, options) {
            // var m = "clicked: " + key + "on element " + $(this).attr("id");
            // window.console && console.log(m) || alert(m); 
            var cnt = "O";          
            if( key == 2 ) cnt= "Enviar";
            if( key == 1 ) cnt= "Devolver";

            $(this).children("div").html( cnt );        
            $(this).children("input[name^='ctx']").val( key );
            validateRow( $(this).parent().parent()  /* the <tr> */);                  
        },
        items: {
                "-1": { name: "&lt;nenhuma&gt;",
                            icon: "edit"
                         }, 
                "2": {  name: "Enviar",
                            icon: "edit",
                            disabled: function(key, opt) { 
                            // this references the trigger element
                            var e = $(this).children("input[name^='estado']").val();
                            return !( e==0 || e==1 ) /*pendente ou devolvido */;
                        }
                    },
                "0": {  name: "Cancelar envio",
                            icon: "cut",
                            disabled: function(key, opt) { 
                            // this references the trigger element
                            var e = $(this).children("input[name^='estado']").val();
                            return !( e==2 ) /* enviado/recebido */;
                        }
                        }
            }                     
    });

Later today I'll try this code in an "empty" web page, just in case some other css is messing with the menu...

Thanks in advance for your support.

@rodneyrehm
Copy link
Contributor

I guess this is a problem of chrome, then. This fiddle reduces your issue to the root-problem.

Now that I see this, I imagine to remember having read something about »the order of object properties not being reliable« or something. I'll investigate (once I find the time).

@tiagof tiagof closed this as completed May 11, 2012
@tiagof tiagof reopened this May 11, 2012
@tiagof
Copy link
Author

tiagof commented May 11, 2012

So the problem is with the "-1" ... seems it is treated as string.
And now i see how the items are ordered :) so I'll solve it by modifying the item names ..

Again, thanks for the great support.

@tiagof tiagof closed this as completed May 11, 2012
@tiagof
Copy link
Author

tiagof commented May 11, 2012

Just wanted to add that Firefox seems to show the items in the order that you code them and Chrome seems to sort them based on the key..

@rodneyrehm
Copy link
Contributor

There are tons of questions on StackOverflow, like http://stackoverflow.com/questions/3186770/chrome-re-ordering-object-keys-if-numerics-is-that-normal-expected - even pointing you to the related V8 issue.

@rrostock1
Copy link

I found that this behavior is also found in IE9. Creating context menu items with a key value of type integer appear out of order. I reproduced it also with the above jsfiddle in IE9. It's hacky but I was able to work around the issue by prefixing a string before the "real" integer value and stripping it off later.
Is it a concern that it is just a defacto standard that object properties are retrieved in insertion order, rather than being a documented behavior? A possible solution might be to add a "order" property or something that can be added to each item at registration time, and then sorting on this when creating the

  • html. I thought about implementing this myself but for things like separators it seems like it may be tricky.

  • @fake-name
    Copy link

    fake-name commented Aug 16, 2016

    I'm curious how this is fixed, because reading the linked question above, apparently there isn't really a way to work around the issue.

    AFICT, basically the only way to actually solve this is to not use an object to pass in the items to be placed into the menu. It might "work" right now, but it's working by virtue of an implementation artifact, rather then because it's correct. This is basically working by depending on the JS equivalent of C undefined behaviour.

    Passing in a map doesn't appear to currently work, though it's the currently "correct" way of having an ordered key-value store.

    @bbrala
    Copy link
    Member

    bbrala commented Aug 16, 2016

    Yeah for now it is indeed relying on the behaviour that reordering only happens when the keys are numeric. This might not be ideal, but if this is changed, it needs to be backward compatible.

    I wouldn't mind the plugin to accept a Map and get values with a getter then. I would recommend making a new issue with the request for the items to accept a Map as definition.

    @JamoCA
    Copy link

    JamoCA commented May 9, 2024

    This tripped me up today too. (12 years later) I was dynamically generating numeric strings for some item keys and the menu became unsorted after the first number strings were added to the menu. I fixed this by prefixing the keys with a string + underscore and then sanitized the key value during the callback.

    @hybridwebdev
    Copy link

    This tripped me up today too. (12 years later) I was dynamically generating numeric strings for some item keys and the menu became unsorted after the first number strings were added to the menu. I fixed this by prefixing the keys with a string + underscore and then sanitized the key value during the callback.

    It's 2024 and you're still using jQuery? Man I feel bad for whoever pays you to do development for them.

    @JamoCA
    Copy link

    JamoCA commented May 10, 2024

    .@hybridwebdev Thanks for the helpful & insightful feedback. LOL. I can only assume by way of contrast that you use native JavaScript for everything you do. Very, very impressive. Would you care to share which superior library you are using for JavaScript-based context menus?

    This issue was reported 12 years ago. While I haven't been using this library for 12 years, I was attempting to indicate that this issue is 12 years old, still exists and the behavior of the numeric object keys being oddly sorted still exists. I'm thankful for active repositories and posts like this that enable me to identify that it's a known issue, and it's easy to work around.

    My clients are quite satisfied. I've been developing web applications since '97 and don't use PHP or WordPress. While I primarily focus on the server-side, I do write some JavaScript and have found the jQuery library has stood the test of time when many other companies, technologies & libraries I've used or considered have been either replaced, are currently defunct or are deprecated. jQuery v4 Beta was just released, and I prefer "using what works" rather than investing time learning each new fad in hopes that it will solve some problem that my code isn't experiencing.

    According to Stack Overflow's survey, 22.87% of developers still use JQuery, it's the third most popular web framework and one of the most starred repositories on GitHub.
    https://www.learnenough.com/blog/what-is-jquery

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    7 participants