防止jsTree节点选择

时间:2014-06-28 23:06:17

标签: jquery jquery-plugins jstree

我正在使用jsTree插件列出文件系统中的文件夹。我需要阻止用户在满足某个条件之前更改到另一个节点 下面的代码并没有停止传播...我看到了其他插件的一些解决方案,但这是一个简单的任务,必须可以在没有其他插件的情况下完成。

$('#jstree').on('select_node.jstree', function (e) 
{
    if (!changeAllowed()
    {
        e.preventDefault(); 
        e.stopImmediatePropagation();
    }
}); 

1 个答案:

答案 0 :(得分:12)

为自己和后代记录:在加载jstree JS之后需要包含以下功能(来自:https://github.com/vakata/jstree/blob/master/src/misc.js):

// jstree conditional select node
(function ($, undefined) {
  "use strict";
  $.jstree.defaults.conditionalselect = function () { return true; };

  $.jstree.plugins.conditionalselect = function (options, parent) {
    // own function
    this.select_node = function (obj, supress_event, prevent_open) {
      if(this.settings.conditionalselect.call(this, this.get_node(obj))) {
        parent.select_node.call(this, obj, supress_event, prevent_open);
      }
    };
  };
})(jQuery);

然后在初始化jstree的实例时执行以下操作:

$('#jstree').jstree({
  'conditionalselect' : function (node) {
    return <something that determines condition> ? true : false;
  },
  'plugins' : ['conditionalselect'],
  'core' : {
    <core options>
  }
});