how to build tree in EXTJS? - extjs

How to build a tree in EXTJS ? It has to include the images(with '+' & '-' symbols) with respective node.Can you get me the code for the same ????

Start by defining an Ext.data.TreeStore to load your data into:
var store = Ext.create('Ext.data.TreeStore', {
proxy: {
type: 'ajax',
url: 'treeData.json'
},
root: {
text: 'Countries',
expanded: true
}
});
Json:
[{
"text": "United Kindom",
"children": [{
"text": "Glasgow",
"leaf": true
}, {
"text": "Edinburgh",
"leaf": true
}, {
"text": "London",
"leaf": true
}],
"leaf": false
},
{
"text": "France",
"leaf": true
}
...
]
Create the Tree Panel and render it to the document's body:
Ext.create('Ext.tree.Panel', {
title: 'Countries & Cities',
width: 500,
height: 300,
store: store,
useArrows: false,
rootVisible: false,
renderTo: Ext.getBody(),
style: 'margin: 50px'
});

Just look at the source code for any of the Ext JS Tree demos.
For example:
Ext.onReady(function(){
// shorthand
var Tree = Ext.tree;
var tree = new Tree.TreePanel({
useArrows: true,
autoScroll: true,
animate: true,
enableDD: true,
containerScroll: true,
border: false,
// auto create TreeLoader
dataUrl: 'get-nodes.php',
root: {
nodeType: 'async',
text: 'Ext JS',
draggable: false,
id: 'src'
}
});
// render the tree
tree.render('tree-div');
tree.getRootNode().expand();
});

This looks like a good example: static tree for a static tree.
Saki makes a lot of tutorials and examples that are very helpful for EXTJS. One of Saki's Examples is an asynchronous tree. You can find it by looking to the left under state.
This seems like a good tutorial for a dynamic tree with a Ruby on Rails backend: dynamic tree with RoR backend.

Related

ExtJS 3.4: how to display plus/minus icons for TreeNode

The documentation for the TreeNode object lists a Config option named expandable:
expandable : Boolean
If set to true, the node will always show a plus/minus icon, even when
empty
I am creating a number of non-leaf objects the following way:
treeNodes[tag] = new Ext.tree.TreeNode({
text : tag,
leaf : false,
expanded : false,
expandable : true,
loaded : true
});
But after adding them to the root TreeNode the result is something like:
Which gets users complaining for having to double-click to expand a node. How can I get the plus/minus buttons as seen in this example?
There is no wrong in the code snippet that you have pasted here. There may be some config in TreePanel which you might be added in your code can cause the problem. May the config like 'iconCls'
Working fiddle is here... Plus and Minus in ExtJS tree
Ext.onReady(function() {
new Ext.tree.TreePanel({
title: 'Simple Tree',
width: 200,
height: 150,
rootVisible: false,
renderTo: Ext.getBody(),
root: {
expanded: true,
children: [{
text: "detention",
leaf: false,
expanded: false,
expandable: true,
loaded: true
}, {
text: "homework",
expanded: true,
children: [{
text: "book report",
leaf: true
}, {
text: "alegrbra",
leaf: true
}]
}, {
text: "buy lottery tickets",
leaf: true
}]
}
});
});
The version used was 3.4.0. Check your version and code.

angularjs kendo datasource data length zero

I'm trying to get my grid (created by angular directive) populated with data, but it is not populating.
Here's my grid declaration:
<div id="dayTypesGrid" kendo-grid k-options='dayTypesGridOptions'></div>
Here's code from my angular controller:
$scope.dayTypesGridOptions = {
schema: {
model: {
id: "description",
fields: {
description: { editable: false, nullable: false },
numberOfDays: { type: "number", validation: { required: true, min: 0} }
}
}
},
columns: [{
field: "description",
title: "Day Type"
}, {
field: "numberOfDays",
title: "Number of Days"
}]
};
So far so good. The grid is instantiated, and I see the two columns. And my angular controller executes my data fetch call, and I store the result in an array $scope.viewModel.dayTypes.
Using the browser dev tools, I can see that $scope.viewModel.dayTypes indeed contains the array of 7 records.
I now want to display those 7 records in the grid, and I do so as follows:
var ds = new kendo.data.DataSource({data: $scope.viewModel.dayTypes});
$('#dayTypesGrid').data('kendoGrid').dataSource = ds;
After that last line of code, my grid still remains empty. Using the browser dev tools, I see that ds.data.length is zero.
What am I missing?
What you're looking for is the setDataSource method:
$('#dayTypesGrid').data('kendoGrid').setDataSource(ds);
Here is how I do it in my project and this works for me. Your mileage may vary because you're using data instead of a transport URL, but I noticed the structure of your grid configuration is a bit different than mine. For example, I define the schema when I create my datasource:
var datasource = new kendo.data.DataSource({
transport: {
read: {
url: "url/to/my/service",
dataType: "json"
}
},
schema: {
data: "results"
}
};
var gridConfig: {
dataSource: dataSource,
rowTemplate: $templateCache.get("modules/reports/row.html"),
height: 500,
scrollable: true,
groupable: true,
filterable: true,
pageable: true,
reorderable: true,
sortable: true,
resizable: true,
"columns": [
{
"field": "status",
"title": "Status",
"width": 200
},
{
"field": "actions",
"title": "Actions",
"width": 200
}
]
}
};

Grid data not being displayed in ExtJs app [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm using ExtJs 4.1.0 and I'm trying to display a grid of data backed by a remote json store. When I render the grid like so:
Ext.Loader.setConfig({enabled:true});
Ext.Loader.setPath({
'Grip':'app'
,'Ext.ux':'ext/examples/ux'
});
Ext.require([
'Grip.view.EditableGrid'
,'Grip.store.UYNMeetingTypes'
,'Grip.view.UYNMeetingGrid'
,'Grip.model.UYNMeeting'
,'Grip.store.UYNMeetings'
,'Ext.ux.CheckColumn'
]);
Ext.require([
'Ext.panel.*',
'Ext.toolbar.*',
'Ext.button.*',
'Ext.container.ButtonGroup',
'Ext.layout.container.Table',
'Ext.tip.QuickTipManager'
]);
Ext.onReady(function() {
var store = new Grip.store.UYNMeetings();
grid = new Grip.view.UYNMeetingGrid({
store: store
,renderTo: Ext.getBody()
});
store.load({
// store loading is asynchronous, use a load listener or callback to handle results
callback: function(){
Ext.Msg.show({
title: 'Store Load Callback',
msg: 'store was loaded, data available for processing',
modal: false,
icon: Ext.Msg.INFO,
buttons: Ext.Msg.OK
});
}
});
});
it works fine. However, when I try to link things up using the manner suggested in the MVC tutorial (http://www.sencha.com/learn/architecting-your-app-in-ext-js-4-part-2/) I can't seem to get my data to display. I'm running ExtJs 4.1.0 on Google Chrome 20.0.1132.47. Any ideas?
I tried to post only the minimally relevant bits of code (though it still seems like a lot). Let me know if there's anything I can clarify. Any help would be greatly appreciated. Thanks!
Grip.controller.UYNMeeting:
Ext.define('Grip.controller.UYNMeeting', {
extend: 'Ext.app.Controller',
refs: [{
ref: 'meetingGrid',
selector: 'uynmeetinggrid'
}],
stores: ['UYNMeetings','UYNMeetingTypes'],
init: function() {
},
onLaunch: function() {
var meetingsStore = this.getUYNMeetingsStore();
meetingsStore.load({
callback: this.onMeetingsLoad,
scope: this
});
var meetingTypesStore = this.getUYNMeetingTypesStore();
meetingTypesStore.load({
callback: this.onMeetingTypesLoad,
scope: this
});
},
onMeetingsLoad: function() {
},
onMeetingTypesLoad: function() {
Ext.Msg.show({
title: 'Store Load Callback',
msg: 'store was loaded, data available for processing',
modal: false,
icon: Ext.Msg.INFO,
buttons: Ext.Msg.OK
});
}
});
Grip.view.UYNMeetingGrid:
var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
clicksToMoveEditor: 1,
autoCancel: false
});
Ext.define('Grip.view.UYNComboBox', {
extend:'Ext.form.field.ComboBox'
,alias:'widget.uyncombo'
,stores:['UYNMeetingTypes']
})
Ext.define('Grip.view.UYNMeetingGrid', {
extend:'Ext.grid.Panel'
,alias:'widget.uynmeetinggrid'
,stores:['UYNMeetings']
,title:'UYN Meetings Grid'
,hideHeaders: true
,bodyPadding:5
,width:550
,height:400
,autoScroll: true
,initComponent:function(){
this.columns = [{
"width": 150,
"text": "Name",
"sortable": true,
//"id": "name",
"dataIndex": "name",
editor: {
allowBlank: false,
xtype: 'textfield'
}
},
{
"width": 150,
"text": "Org.",
"sortable": true,
//"id": "org",
"dataIndex": "org",
editor: {
allowBlank: true,
xtype: 'textfield'
}
},
{
"width": 100,
"text": "Date",
"sortable": true,
//"id": "date",
"dataIndex": "date",
editor: {
xtype: 'datefield',
allowBlank: false,
format: 'm/d/Y',
maxText: 'Cannot have a meeting date in the future!',
maxValue: Ext.Date.format(new Date(), 'm/d/Y')
}
},
{
"width": 75,
"text": "Meeting Type",
"sortable": true,
//"id": "meeting_type",
"dataIndex": "meeting_type"/*,
editor: {
xtype: 'uyncombo'
}*/
},
{
"width": 75,
"text": "Num Hours",
"sortable": true,
//"id": "num_hours",
"dataIndex": "num_hours",
editor: {
allowBlank: false,
// step: '.1',
xtype: 'numberfield'
}
}];
this.dockedItems = [{
dock:'bottom'
,xtype:'toolbar'
,items: [{
xtype:'button'
,text:'New'
,action:'newuynmeeting'
}, {
xtype:'button'
,text:'Edit'
,action:'edituynmeeting'
}, {
xtype:'button'
,text:'Remove'
,action:'removeuynmeeting'
}]
}];
this.callParent();
}
,plugins: [rowEditing]
,listeners: {
'selectionchange': function(view, records) {
// grid.down('#removeEmployee').setDisabled(!records.length);
}
}
});
Grip.view.Viewport:
Ext.define('Grip.view.Viewport', {
extend: 'Ext.container.Viewport',
layout: 'fit',
requires: [
'Grip.view.ContactsPanel'
,'Grip.view.UYNMeetingForm'
,'Grip.view.UYNMeetingGrid'
,'Grip.view.NavBar'
],
renderTo: 'app',
tbar:{
xtype:'mynavbar'
},
items:[{
xtype:'tabpanel',
items:[{
title:'Contacts',
xtype:'tabpanel',
items:[{
title:'Contacts'
,xtype:'contactspanel'
},{
title:'Add Contact'
,html:'TODO: Add content'
}
]
},{
title:'UYN',
xtype:'tabpanel',
items:[{
title:'UYN Meetings'
//,html:'Foo'
,xtype:'uynmeetinggrid'
}]
}]
}]
});
I don't know if it's a typo, but a grid doesn't have a stores config - it can only display the data of one store, thus it has a store config (singular, not plural)

