【问题标题】:ContextMenu Items in jQueryjQuery 中的 ContextMenu 项
【发布时间】:2017-03-09 09:51:26
【问题描述】:

我想通过 jQuery 显示有条件的 contextMenu 项目。

例如:

我的帐户中有汽车。我想根据条件显示选项。

如果汽车是我自己的,那么我应该可以看到所有菜单项。如果它与我共享,那么只有查看菜单对我可见。

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);
                                });
                            }
                        },

【问题讨论】:

  • 请分享你已经拥有的东西
  • 我正在发送带有汽车 ID 的 ajax 调用,这给了我汽车是我自己的或与我共享的。如果与我共享汽车,我想添加此条件仅显示查看 oprion。其他明智的显示所有选项,如 EDIT、DELETE 等。

标签: javascript jquery contextmenu jstree


【解决方案1】:

您必须检查上下文菜单方法中的data 参数,如下所示(前提是树节点的node.data 将具有值)。

查看演示 - 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 的回复。但是你能帮助使 ajax 响应全局化,以便我可以在项目中使用它
  • 当然,但恐怕我没明白。是否要在右键单击节点并显示上下文菜单之前进行 ajax 调用?
  • 是的。根据ajax调用结果显示菜单子项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
  • 2014-07-17
相关资源
最近更新 更多