Last active
May 15, 2017 14:11
-
-
Save FlorinMotoc/589e6ae7ef5e0d04cb2b29457c2a2ee3 to your computer and use it in GitHub Desktop.
JsTree Plugin - Custom Text (you can use what you want instead of required `text` key)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JsTree Plugin - Custom Text (you can use what you want instead of required `text` key) | |
(function ($) { | |
"use strict"; | |
$.jstree.defaults.customtext = 'text'; | |
$.jstree.plugins.customtext = function (text_key, parent) { | |
var self = this; | |
this.customtext_transformer = function (values) { | |
if (text_key === 'text') { | |
return values; | |
} | |
$.each( values, function( key, value ) { | |
values[key]['text'] = value[text_key]; | |
delete value[text_key]; | |
if (typeof value['children'] === 'object' && value['children'].length) { | |
values[key]['children'] = self.customtext_transformer(value['children']); | |
} | |
}); | |
return values; | |
}; | |
this.init = function (el, options) { | |
options.core.data = self.customtext_transformer(options.core.data); | |
parent.init(el, options); | |
}; | |
}; | |
})(jQuery); | |
/** Example: | |
$('#container').jstree({ // used `name` instead of `text` | |
core: {data: [{"name": "Root node", "children": [{"name": "Child node 1"}, {"name": "Child node 2"}]}]}, | |
plugins: ['customtext'], | |
customtext: 'name' | |
}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment