Coder Social home page Coder Social logo

Comments (9)

rodneyrehm avatar rodneyrehm commented on May 23, 2024

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.

from jquery-contextmenu.

tiagof avatar tiagof commented on May 23, 2024

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.

from jquery-contextmenu.

rodneyrehm avatar rodneyrehm commented on May 23, 2024

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).

from jquery-contextmenu.

tiagof avatar tiagof commented on May 23, 2024

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.

from jquery-contextmenu.

tiagof avatar tiagof commented on May 23, 2024

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..

from jquery-contextmenu.

rodneyrehm avatar rodneyrehm commented on May 23, 2024

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.

from jquery-contextmenu.

rrostock1 avatar rrostock1 commented on May 23, 2024

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.

  • from jquery-contextmenu.

    fake-name avatar fake-name commented on May 23, 2024

    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.

    from jquery-contextmenu.

    bbrala avatar bbrala commented on May 23, 2024

    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.

    from jquery-contextmenu.

    Related Issues (20)

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. 📊📈🎉

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google ❤️ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.