prevent jsTree node select

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

I'm using the jsTree plugin to list folders in the file system. I need to prevent a user from changing to another node before a certain condition is met.
The below code does not stop the propagation... i saw some solutions with other plugins but this is a simple task it must be possible without other plugins.

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

回答1:

Documenting for myself and posterity: you need to include the following function AFTER loading the jstree JS (from: 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); 

Then when initializing an instance of jstree do something like this:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!