How to get metadata of JsTree items from contextmenu? - jstree

In jstree , I can right click on a node and select an item from the context menu.
In the code below, I am trying to use the obj object to get the tree item. I can get that using $(obj) however this is just a HTML list item. How do I get metadata (foo) associated with that ?
$(element).jstree(
"data" : { "data" : {"title" : "An item"}, "metadata":{"foo" :"bar"}},
"plugins" : ["themes", "ui", "contextmenu"],
"contextmenu" : {
"items" : function($node) {
return {
"Menu1" : function(obj){
//my menu action
}
};
};
}
);

Here is how the metadata can be accessed.
obj.data().foo

I had the same issue, when trying to fetch the ID of a selected jsTree node.
It can be done, you just need to create your own function to populate the contextmenu, to fetch the relevant values from your JSON data:
jsTree Context Menu selected item?

In arguments of function contextmenu.items you have access of current item data. This dynamically invocable function, fired every time on new right click event on node, See example:
$('#post-tree').jstree({
core: {
data: treeData
},
plugins: ['contextmenu'],
contextmenu: {
items: function (item) {
return {
addPost: {
label: 'Add new Post',
action: function () {
console.log(item.original); // metadata object
}
},
addSubcategory: {
label: 'Add subcategory',
action: function () {
console.log('addSubcategory');
}
},
editCategory: {
label: 'Properties',
action: function () {
console.log('editCategory')
}
},
delete: {
label: 'Delete',
action: function () {
console.log('delete Category');
}
}
};
}
}
});

Related

Extend control with HBox container and inherited but customized event

The idea is to have a HBox container under the MultiComboBox control to which selected tokens will be pushed. I have followed different tutorials and couldn't get a success. A multiComboBox is simply now shown.
The idea:
Simplified (testing) implementation of custom control:
sap.ui.define([
'sap/m/MultiComboBox',
'sap/m/HBox'
], function (MultiComboBox, HBox) {
'use strict';
/**
* Constructor for a new MultiCombobox with tokens.
*/
return MultiComboBox.extend('drex.control.DropDownWithTags', {
metadata: {
aggregations: {
_tokensContainer: { type: 'sap.m.HBox', multiple: false }
},
},
init: function () {
MultiComboBox.prototype.init.apply(this, arguments);
this.setAggregation('_tokensContainer', new HBox());
},
_addToken: function () {
this.getAggregation('_tokensContainer').insertItem({text: 'test'});
},
_handleSelectionLiveChange: function(oControlEvent) {
this._addToken();
MultiComboBox.prototype._handleSelectionLiveChange.apply(this, arguments);
},
renderer: function (rm, DropDownWithTags) {
rm.write('<div');
rm.writeControlData(DropDownWithTags);
rm.write('>');
rm.renderControl(DropDownWithTags.getAggregation('_tokensContainer'));
rm.write('</div>');
}
});
});
XML (no change, except for a name, could that be a problem?). Adding HBox aggregation to it does not help.
<drex:DropDownWithTags
items="{
path: 'diseaseList>/allDiseases'
}"
selectedKeys="{filterModel>/disease}"
selectionFinish="onSelectDisease">
<core:Item key="{diseaseList>id}" text="{diseaseList>Name}"/>
</drex:DropDownWithTags>
Any idea why it happens ? I cannot see my mistake.
there are many ways to do this. here is one way
sap.ui.define([
'sap/ui/core/Control',
'sap/ui/core/Item',
'sap/m/MultiComboBox',
'sap/m/HBox',
'sap/m/Text'
], function (Control, Item, MultiComboBox, HBox, Text) {
Control.extend('DropDownWithTags', {
metadata: {
aggregations: {
combo: { type: 'sap.m.MultiComboBox', multiple: false },
_hbox: { type: 'sap.m.HBox', multiple: false }
},
},
init: function () {
Control.prototype.init.apply(this, arguments);
this.setAggregation('_hbox', new HBox({
items: [
]
}));
},
renderer: function (rm, oControl) {
rm.write('<div');
rm.writeControlData(oControl);
rm.write('>');
rm.write('<div>');
rm.renderControl(oControl.getAggregation('combo'));
rm.write('</div>');
rm.write('<div>');
rm.renderControl(oControl.getAggregation('_hbox'));
rm.write('</div>');
rm.write('</div>');
},
onAfterRendering: function() {
var combo = this.getAggregation('combo')
var hbox = this.getAggregation('_hbox');
combo.attachEvent("selectionChange", function() {
hbox.destroyItems();
var text = this.getSelectedItems().map(function(item) {
return item.getText();
});
if (text.length > 0) {
hbox.addItem(new Text({ text: text.join(",")}))
}
})
}
});
var combo = new DropDownWithTags({
combo: new MultiComboBox({
items: [
new Item({
key: "test",
text: "test"
}),
new Item({
key: "test1",
text: "test1"
})
]
})
});
combo.placeAt("content")
});

How to access the object passed along with the event object in the fireBuy() method?

<script>
sap.ui.core.Control.extend("com.controls.MyButton", {
metadata: {
aggregations: {
buyButton : { type: "sap.ui.commons.Button", multiple: false}
},
events: {
buy: {enablePreventDefault: true}
}
},
init: function() {
var oControl = this;
//create a button to allow used buying that book
var oBuyBtn = new sap.ui.commons.Button({
text: "Click Me",
press: function (oEvent) {
oControl.fireBuy({
someData : "some data I want to pass along with the event object"
});
}
});
this.setAggregation("buyButton", oBuyBtn);
},
renderer: {
render: function(oRm, oControl) {
oRm.renderControl(oControl.getAggregation("buyButton"));
}
}
});
var oButton = new com.controls.MyButton({
buy: function(oEvent) {
alert(oEvent.someData);
}
});
oButton.placeAt("content");
</script>
The code is shown above. I want to get the someData property in the change callback function, How can I possibly achieve that? I tried oEvent.someData, but I got undefined alert. The code is from nabisoft website, but there has no explanation on how to access the object passed by the fireBuy method.
var oButton = new com.controls.MyButton({
buy: function(oEvent) {
alert(oEvent.getParameter("someData"));
}
});
oEvent.getParamter("someData") solves this!

jsTree state plugin reloads page continually

I'm using jsTree as a navigation menu, and after clicking any of the links the page reloads over and over... I've searched all day for an answer to this and would very much appreciate a hand.
$('#NavTreeContainer').jstree({
'core': {
'themes': {
'name': 'default',
'responsive': true,
'stripes': true,
'dots': true,
'icons': false
}
},
'state' : { 'key' : 'navTree' },
'plugins' : [ 'state' ]
}).on('select_node.jstree', function(e, data) {
document.location = data.instance.get_node(data.node, true).children('a').attr('href');
});
$('#ExpandAllNavTreeButton').click(function() {
$('#NavTreeContainer').jstree('open_all');
});
$('#CollapseAllNavTreeButton').click(function() {
$('#NavTreeContainer').jstree('close_all');
});
I was able to get this functionality working by only binding the select node event after the tree state has been restored;
$('#jstree_id').jstree({
"state": { "key": "jstree_id" },
"plugins": ["state"]
}).on('restore_state.jstree', function () {
$('#jstree_id').on('select_node.jstree', function (e, data) {
var href = data.node.a_attr.href;
document.location.href = href;
});
});
If you already solved this I hope it helps someone else.

How to create knockout datagrid on popup from within datagrid

I have a datagrid that has additional information I'd like to show in a pop up.
I display an icon to alert the user this information exists. The icon has a click action that performs a callback to the server to get this information. When I click this, I have all my data ready to view, but am not sure of the MVVM logistics of this.
Here is a mockup of what I'm trying to do. The alert function displays a modal popup. The click handler works, and the callback works fine.
I'm wondering, where do I create the new grid model, should it be a part of the current grid model, where do I apply bindings?
var initialData = [
{ name: "ABC", number: 1, icon: true},
{ name: "DEF", number: 2, icon: false },
];
var GridModel = function(items) {
this.items = ko.observableArray(items);
this.gridViewModel = new ko.simpleGrid.viewModel({
data: this.items,
columns: [
{ headerText: "Name", rowText: "name" },
{ headerText: "Number", rowText: "number" },
{ headerText: "Addl Info",
rowText: function (item)
{
return item.icon ? "<i class=\"icon\"></i>" : "";
},
clickHandler: function (item)
{
function callback(data) {
// data is now an array of objects I want a data grid for that displays in the pop up.
alert("Additional Information", <HTML GOES HERE>);
}
$$.getJSON("url", [], callback);
}
],
});
};
ko.applyBindings(new GridModel(initialData));

Ext TreePanel: How to add Child Nodes to the expanded node?

I am trying to create a Tree from Ext.tree.Panel with JSON data.
My JSON
{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
I want the first level nodes to be the "firstName: from the above JSON. I am able to achieve this but when any of the node is expanded the same nodes are displayed as children to the expanded node shown below.
- John
---> + John
---> + Anna
---> + Peter
+ Anna
+ Peter
When any of the node is expanded I want to add children from another JSON file. but here the problem is before adding the children to the node i can see the child nodes attached as above.
Is there any way to avoid this behavior ?
Code Snippet
My TreePanel:
Ext.define('MyApp.view.MyTreePanel', {
extend: 'Ext.tree.Panel',
height: 439,
width: 400,
title: 'My Tree Panel',
store: 'MyTreeStore',
displayField: 'firstName',
rootVisible: false,
initComponent: function() {
var me = this;
Ext.applyIf(me, {
viewConfig: {
}
});
me.callParent(arguments);
}
});
TreeStore:
Ext.define('MyApp.store.MyTreeStore', {
extend: 'Ext.data.TreeStore',
requires: [
'MyApp.model.MyModel'
],
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
autoLoad: true,
autoSync: true,
model: 'MyApp.model.MyModel',
storeId: 'MyTreeStore',
proxy: {
type: 'ajax',
url: 'employees.json',
reader: {
type: 'json',
root: 'employees'
}
}
}, cfg)]);
}
});
**Model:**
Ext.define('MyApp.model.MyModel', {
extend: 'Ext.data.Model',
fields: [
{
name: 'firstName'
}
]
});
Thanks in advance....
The url will get called for every node you click on that hasn't been 'loaded' yet. I'm not sure why ExtJS does this as the parameters change per node clicked. What I've done because I needed a lot of extra functionality is override the load() in the tree.Store (sorry, it's a little messy and could use a cleanup):
Ext.define('MyApp.store.MyTreeStore', {
extend: 'Ext.data.TreeStore',
load: function(options) {
options = options || {};
options.params = options.params || {};
var me = this,
node = options.node || me.tree.getRootNode(),
root;
// If there is not a node it means the user hasnt
// defined a rootnode yet. In this case lets just
// create one for them.
if (!node) {
node = me.setRootNode({
expanded: true
});
}
if (me.clearOnLoad) {
node.removeAll();
}
Ext.applyIf(options, {
node: node
});
options.params[me.nodeParam] = node ? node.getId() : 'root';
if (node) {
node.set('loading', true);
}
var root = me.getRootNode();
if (node.isRoot()) {
// put some root option.params here.
// otherwise, don't pass any if you don't need to
} else {
options.params = Ext.JSON.encode({
// or some parameter here for the node to
// get back the correct JSON data
nodeId: node.get('id')
});
}
return me.callParent([options]);
},
model: 'MyApp.model.MyModel',
storeId: 'MyTreeStore',
proxy: {
type: 'ajax',
url: 'employees.json',
reader: {
type: 'json',
root: 'employees'
}
},
autoLoad: false,
listeners: {
append: function(store,node,index) {
// on append depth 1 is first level, beforeappend it is 0
if (node.get('depth') == 1) {
// setting these values here assures that the node knows it's been loaded already.
// node.set('leaf',true); force leaf true, false or whatever you want.
node.set('loaded',true);
node.commit();
}
}
}
});
Also, you can do something like this to your JSON if you need to have the expand / collapse icons there. These are default fields that get added by default.
{
"employees": [
{ "firstName":"John" , "lastName":"Doe", leaf: false, children: []},
{ "firstName":"Anna" , "lastName":"Smith", leaf: false, children: []},
{ "firstName":"Peter" , "lastName":"Jones", leaf: false, children: []}
]
}