sencha touch create tabs from store data - extjs

I am new to Senscha Touch and I have been struggling with this for hours.
I am trying to create an application where the main page has 1-3 tabs where title and content(html5) depends on json data.
My model has two fields: "title" and "text".
My store has dummy data for 2 tabs:
Ext.define('***', {
extend: 'Ext.data.Store',
config: {
model: '***',
xtype: 'informationTabs',
data: [
{
title: 'Museum',
text: 'Random html5'
},
{
title: 'Museum_2',
text: 'Random html5_2'
},
{
title: 'Museum_3',
text: 'Random html5_2'
}
]
}
})
To show it as a list i have the following code:
Ext.define('***', {
extend: 'Ext.Panel',
xtype: 'informationsTabs',
config: {
title: 'InformationTabs',
layout: 'fit',
items: [
{
xtype: 'list',
store: 'InformationTabs',
itemTpl: '{title}, {text}'
}
]
}
});
How do I get that to instead of making a list with two items, create two tabs with their title and text inside?
So in this case i should have two tabs.
Tab 1: Title = "title_1", content = "random_html5"
Tab 2: Title = "title_2", content = "random_html5_2"
Update:
With the following code (thanks kevhender!) it "works", except i get an extra "[object Object]" as the first tab. This option is also the only one with blue background when you click that tab.
Also this.callParent(); gets "Unresolved function or method".
Ext.define('***', {
extend: 'Ext.TabPanel',
xtype: 'informationsTabs',
config: {
title: 'informationsTabs',
items: [
{
xtype: 'tabbar',
store: 'InformationTabs',
title: '',
html: ['']
}
]
},
initialize: function() {
var items = [];
Ext.getStore('InformationTabs').each(function(rec) {
items.push({
title: rec.get('title'),
html: rec.get('text')
});
});
this.setItems(items);
this.callParent();
} });
screenshot: http://oi41.tinypic.com/2gsn53p.jpg

Since the store is dynamic, you won't be able to do the full definition in your static config block. You could put the tab creation into the initialize method, something like this:
Ext.define('***', {
extend: 'Ext.tab.Panel',
xtype: 'informationsTabs',
config: {
title: 'InformationTabs',
layout: 'fit',
items: [
{
xtype: 'list',
store: 'InformationTabs',
itemTpl: '{title}, {text}'
}
]
},
initialize: function() {
var items = [];
Ext.getStore('InformationTabs').each(function(rec) {
items.push({
title: rec.get('title'),
html: rec.get('text')
});
});
this.setItems(items);
this.callParent();
}
});

Related

Fujitsu HPC Gateway Addon - How To Use Separate Files

