-
Notifications
You must be signed in to change notification settings - Fork 744
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
Comments
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. |
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: "<nenhuma>",
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. |
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). |
So the problem is with the "-1" ... seems it is treated as string. Again, thanks for the great support. |
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.. |
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. |
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. |
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. |
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. |
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. |
.@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.
|
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
The text was updated successfully, but these errors were encountered: