2

i am using contexMenu by SWIS and i don't know how can i change the selected vale from a checkbox. i have this code:

function isOk(){
    return false;
}

$.contextMenu({
    selector: "td[name*='someSelector']", 
    callback: function(key, options) {
        var m = "clicked: " + key + " id: "+ this.attr('id');
        window.console && console.log(m) || alert(m);
    },
    items: {
        "okOption": {
            name: "ok", 
            type: 'checkbox', 
            selected: function(){ return isOk(); }
        },
        "quit": {
            name: "Close Menu", 
            icon: function(){
                return 'context-menu-icon context-menu-icon-quit';
            }
        }
    }
});

But the checkbox is always true.

I don't know what is wrong or if the selected doesn't works with a function and only works with a true or false.

function isOk(){
			return false;
		}
		
		$.contextMenu({
			selector: "[name*='someSelector']", 
			callback: function(key, options) {
				var m = "clicked: " + key + " id: "+ this.attr('id');
				window.console && console.log(m) || alert(m);
			},
			items: {
				"okOption": {
					name: "ok", 
					type: 'checkbox', 
					selected: {function(){ return isOk(); }}
				},
				"quit": {name: "Close Menu", icon: function(){
					return 'context-menu-icon context-menu-icon-quit';
					}
				}
			}
		});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.js"></script>
<script src="https://swisnl.github.io/jQuery-contextMenu/js/theme.js"></script>
<link href="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.css" rel="stylesheet"/>

<span name="someSelector"> ClickRightHere</span>

2
  • 1
    it'd be good to have the HTML, and even better if you post everything in a Stackoverflow code snippet! :) Commented Jun 26, 2017 at 7:56
  • Code snippet published. Thanks. Commented Jun 26, 2017 at 8:23

2 Answers 2

2

Finally i solved using a event: Here the snippet with the solution for future people consult and solve it.

function isOk() {
  return false;
}

$.contextMenu({
  selector: "[name*='someSelector']",
  callback: function(key, options) {
    var m = "clicked: " + key + " id: " + this.attr('id');
    window.console && console.log(m) || alert(m);
  },
  items: {
    "okOption": {
      name: "ok",
      type: 'checkbox'
    },
    "quit": {
      name: "Close Menu",
      icon: function() {
        return 'context-menu-icon context-menu-icon-quit';
      }
    }
  },
  events: {
    show: function(opt) {
      // this is the trigger element
      var $this = this;
      // import states from data store 
      $this.data().okOption = isOk();

      $.contextMenu.setInputValues(opt, $this.data());
      // this basically fills the input commands from an object
      // like {name: "foo", yesno: true, radio: "3", &hellip;}
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.js"></script>
<script src="https://swisnl.github.io/jQuery-contextMenu/js/theme.js"></script>
<link href="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.css" rel="stylesheet" />

<span name="someSelector"> ClickRightHere</span>

Sign up to request clarification or add additional context in comments.

2 Comments

You can safely remove the selected: {function(){ return isOk(); }} part - it is of no use anymore.
Yes, thanks, it's true. Answer edited with the change.
0

You cannot use a function for the selected option. The documentation says:

selected: string or boolean

So you could use a variable like this:

var myFlag = false;

$.contextMenu({
    ...
    items: {
        "okOption": {
            name: "ok", 
            type: 'checkbox', 
            selected: myFlag
        },
    ...

1 Comment

yes, i thought use these but, i need to check when the right button click, and in these way only check when the page is loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.