How can I split the logic for an HPC Gateway Addon into separate files? i.e. I'd like to do something like this:
Main.js
Ext.define('App.view.main.Main', {
extend: 'Ext.tab.Panel',
items: [{
title: 'Stuff',
xtype: 'stuff',
iconCls: 'x-fa fa-object-group'
}, {
title: 'About',
xtype: 'about',
iconCls: 'x-fa fa-info-circle',
}]
});
Stuff.js
Ext.define('App.widget.Stuff', {
extend: 'Ext.panel.Panel',
alias: 'widget.stuff',
html: '<h1>Stuff'
});
About.js
Ext.define('App.widget.About', {
extend: 'Ext.panel.Panel',
alias: 'widget.about',
html: '<h1>Version: x.y.z'
});
According to the official fujitsu docs for creating an addon, there are no examples of separating your logic into multiple files, just two monolithic files which contain the view & the controller logic:
appModule.js
Ext.define('desktop.addons.app_abcdef12.view.appModule', {
extend: 'desktop.core.utils.Module',
requires:[],
/**
* Create window
* #param taskBar the task bar
* #returns {desktop.core.view.utils.ModuleView} the module view
*/
createWindow: function (taskBar) {
var me = this;
return Ext.create('desktop.core.view.utils.ModuleView', {
title: me.title,
iconCls: me.iconCls,
stateId: 'app-module',
name: 'app-window',
taskBar: taskBar,
multipleView: false,
width: 320,
height: 150,
items:me.buildItems(),
buttons: [
]
});
},
/**
* Build items
* #returns {{xtype: string, layout: {type: string, align: string}, items: *[]}} items in json format
*/
buildItems:function(){
return {
xtype: 'panel',
layout: 'center',
items: [{
xtype: 'textfield',
name: 'value',
fieldLabel: desktop.core.utils.Symbols.getText('app_abcdef12.textfield.input')+" *",
allowBlank:false,
listeners: {
change: function(textfield, newValue){
var window = textfield.up('window');
window.fireEvent('textfieldChanged', textfield, newValue);
}
}
}]
};
}
});
appController.js
Ext.define('desktop.addons.app_abcdef12.controller.appController', {
extend: 'desktop.core.controller.abstract.AbstractToriiController',
requires: [],
control: {
'window[name=app-window]': {
textfieldChanged: 'performTextfieldChanged'
}
},
performTextfieldChanged: function (textfield, newValue) {
if (Ext.isEmpty(newValue)) {
textfield.up('window').down('button[name=save]').disable();
} else {
textfield.up('window').down('button[name=save]').enable();
}
}
});
When I tried to create something like desktop.addons.app_abcdef12.view.Stuff with an xtype of widget.stuff I crashed the HPC instance, and had to delete the addon listed in mongodb and restart the services. This seriously reduces the number of times I can use trial and error to find out what works.
Anybody done this before?
In the end I didn't manage to get anything working using xtype but I did when using Ext.create directly:
appModule.js
buildItems:function(){
return {
xtype: 'tabpanel',
items: [
Ext.create('desktop.addons.app_abcdef12.view.Stuff'),
Ext.create('desktop.addons.app_abcdef12.view.About'),
]
};
}
Stuff.js
Ext.define('desktop.addons.app_abcdef12.view.Stuff', {
title: 'Stuff',
extend: 'Ext.panel.Panel',
iconCls: 'x-fa fa-id-card',
html: '<h1>Stuff'
});
About.js
Ext.define('desktop.addons.app_abcdef12.view.About', {
title: 'About',
extend: 'Ext.panel.Panel',
iconCls: 'x-fa fa-info',
html: '<h1>About'
});

Sencha Touch: How Can I pass Data from a view to Controller and Controller to Next View

I want to pass the an ID from a Tabview (myView1) to a Controller(theController) then after that I want that Controller class to pass data to another Tabview (myView2) at the same Panel. Anyone has any Idea, how can I do this?
You can do like this
in your controller:
config:{
refs:{
myView1: "myView1Ref",
myView2: "myView2Ref"
},
controls:{
myView1:{
idgiven: "onIdGiven"
}
}
},
onIdGiven: function(id){
this.getMyView2().setData(yourData);
}
in MyView1, when you want to give the id, just do
this.fireEvent(yourId);
This is my Tab Panel View class (MyTabPanel)
Ext.define('MyApp.view.MyTabPanel', {
extend: 'Ext.tab.Panel',
config: {
items: [
{
xtype: 'container',
title: 'Tab 1',
id: 'tab1',
items: [
{
xtype: 'textfield',
label: 'User'
},
{
xtype: 'container',
height: 46,
margin: '5px',
items: [
{
xtype: 'button',
ui: 'action',
width: 173,
text: 'Send Data'
}
]
}
]
},
{
xtype: 'container',
title: 'Tab 2',
id: 'tab2',
tpl: [
'{id}'
]
},
{
xtype: 'container',
title: 'Tab 3',
id: 'tab3'
}
]
}
});
This is my Controller :
Ext.define('MyApp.controller.MyController', {
extend: 'Ext.app.Controller',
config: {
refs: {
Tab1: '#tab1',
Tab2: '#tab2',
Tab3: '#tab3'
},
control: {
"Tab2": {
activate: 'onTabpanelActivate1'
}
}
},
onTabpanelActivate1: function(newActiveItem, container, oldActiveItem, eOpts) {
var Rec = [{id:1}]; // this should come from my #tab1 tab and set to #tab2 tab
this.getTab2().setRecord(Rec); // So this is the problem I am facing
}
});
Ext.define('myapp.controller.theController', {
extend: 'Ext.app.Controller',
config: {
refs: {
views: [
'myapp.view.myView1',
'myapp.view.myView2',
],
refMyView1: 'myView1',
refMyView2: 'myView2',
},
control: {
'button[action=submit]':{ // on tab of submit button
tap: 'submitTapHandler'
},
},
},
submitTapHandler: function(){
var view1data = this.getRefMyView1().getValues(); // view1data contain all field data of view1 in record form
//controller to view pass data
var myView2 = Ext.create('myapp.view.myView2');
// create a record that key value have name of fields and value to pass to that field
var record;
// set record key value
myView2.setRecord(record);
}
})

