0

I want to show conditional contextMenu Items via jQuery.

For Example :

I have cars in my account. And I wanted to show options on conditions.

If car is of my own then all menu items should be visible to me. And if it is shared with me then only View Menu should to visible to me.

if (type == 'vehicle') {
   (function () {
     var vehicle_id = node.data.vehicle_id;
     var vehicle_status = '';
     $.ajax({
        url: baseUrl + '/check-vehicle-status/'+vehicle_id,
                        success: function(data) {
                            console.log(data);
                            if(data == 'shared'){
                                //what should I write here? to show only View option 
                            }
                        }
                    });

                    items = {
                        "View": {
                            "label": "View Vehicle", 
                            "action": function action() {
                                self.viewVehicle(vehicle_id);
                            }
                        },
                        "modify": {
                            "label": "Edit Vehicle", 
                            "action": function action() {
                                self.editVehicle(vehicle_id);
                            }
                        },
                        "delete": {
                            "label": "Delete Vehicle",
                            "action": function action() {
                                dialogHandler.showDeleteVehicle(function () {
                                    self.removeVehicle(vehicle_id);
                                });
                            }
                        },
MongoUser
  • 131
  • 1
  • 4
  • Please share what you already have – Nikolay Ermakov Mar 09 '17 at 12:30
  • I am sending ajax call with car ID which gives me car is my own or shared with me. soo I want to add this condition if car is shared with me show only View oprion. Other wise show all options likes EDIT,DELETE, etc. – MongoUser Mar 10 '17 at 05:42

1 Answers1

1

You will have to check the data param in the context menu method like below (provided that the node.data of a tree node will have a value).

Check demo - Fiddle Demo

...
contextmenu: {
  items: function (node) { 

      // remove default context menu items
      var tmp = $.jstree.defaults.contextmenu.items();
      delete tmp.rename;
      delete tmp.remove;
      delete tmp.ccp;
      delete tmp.create;

      for (menuItem in items) {
         if( menuItem === 'View' || node.data !== 'shared') { 
              tmp[ menuItem ] = {
                 id: menuItem,
                 label: items[menuItem].label,
                 action: items[menuItem].action
              }
          }
      }

      return tmp;
  }
},
Nikolay Ermakov
  • 5,031
  • 2
  • 11
  • 18