UnCaught SyntaxError with extjs 4

I have been getting UnCaught SyntaxError : Unexpected Token : .
I'm really puzzled where this would come from. When i use the sample json it works
http://www.sencha.com/forum/topics-browse-remote.php
The issue maybe that i should use httpProxy. But i'm unsure how to incorporate this.
Ext.Loader.setConfig({ enabled: true });
Ext.Loader.setPath('Ext.ux', '../ux/');
Ext.require([
'Ext.grid.*',
'Ext.data.*',
'Ext.util.*',
'Ext.toolbar.Paging',
'Ext.ux.PreviewPlugin',
'Ext.ModelManager',
'Ext.tip.QuickTipManager'
]);
Ext.onReady(function () {
Ext.tip.QuickTipManager.init();
Ext.define('ForumThread', {
extend: 'Ext.data.Model',
fields: [
'title', 'threadid'
],
idProperty: 'threadid'
});
// create the Data Store
var store = Ext.create('Ext.data.Store', {
pageSize: 50,
model: 'ForumThread',
remoteSort: true,
proxy: {
type: 'jsonp',
//url: 'http://www.sencha.com/forum/topics-browse-remote.php',
url: 'https://www.estore.localhost/usergrid/indexgrid',
reader: {
root: 'topics',
totalProperty: 'totalCount'
},
// sends single sort as multi parameter
simpleSortMode: true
},
sorters: [{
property: 'title',
direction: 'DESC'
}]
});
// pluggable renders
function renderTopic(value, p, record) {
return "test";
}
var pluginExpanded = true;
var grid = Ext.create('Ext.grid.Panel', {
width: 700,
height: 500,
title: 'ExtJS.com - Browse Forums',
store: store,
disableSelection: true,
loadMask: true,
viewConfig: {
id: 'gv',
trackOver: false,
stripeRows: false,
plugins: [{
ptype: 'preview',
bodyField: 'excerpt',
expanded: true,
pluginId: 'preview'
}]
},
// grid columns
columns: [{
// id assigned so we can apply custom css (e.g. .x-grid-cell-topic b { color:#333 })
// TODO: This poses an issue in subclasses of Grid now because Headers are now Components
// therefore the id will be registered in the ComponentManager and conflict. Need a way to
// add additional CSS classes to the rendered cells.
id: 'topic',
text: "Topic",
dataIndex: 'title',
flex: 1,
renderer: renderTopic,
sortable: false
}],
// paging bar on the bottom
bbar: Ext.create('Ext.PagingToolbar', {
store: store,
displayInfo: true,
displayMsg: 'Displaying topics {0} - {1} of {2}',
emptyMsg: "No topics to display",
items: [
'-', {
text: 'Show Preview',
pressed: pluginExpanded,
enableToggle: true,
toggleHandler: function (btn, pressed) {
var preview = Ext.getCmp('gv').getPlugin('preview');
preview.toggleExpanded(pressed);
}
}]
}),
renderTo: 'topic-grid'
});
// trigger the data store load
store.loadPage(1);
});
Here is the json
{
"totalCount": "4",
"topics": [{
"threadid": "435",
"title": "55e38867-2b1e-4fc4-8179-5907c1c80136"
}, {
"threadid": "444",
"title": "4975db6c-d9cd-4628-a6e9-ca1d4815d28d"
}, {
"threadid": "529",
"title": "c778e5ea-eb79-42b1-8f13-7cba4600491f"
}, {
"threadid": "530",
"title": "a1024264-9eed-4784-ab91-cf2169151478"
}]
}
jsonP is a special technique for retrieving data from a server in a different domain. The main idea of jsonP is that
JsonPProxy injects a <script> tag into the DOM whenever an AJAX
request would usually be made
So imagine what would happen if proxy processed your json. It would inject <script> tag like this:
<sript>
{
"totalCount": "4",
"topics": [{
"threadid": "435",
"title": "55e38867-2b1e-4fc4-8179-5907c1c80136"
},
// ...
]
}
</script>
When this script is being executed it defenetly throws an exception.
So, like Xupypr MV has already written, you should wrap your outcoming-from-server string into:
myCallback(
//your json here
);
in this case JsonPProxy would inject <script> like this:
<sript>
myCallback({
"totalCount": "4",
"topics": [{
"threadid": "435",
"title": "55e38867-2b1e-4fc4-8179-5907c1c80136"
},
// ...
]
});
</script>
and it would be valid <script> tag.
Now you have to indicate in the store that you are using 'myCallback' as callback function (notice the callbackKey config):
var store = Ext.create('Ext.data.Store', {
// ...
proxy: {
type: 'jsonp',
url: 'https://www.estore.localhost/usergrid/indexgrid',
callbackKey: 'myCallback',
// ...
},
// ...
});
Check out docs for more info.
Notice, original (sencha example) url returns some extra data, not only json.
Make you response look like this:
Ext.data.JsonP.callback1({
"totalCount": "4",
"topics": [{
"threadid": "435",
"title": "55e38867-2b1e-4fc4-8179-5907c1c80136"
}, {
"threadid": "444",
"title": "4975db6c-d9cd-4628-a6e9-ca1d4815d28d"
}, {
"threadid": "529",
"title": "c778e5ea-eb79-42b1-8f13-7cba4600491f"
}, {
"threadid": "530",
"title": "a1024264-9eed-4784-ab91-cf2169151478"
}]
});
I was having the same problem and took me a while to figure it out.
I'm using Sencha Touch 2, which also implements the Ext.data.proxy.JsonP. The Extjs 4 proxy automatically creates a callback parameter each time it is sent to the server, e.g. callback1, callback2, sequentially. It is important to catch this parameter and return it with the json, otherwise the second time it is called it will complain not having a callback1 method.
The section "Implement on the server side" on this page
http://docs.sencha.com/touch/2-0/#!/api/Ext.data.proxy.JsonP
describes how to create callback with the JsonP. I suppose the original example http://www.sencha.com/forum/topics-browse-remote.php also does this.
I was also having the same problem, and was a mistake in server side code. I was using php file but not appended the callback going from frontend. So http://docs.sencha.com/touch/2-0/#!/api/Ext.data.proxy.JsonP is helpful which explains the structure of different types of serverside code for jsonp request
Thanks
Tapaswini

How to form the folder structured tree in EXTJS?

Can anyone give the coding for the formation of folder structured tree in EXTJS ? how can i make it ? can anyone help for the coding of the same ?
http://dev.sencha.com/deploy/dev/examples/tree/reorder.html
Ext.onReady(function(){
// shorthand
var Tree = Ext.tree;
var tree = new Tree.TreePanel({
useArrows: true,
autoScroll: true,
animate: true,
enableDD: true,
containerScroll: true,
border: false,
// auto create TreeLoader
dataUrl: 'get-nodes.php',
root: {
nodeType: 'async',
text: 'Ext JS',
draggable: false,
id: 'src'
}
});
// render the tree
tree.render('tree-div');
tree.getRootNode().expand();
});

Resources