Sencha touch 2 - Show data in DataView

I am new in ExtJS, and I already, have an example of doctors list but, in doctor detail page, Its look like a populated form, Here is my view file:
Ext.define("GS.view.DoctorView", {
extend: "Ext.form.Panel",
requires: "Ext.form.FieldSet",
alias: "widget.doctorview",
config:{
scrollable:'vertical'
},
initialize: function () {
this.callParent(arguments);
var backButton = {
xtype: "button",
ui: "back",
text: "Back List",
handler: this.onBackButtonTap,
scope: this
};
var topToolbar = {
xtype: "toolbar",
docked: "top",
title: "Doctor Profile",
items: [
backButton,
{ xtype: "spacer" }
]
};
var bottomToolbar = {
xtype: "toolbar",
docked: "bottom",
items: [
]
};
var doctorNameView = {
xtype: 'textfield',
name: 'title',
label: 'Title',
required: true
};
var doctorDescView = {
xtype: 'textareafield',
name: 'narrative',
label: 'Narrative'
};
this.add([
topToolbar,
{ xtype: "fieldset",
items: [doctorNameView, doctorDescView]
},
bottomToolbar
]);
},
onBackButtonTap: function () {
this.fireEvent("backToListCommand", this);
}
});
Which steps I take to convert it to dataview, or how to use my custom HTML?
Firstly, your question is very vague so you should go through some dataview tutorial or article to understand why you really need a data view here because list is a dataview and form is what you are creating as details view.
http://www.sencha.com/blog/dive-into-dataview-with-sencha-touch-2-beta-2
http://docs.sencha.com/touch/2-1/#!/guide/dataview
To use your custom HTML you have to use xtemplate
http://docs.sencha.com/touch/2-1/#!/api/Ext.XTemplate

Get an Ext.dataview.List instance to show up inside of an Ext.tab.Panel instance

