Select node in treepanel by default - extjs

I am using extjs 3.4
I would like to have preselected node when treepanel first loads. In my example this would be second node. Same thing as mouse click on that node - just it happens by default on selected node.
So, how can I simulate mouse click on second node? (or just make it active item by default)
Ext.ns('App.Misc.West');
App.Misc.West.View = Ext.extend(Ext.Panel, {
constructor: function (config) {
var me = this;
var cfg = {
items: [{
xtype: 'treepanel',
ref: 'treeMisc',
id: 'treeMisc',
root: new Ext.tree.AsyncTreeNode({
expanded: true,
text: 'Data tables',
children: [{
text: 'Books',
bookTitle: 'Books',
leaf: true
}, {
text: 'Cars',
bookTitle: 'Cars',
leaf: true
}]
}),
rootVisible: true
}]
};
Ext.apply(cfg, config);
App.Misc.West.View.superclass.constructor.call(me, cfg);
}
});

You can use checked (multiple selection with checkbox) or select programmatically in the 'load' and node 'expand' handlers:
new Ext.tree.TreePanel({
region: 'west',
collapsible: true,
title: 'Navigation',
xtype: 'treepanel',
width: 200,
autoScroll: true,
split: true,
loader: new Ext.tree.TreeLoader({
listeners: {
}
}),
root: new Ext.tree.AsyncTreeNode({
expanded: true,
text: 'Data tables',
children: [{
text: 'Books',
bookTitle: 'Books',
leaf: true
}, {
text: 'Cars',
bookTitle: 'Cars',
leaf: true,
//checked: true // <-- selected with checkbox
}]
}),
rootVisible: false,
listeners: {
// Single selection (simulated mouse click)
load: function (treeLoader, node) {
this.getRootNode().on('expand', function (node) {
this.getSelectionModel().select(this.getRootNode().childNodes[1]);
}, this);
}
},
renderTo: Ext.getBody()
});

Related

how to create a child in a tree view in ext.js

I need to create a child in a tree view how to do that kindly help. I have tried like this but it's not working.
var myLocations = new Ext.Panel({
title: 'my work',
id: 'student',
height: 100,
autoScroll: true,
iconCls: 'myPanel',
// ,autoLoad: { url: 'UserLocDetails.aspx?UserName=' + strUser }
children: [{
text: "Leaf node (<i>no folder/arrow icon</i>)",
title: 'dasdasd',
leaf: true,
id: 'myLoc',
qtitle: 'Sample Tip Title',
qtip: 'Tip body'
}]
});
You need to create a TreeStore in order to store data for a tree, and some component that can display a tree, for example a TreePanel. Try the following code, it is for Ext JS 7.3.0 Classic Material, but can be adopted to other versions:
Ext.define('myTreeStore', {
extend: 'Ext.data.TreeStore',
root: {
expanded: true
},
proxy: {
type: 'memory',
reader: {
type: 'json'
}
},
data: {
children: [{
text: 'Main 1',
leaf: true
}, {
text: 'Main 2',
children: [{
text: 'Sub 21',
leaf: true
},{
text: 'Sub 22',
leaf: true
}]
}, {
text: 'Main 3',
leaf: true
}]
}
})
Ext.application({
name: 'Fiddle',
launch: function () {
const treeStore = Ext.create('myTreeStore');
Ext.create('Ext.tree.Panel', {
rootVisible: false,
renderTo: Ext.getBody(),
title: 'Tree Example',
width: 400,
store: treeStore,
});
}
});

ExtJS6: Second instance of TreeStore has no grandchildren

ExtJS6: Second instance of TreeStore has no grandchildren.
I have a class, MyStore, which inherits from TreeStore.
Then, I created an instance of MyStore to be displayed in a treepanel.
So far so good.
Then, I created another instance of MyStore to be displayed in another treepanel.
Here comes the problem: the second treepanel does not have grandchild. All it has are it's root node and it's root node's children.
How do I code this so that the 2nd (and succeeding) instance of TreeStore will have grandchildren?
https://fiddle.sencha.com/#fiddle/1ehq
Ext.define('MyStore',{
extend: 'Ext.data.TreeStore',
root: {
expanded: true,
text: 'This is the root node',
children: [
{
text: 'Food',
expanded: true,
children: [
{
text: 'Cake',
leaf: true
},
{
text: 'Ice cream',
leaf: true
}
]
},
{
text: 'Toys',
expanded: true,
children: [
{
text: 'Ball',
leaf: true
},
{
text: 'Bat',
leaf: true
}
]
}
]
}
});
Ext.widget('treepanel',{
title: 'first tree panel',
renderTo: Ext.getBody(),
store: new MyStore()
});
Ext.widget('treepanel',{
title: 'second tree panel',
renderTo: Ext.getBody(),
store: new MyStore()
});
Ext.widget('panel', {
html: 'What happend to the "Food" and "Toys" of the second tree panel? Why did they loose their children?',
renderTo: Ext.getBody(),
});
Use this Fiddle:https://fiddle.sencha.com/#fiddle/1hti
Ext.define('MyStore',{
extend: 'Ext.data.TreeStore'
});
var store1 = Ext.create('MyStore',{
extend: 'Ext.data.TreeStore',
root: {
expanded: true,
text: 'This is the root node',
children: [
{
text: 'Food',
expanded: true,
children: [
{
text: 'Cake',
leaf: true
},
{
text: 'Ice cream',
leaf: true
}
]
},
{
text: 'Toys',
expanded: true,
children: [
{
text: 'Ball',
leaf: true
},
{
text: 'Bat',
leaf: true
}
]
}
]
}
});
var store2 = Ext.create('MyStore',{
extend: 'Ext.data.TreeStore',
root: {
expanded: true,
text: 'This is the root node',
children: [
{
text: 'Food',
expanded: true,
children: [
{
text: 'Cake',
leaf: true
},
{
text: 'Ice cream',
leaf: true
}
]
},
{
text: 'Toys',
expanded: true,
children: [
{
text: 'Ball',
leaf: true
},
{
text: 'Bat',
leaf: true
}
]
}
]
}
});
Ext.widget('treepanel',{
title: 'first tree panel',
renderTo: Ext.getBody(),
store: store1
});
Ext.widget('treepanel',{
title: 'second tree panel',
renderTo: Ext.getBody(),
store: store2
});
Ext.widget('panel', {
html: 'What happend to the "Food" and "Toys" of the second tree panel? Why did they loose their children?',
renderTo: Ext.getBody(),
});

Extjs tree wont appendChold to selected node in tree panel

I have a tree panel, and I am trying to add a bellow the selected item.
http://jsfiddle.net/zer8oLo7/
If you click on "Add To Root", it is working.
But. if you click on "Add To Selected", it doesn't work:
Ext.application({
name: 'Fiddle',
launch: function () {
var store = Ext.create('Ext.data.TreeStore', {
root: {
expanded: true,
children: [{
text: "detention",
leaf: true
}, {
text: "homework",
expanded: true,
children: [{
text: "book report",
leaf: true
}, {
text: "algebra",
leaf: true
}]
}, {
text: "buy lottery tickets",
leaf: true
}]
}
});
Ext.create('Ext.tree.Panel', {
//title: 'Simple Tree',
itemId: 'projectTree',
viewConfig: {
plugins: {
ptype: 'treeviewdragdrop'
}
},
id: 'projectTree',
width: 200,
height: 500,
store: store,
renderTo: Ext.getBody(),
dockedItems: [{
xtype: 'toolbar',
dock: 'top',
items: [{
text: 'Add To Selected',
handler: function () {
var treeNode = Ext.getCmp('projectTree').getSelectionModel().getSelection()
treeNode.appendChild({
text: 'Child 4',
leaf: false
});
}
}, {
text: 'Add to root',
handler: function () {
var treeNode = Ext.getCmp('projectTree').getRootNode();
treeNode.appendChild({
text: 'Child 4',
leaf: false
});
}
}]
}],
});
}
});
getSelection() somehow returns an array of nodes, so you need to take only first node:
...
treeNode[0].appendChild({
text: 'Child 4',
leaf: false
});
...

how to create ExtJs 4 Tree inside accordion using store?

I have an application with possibly 4 level menu,
i want to implement it with Accordion for first level and a tree inside each accordion panel.
how could i implement this with a store?
http://i.stack.imgur.com/DFQDl.png
Here is the working example :
ExtJS - Treepanel inside the Accordion Menu
Ext.onReady(function () {
Ext.create('Ext.panel.Panel', {
title: 'Accordion Layout',
width: 300,
height: 300,
defaults: {
// applied to each contained panel
bodyStyle: 'padding:15px'
},
layout: {
// layout-specific configs go here
type: 'accordion',
titleCollapse: false,
animate: true,
activeOnTop: true
},
items: [{
title: 'Panel 1',
id: 'panelOne',
html: 'Panel content!'
}, {
title: 'Panel 2',
html: 'Panel content!'
}, {
title: 'Panel 3',
html: 'Panel content!'
}],
renderTo: Ext.getBody()
});
var store = Ext.create('Ext.data.TreeStore', {
root: {
expanded: true,
children: [
{ text: "detention", leaf: true },
{ text: "homework", expanded: true, children: [
{ text: "book report", leaf: true },
{ text: "algebra", leaf: true}
] },
{ text: "buy lottery tickets", leaf: true }
]
}
});
Ext.create('Ext.tree.Panel', {
title: 'Simple Tree',
id: 'treePanel',
header: false,
width: 200,
height: 150,
store: store,
rootVisible: false,
renderTo: Ext.getBody()
});
var panel = Ext.getCmp('panelOne');
var tree = Ext.getCmp('treePanel');
panel.add(tree);
});
Ali,
here is the modified edition, hope it's right!
ExtJS - TreePanel inside the Panel
Ext.define('AccModel', {
extend: 'Ext.data.Model',
fields: ['id','text']
});
var accordion = Ext.create('Ext.panel.Panel', {
width: 400,
height: 620,
id: 'accordionPanel',
defaults: {
bodyStyle: 'padding:15px'
},
layout: {
type: 'accordion',
titleCollapse: false,
animate: true,
activeOnTop: false
},
renderTo: Ext.getBody()
});
var addAccordion = function() {
Ext.Ajax.request({
url: 'treenode.json',
success: function(response) {
var nodes = Ext.JSON.decode(response.responseText);
Ext.each(nodes, function(node) {
accordion.add({
title: node.id,
id: node.id,
items: Ext.create('Ext.tree.Panel', {
header: false,
width: 380,
height: 600,
rootVisible: false,
root: {
expanded: false,
children: node.children
}
})
});
})
}
});
}
var init=function() {
addAccordion();
}
Ext.onReady(function () {
init();
});

Sencha Touch 2.0 — How can I hide my list item?

Given a list and a segmentedButton in my panel's items:
[{
xtype:'segmentedbutton',
id:'segmented-btn',
items: [{
ui:'action',
text: 'A',
pressed: true
},{
ui:'action',
text: 'B',
}]
},{
xtype: 'list',
id: 'toList',
scrollable: false,
data: [],
itemTpl: ['<div id="{title}-item">{title}</div>']
},{
xtype: 'list',
id: 'fromList',
ui: 'round',
data: [],
itemTpl: ['<div id="{title}-item">{title}</div>']
}]
When the segmented button A is pressed, the 'To' list item will be hidden and when my segmented button B is pressed, the 'From' list item will be hidden and the 'To' list item will shown.
Adding a listener function to the segmented button
listeners: {
toggle: function(container, button, pressed){
if(button.text == 'A'){
this.down('#fromList').show();
this.down('#toList').hide();
} else {
this.down('#fromList').hide();
this.down('#toList').show();
}
}
}
Should work if the handler function doesn't.
If you you use Segmented Button You have to specify the hidden:true
In which list you need initially you dont specify the hidden:true and the othe second list you have to specify hidden:true
[{
xtype: 'segmentedbutton',
id: 'segmented-btn',
items: [{
ui: 'action',
text: 'A',
id: 'Abutton', // i added
pressed: true
}, {
ui: 'action',
text: 'B',
id: 'Bbutton', // i added
}]
}, {
xtype: 'list',
id: 'toList',
scrollable: false,
data: [],
itemTpl: ['<div id="{title}-item">{title}</div>']
}, {
xtype: 'list',
id: 'fromList',
ui: 'round',
hidden: true, // i added
data: [],
itemTpl: ['<div id="{title}-item">{title}</div>']
}]
**Now in Controller Code**
'button[action=Abutton]': {
tap: 'Abutton',
},
'button[action=Bbutton]': {
tap: 'Bbutton',
},
// now Action
Abutton: function () //first segment button
{
Ext.getCmp('fromList').hide();
Ext.getCmp('toList').show();
},
Bbutton: function () // second segment button
{
Ext.getCmp('toList').hide();
Ext.getCmp('fromList').show();
}
Try this it will work

Resources