Sencha Touch :How to write different select events for Different li/rows in a list - sencha-touch

I have implemented a list view for different colors in Sencha Touch.
And implemented a item tap method to push the view in container.
And Implemented 4 views for each 4 color RedPage.js, WhitePage.js, yelloPage.js, GreenPage.js.
View:
{
xtype: 'list',
id : 'ColorsList'
width: '20%',
itemTpl: [
'<div><b>{Color}<b></div>'
],
data: [
{Color: 'Red' },
{Color: 'Blue'},
{Color: 'Yellow},
{Color: 'Green}
]
},
Container:
ColorsList: '#ColorsList',
init function(){
..
..
..
..
'ColorsList' : {
itemtap : 'ColorsPagesSelected'
},
...
...
...
},
ColorsPagesSelected Method:
ColorsPagesSelected: function() {
alert('Color page pushed');
this.getColorrightnavigation().push(Ext.create('ArtGalleryApp.view.RedPage'));
},
Since i used Colorslist id and used to push RedPage for list, it displays RedPage for each item in the list.
I dont know how to get different pages for differnt rows
Can any one help me pls thanks in advance

assuming you defined your colored pages like this:
Ext.define('App.view.RedPage', {
extend: 'Ext.Panel',
xtype: 'RedPage',
...
});
use helpful Ext.widget function
ColorsPagesSelected: function(ct, index, target, record) {
alert('Color page pushed');
var color = record.get('Color'),
pageName = color + 'Page',
page = Ext.widget(pageName);
this.getColorrightnavigation().push(page);
}

My favorite hack for this is to assign functions to records
{
xtype: 'list',
id: 'ColorsList'
width: '20%',
itemTpl: ['<div><b>{Color}<b></div>'],
data: [{
Color: 'Red',
getPage: function() {
return Ext.create('ArtGalleryApp.view.RedPage');
}
}, {
Color: 'Blue'
}, {
Color: 'Yellow},
{Color: '
Green
}]
},
init function(){
'ColorsList' : {
itemtap : 'ColorsPagesSelected'
},
ColorsPagesSelected: function(ct, index, target, record) {
alert('Color page pushed');
this.getColorrightnavigation().push(record.getPage());
}

Related

Displaying buttons in a Dataview in Sencha 2

I'm trying to create a view, which has essentially a list where each row has text and 2 buttons aligned right. The text of the buttons will always be the same - only the text of the row will need to be fetched from the store.
I saw this question but couldn't get the proposed solution to work. I definitely have records in my store, but the dataview does not display.
A simplified version of my code:
My main view:
Ext.define('MyApp.view.Main', {
extend: 'Ext.Container',
...
config: {
layout: {
type: 'vbox',
pack: 'center',
align: 'middle'
},
items: [
...
{xtype: 'mylist'}
...
]
...
}
...
});
DataView:
Ext.define('MyApp.view.MyList', {
extend: 'Ext.dataview.DataView',
alias: 'widget.mylist',
requires: ...,
config: {
useComponents: true,
store: 'my-store',
defaultType: 'listitem'
}
});
DataItem:
Ext.define('MyApp.view.ListItem', {
extend: 'Ext.dataview.component.DataItem',
alias: 'widget.listitem',
config: {
layout: ...
items: [{
xtype: 'component',
itemId: 'description',
html: '',
flex: 2
},
{
xtype: 'button',
text: 'a button',
itemId: ...,
flex: ...
},
{
... another button
}]
},
updateRecord: function(record) {
...
this.down('#description').setHtml(record.get('description'));
// record.get('description') does give me an undefined value
}
});
Assuming my stores and models are set up properly, what could the problem be?
Alternatively, maybe I should just use a standard list, but set the width to <100%, and just add 2 buttons to the right for each row. Though I'd rather get the current approach to work...
Edit:
Ended up using the dataMap approach.
My DataItem now looks like:
config: {
layout: {
type: 'hbox',
align: 'center'
},
dataMap: {
getDescription: {
setHtml: 'description'
}
},
description: {
flex: 1
}
},
applyDescription: function(config) {
return Ext.factory(config, Ext.Component, this.getDescription());
},
updateDescription...
Basically like this.
I see the labels now, but am stuck on how to get a button in there. Since I don't want to link it to a store, I don't want to put it in the dataMap. I tried putting it in items, but to no avail.
Edit 2:
Well, getting the button to display was easier than I thought. It's just a repeat of what I did with the label, but without including it in the dataMap - yesButton: {...}, applyYesButton: f(){...}, updateYesButton: f(){...}...
The last issue I need to resolve is, how to uniquely identify them. I want a listener on the button, but somehow pass in the record into the handler.
My solution for getting a button in a dataview without linking it to the store. It's actually, somewhat unsurprisingly, similar to this blog post.
view - ListItem.js
...
config: {
layout: {
type: 'hbox',
align: 'center'
},
dataMap: {
getDescription: {
setHtml: 'description'
}
},
description: {
cls: ...,
flex: 4
},
myButton: {
cls: ...,
text: 'Button!',
flex: 1
}
},
applyDescription, updateDescription (same as in the question),
applyMyButton: function(config) {
return Ext.factory(config, Ext.Button, this.getMyButton());
},
updateMyButton: function(newButton, oldButton) {
... same logic as the other update method
}
In my dataview:
...
config: {
itemId: ...,
store: ...,
useComponents: true,
defaultType: 'listitem',
width: '100%',
flex: 1,
listeners: {
itemtap: function(dataview, index, element, evt) {
var store = dataview.getStore();
var record = store.getAt(index);
...
}
}
}
...

How to add toolbar to the sencha touch 2 list item?

I have sencha touch 2 list , i want to add toolbar to the left of each list item . Is it possible if so how? I referred this link : http://www.sencha.com/blog/dive-into-dataview-with-sencha-touch-2-beta-2, But in my case i have to add toolbar .
It must be like :
toolbar-list item text-closure icon
toolbar-list item text-closure icon
toolbar-list item text-closure icon
toolbar-list item text-closure icon
You can look DataItem documentation. Here the code of example:
Ext.define('MyDataItem', {
extend: 'Ext.dataview.component.DataItem',
alias: 'widget.mydataitem',
config: {
padding: 10,
layout: {
type: 'hbox'
},
defaults: {
margin: 5
},
items: [{
xtype: 'button',
text: 'Val1'
}, {
xtype: 'component',
flex: 1,
html: 'val2',
itemId: 'textCmp'
}]
},
updateRecord: function(record) {
var me = this;
me.down('button').setText(record.get('val1'));
me.down('#textCmp').setHtml(record.get('val2'));
me.callParent(arguments);
}
});
DataItem extends from container. So you can add any component on it. Included Toolbar. Add your toobar in the items array with option docked: 'left'.

Dynamic Number of Radio fields

I'm trying to create a multiple choice class where I pass in the question and anywhere from 2 to 8 possible answers from which the user must choose one. How can I dynamically pass my items to my class, and create a new radiofield for each possible answer? Here's my current class with two possible answers (Red, White)
Ext.define('Sencha.view.question.QuestionTypeOne', {
extend: 'Ext.Container',
xtype: 'question-type-one',
requires: [
'Ext.TitleBar'
],
config: {
height: '250px',
width: '250px',
items: [
{
xtype: 'fieldset',
title: 'What\'s your favorite color?',
instructions: 'Select one',
defaults: {
xtype: 'radiofield',
labelWidth: '40%'
},
items: [
{
name: 'color',
value: 'red',
label: 'Red'
},
{
name: 'color',
value: 'white',
label: 'White'
}
]
}
]
},
initialize: function () {
this.callParent(arguments);
}
});
And it would be invoked something like this?
{
xtype: 'question-type-one',
// question: "What's your favorite color?",
// items: []
}
while creating view for question-type-one you can pass list of questions/answers and in initialize function you can iterate over this data and add items to you fieldset. For this you might have to keep null config q & a in view class.
var question = Ext.create("Sencha.view.question.QuestionTypeOne", {
q : "blah blah blah",
a : {"a1", "a2", "a3"}
});
then in initialize function
initialize: function () {
this.callParent(arguments);
var question = this.config.q;
var answers = this.config.a;
var container = this.down('fieldset');
// now iterate over answers and all container.add(answerdfield);
}

Button Event not getting fired when put inside Tab Panel in Sencha Touch 2

I have put two buttons inside a tab Panel in Sencha Touch 2 application. The code is given below:
var btnAttendance = Ext.create('Ext.Button', {
text: 'Take Attendance',
padding: 10,
width: 200,
handler: function () {
alert('a');
}
});
var btnAddUser = Ext.create('Ext.Button', {
text: 'Add User',
padding: 10,
width: 200,
handler: function () {
alert('a');
}
});
var buttonContainer = Ext.create('Ext.Panel', {
fullscreen: true,
title: 'Home',
defaults: {
margin: 5
},
layout: {
type: 'vbox',
align: 'center'
},
padding: 10,
items: [
btnAttendance,
btnAddUser
]
});
Ext.application({
requires: [
'Ext.tab.Panel'
],
launch: function () {
Ext.Viewport.add({
xtype: 'tabpanel',
items: [
buttonContainer,
{
title: 'Present',
items: {
html: '2',
centered: true
},
cls: 'present'
},
{
title: 'Absent',
items: {
html: '3',
centered: true
},
cls: 'absent'
}
]
});
}
});
I have tried modifying the handler function as:
handler: function (button, event)
But this also doesn't work.
Thanks,
Nitin
You need to put all your code inside Ext.application... and launch:function(){...}.... Your code is working fine. See demonstration here.
But then again, buttonContainer is not really being added to any tab panel. If you change the tab, you can see buttonContainer is gone once to change the tabs. If you want to add it inside any tab do this-
First -
Set fullscreen:false to you buttonContainer panel.
var buttonContainer = Ext.create('Ext.Panel', {
fullscreen: false,
....... //rest of the code.
And suppose you want to add those buttons in Present tab. You can do this with following--
Ext.Viewport.add({
xtype: 'tabpanel',
items: [
{
title: 'Present',
cls: 'present',
items: [
{
html: '2',
centered: true
},
buttonContainer
]
},
{
title: 'Absent',
items: {
html: '3',
centered: true
},
cls: 'absent'
}
]
});
Here, buttonContainer is added as an item inside present tab only. You can switch the tabs and can see buttons there with correct event handler.
See this demo here
If you follow MVC architecture, your job becomes much easier writing application having multiple views and user interactions. ST is all about MVC and it's good practice to follow it.

Change border of grid

I have following code that I creating a grid
var store = Ext.create('Ext.data.ArrayStore', {
fields: [
{ name: 'company' },
{ name: 'price', type: 'float' },
{ name: 'change', type: 'float' },
{ name: 'pctChange', type: 'float' }
],
data: myData
});
var grid = Ext.create('Ext.grid.Panel', {
store: store,
renderTo: 'divGrid',
columns: [
{ text: 'Company',
flex: 1,
dataIndex: 'company'
},
{ text: 'Price',
flex: 1,
dataIndex: 'price'
},
{ text: 'Change',
flex: 1,
dataIndex: 'change'
},
{ text: '% Change',
flex: 1,
dataIndex: 'pctChange'
}],
height: 250,
width: '100%',
title: 'Array Grid',
renderTo: 'grid-example',
viewConfig: {
stripeRows: true
}
});
});
I want to change color and width of border grid. How can I do it ?
Quick and dirty you can set this config on any grid or really any component that draws a box:
style: 'border: solid Red 2px'
The more correct way is to create a css rule and set cls:'myRedBorderRule' in the config.
EDIT:
var grid = Ext.create('Ext.grid.Panel', {
store: store,
renderTo: 'divGrid',
style: 'border: solid Red 2px',
.....
ExtJS Grid Panel class provides you with parameters to define your custom styles. You can make use of the following class parameters :
border
bodyStyle
bodyCls
bodyBorder
bodyPadding
You can use combination of these parameters to manipulate the grid's border and body styles. Refer to the docs for details of these parameters.
Add below css to remove border
.x-panel-body .x-grid-item-container .x-grid-item {
border: none;
}