I cannot figure out how to show my Ext.dataview.List object inside of another object. If I try to show the Ext.dataview.List object in the viewport, by itself, I can see my data. But if I try to include it as a component inside of another component, it thinks there is no data (judging from the HTML output), and doesn't appear to display anything.
So, if I have an app.js with the following code:
Ext.application({
name: 'MobileUnion',
views: ['LocalsList'],
models: ['Local'],
stores: ['Locals'],
launch: function() {
// Initialize the main view
var mainView = {
xtype: 'localslist'
};
Ext.Viewport.add([mainView]);
}
});
I can set up MobileUnion.view.LocalsList this way, which results in no list:
Ext.define('MobileUnion.view.LocalsList', {
extend: 'Ext.Container',
alias: 'widget.localslist',
requires: 'Ext.dataview.List',
config: {
items: [
{
itemId: 'localsList',
store: 'Locals',
loadingText: 'Loading Locals...',
emptyText: '<div>No locals found.</div>',
onItemDisclosure: true,
itemTpl: '<div>{localName}</div><div>{designation}</div>'
}
]
}
});
Or, I can set it up this way, which results in a populated list, but not inside of another component:
Ext.define('MobileUnion.view.LocalsList', {
extend: 'Ext.dataview.List',
alias: 'widget.localslist',
config: {
itemId: 'localsList',
store: 'Locals',
loadingText: 'Loading Locals...',
emptyText: '<div>No locals found.</div>',
onItemDisclosure: true,
itemTpl: '<div>{localName}</div><div>{designation}</div>'
}
});
The reason I am trying to pull this off is so that I can get a list component to display inside of a tab panel.
Note: I have assembled a Fiddle with everything, here: http://www.senchafiddle.com/#6nCH8
I must be missing something basic, but at this point, having gone through the documentation, and purchased two books, one of which I've read fully, and the other of which I am reading while coding, I am still without an answer. Any help would be most greatly appreciated.
Additional Code
app/model/Local.js
Ext.define('MobileUnion.model.Local', {
extend: 'Ext.data.Model',
config: {
idProperty: 'id',
fields: [
{ name: 'id', type: 'int' },
{ name: 'dateCreated', type: 'date', dateFormat: 'c' },
{ name: 'localName', type: 'string' },
{ name: 'designation', type: 'string' }
],
}
});
app/store/Locals.js
Ext.define('MobileUnion.store.Locals', {
extend: 'Ext.data.Store',
requires: ['MobileUnion.model.Local'],
config: {
model: 'MobileUnion.model.Local',
data: [
{ localName: 'ABCD', designation: '123' },
{ localName: 'WXYZ', designation: '456' }
],
autoLoad: true,
sorters: [{ property: 'dateCreated', direction: 'DESC'}]
}
});
In option 1, you define a container but you actually don't add items to you container. To add your list to the container, add xtype: 'list' to your items array. Next thing you have to do is to set the containers layout to fit, otherwise the list won't show up. This should work for you:
Ext.define('MobileUnion.view.LocalsList', {
extend: 'Ext.Container',
alias: 'widget.localslist',
requires: 'Ext.dataview.List',
config: {
layout: {
type: 'fit' //set containers layout
},
items: [
{
xtype: 'list', //add xtype
itemId: 'localsList',
store: 'Locals',
loadingText: 'Loading Locals...',
emptyText: '<div>No locals found.</div>',
onItemDisclosure: true,
itemTpl: '<div>{localName}</div><div>{designation}</div>'
}
]
}
});
More about layouts in Sencha: http://docs.sencha.com/touch/2-1/#!/guide/layouts
Good luck!

show window in tabpanel

I am working on extjs4, my case is:
Extjs mvc is used to build my application, viewport is the toppest container of my application, west region is a tree, center region is a tabpage container, when click the tree item, a new page with certain content will be created. then in this page, I popup a model window, this model window just mask the page, not the whole viewport, so that I can still click the tree item to open another new page.
I have achieved this, but there is a problem, if I have already open a model window in a tab, and I switch to a another tab then return back, the model window is hidden, but I still want that window to show if I haven't closed it. Can anyone help me, is there a better way except using ifram in tabpage?
app.js:
Ext.application({
name: 'SysOpv',
appFolder: '/Js/AppSysOpv/app',
autoCreateViewport: true,
controllers: [
'Category',
'Band'
]
});
Viewport:
Ext.define('SysOpv.view.Viewport', {
extend: 'Ext.container.Viewport',
layout: 'fit',
initComponent: function() {
this.items = {
dockedItems: [{
dock: 'top',
xtype: 'toolbar',
height: 80,
items: [
{ xtype: 'component', html: 'setup' }
]
}],
layout: {
type: 'hbox',
align: 'stretch'
},
items: [{
width: 250,
xtype: 'categorytree'
}, {
id: 'maintabpanel',
flex: 1,
xtype: 'tabpanel'
}]
};
this.callParent(arguments);
}
});
Tree View:
Ext.define('SysOpv.view.category.Tree', {
extend: 'Ext.tree.Panel',
alias: 'widget.categorytree',
title: 'setup',
rootVisible: false,
useArrows: true,
hideHeaders: true,
columns: [{
flex: 1,
xtype: 'treecolumn',
text: 'Name',
dataIndex: 'name'
}],
store: 'Category',
initComponent: function() {
this.callParent(arguments);
}
});
Window View:
Ext.define('SysOpv.view.edit.Band', {
extend: 'Ext.window.Window',
alias: 'widget.editband',
title: 'Setup',
layout: 'fit',
constrain: true,
modal: true,
initComponent: function() {
this.items = [{
xtype: 'form',
bodyPadding: 10,
items: [{
xtype: 'textfield',
name: 'name',
fieldLabel: 'Name'
}]
}];
this.buttons = [{
text: 'Save',
action: 'save'
}, {
text: 'Cancel',
scope: this,
handler: this.close
}];
this.callParent(arguments);
}
});
Tree Controller:
Ext.define('SysOpv.controller.Category', {
extend: 'Ext.app.Controller',
models: [ 'Category' ],
stores: [ 'Category' ],
views: [ 'category.Tree' ],
init: function() {
this.control({
'categorytree': {
itemdblclick: this.onTreeItemdblclick
}
});
},
onTreeItemdblclick: function (tree, record, item, index, e, eOpts) {
var mainTabs = Ext.getCmp('maintabpanel');
var tabId = record.get('id');
if (mainTabs) {
var checkTab = mainTabs.getComponent(tabId);
if (checkTab) {
mainTabs.setActiveTab(checkTab);
} else {
var controller;
var list;
switch (tabId) {
case '0101':
list = Ext.widget('listband');
break;
}
if (list)
{
var tabPage = mainTabs.add({
id: record.get('id'),
title: record.get('name'),
closable: true,
layout: 'fit',
items: [ list ]
});
mainTabs.setActiveTab(tabPage);
}
}
}
}
});
Module Controller:
Ext.define('SysOpv.controller.Band', {
extend: 'Ext.app.Controller',
models: [ 'Band' ],
stores: [ 'Band' ],
views: [ 'list.Band', 'edit.Band' ],
init: function() {
this.control({
'listband button[action=edit]': {
click: this.onEdit
}
});
},
onEdit: function(button, e, eOpts) {
var edit = Ext.widget('editband');
var list = button.up('gridpanel');
if (list.getSelectionModel().hasSelection()) {
var record = list.getSelectionModel().getLastSelected();
// I use renderTo here but have no effect,
// so I search in the google find a way to show the window in tab,
// and not mask all the viewport.
button.up('#0101').add(edit);
edit.down('form').loadRecord(record);
edit.show();
} else {
console.log('Not selected');
}
}
});
Below is example solution:
Ext.create('Ext.TabPanel', {
renderTo: 'container',
items: [
{
title: 'Tab 1',
itemId: 'tab1',
items: [
{ xtype: 'button', text: 'Show window', handler: function(){
var tab = this.up('#tab1'); // Find tab
var win = Ext.widget('editband'); // Find window
this.up('tabpanel').showWindow(tab, win);
} }
]
},
],
showWindow: function(tab, w){
tab.add(w);
tab.popup = w;
w.on('close', function() { // clean up after window close
delete this.popup;
}, tab, { single: true });
w.show();
},
listeners: {
tabchange: function(panel, tab) {
if (tab.popup !== undefined) { // show window after tab change
tab.popup.show();
}
}
}
});
Basically I've created event handler for tabchange event in which I re-show window.
Working sample: http://jsfiddle.net/aCxYU/1/

Resources