Sencha touch - trying to delete row from list - list

I try to get an editable list with this code:
var isEditing = false;
new Ext.Application({
launch: function(){
new Ext.Panel({
//layout: 'card',
fullscreen: true,
items: new Ext.List({
id: 'myList',
store: new Ext.data.Store({
fields: ['myName'],
data: [{ myName: 1 }, { myName: 2 }, { myName: 3}]
}),
itemSelector: '.x-list-item',
multiSelect: true,
itemTpl: '<span class="name">{myName}</span>',
tpl: new Ext.XTemplate(
'<tpl for=".">' +
'<div class="x-list-item">' +
'<tpl if="this.isEditing()">' +
'<img src="images/delete.gif" ' +
'onclick="Ext.getCmp(\'myList\').myDeleteItem({[xindex-1]})" ' +
'style="vertical-align: middle; margin-right: 15px;"/>' +
'</tpl>' +
'{myName}</div>' +
'</tpl>',
{
compiled: true,
isEditing: function () {
console.log('isEditing (tpl):' + isEditing)
return isEditing;
}
}),
myDeleteItem: function (index) {
var store = this.getStore();
var record = store.getAt(index);
console.log('removing ' + record.data.myName);
store.remove(record);
},
listeners: {
itemtap: function () {
if (isEditing){
console.log('isEditing: ' + isEditing);
return;
}
},
beforeselect: function () {
console.log('isEditing: before ' + !isEditing);
return !isEditing;
}
}
}),
dockedItems: [{
dock: 'top',
xtype: 'toolbar',
layout: { pack: 'right' },
items: [
{
xtype: 'button',
text: 'Edit',
handler: function () {
var list = Ext.getCmp('myList');
if (!isEditing)
list.mySelectedRecords = list.getSelectedRecords();
isEditing = !isEditing;
this.setText(isEditing ? 'Save' : 'Edit');
list.refresh();
if (!isEditing)
list.getSelectionModel().select(list.mySelectedRecords);
}
}]
}]
});
}
});
but its not working like it should. If I press the EDIT button there is no delete-image and so there is no deleted item....

There are 3 things that I can see:
The Template is rendered once, you will need to call .refresh() or .refreshNode() on the list to update any item templates. The better way to accomplish this would be to hide the delete button via CSS and display it when the 'edit' button is clicked.
There is probably a naming conflict between the isEditing variable declared at the top and the isEditing function reference. It is very confusing to have these two things named the same, and can lead to problems with variable scoping.
The click event that you are looking for may be intercepted by the parent list item and Sencha Touch is turning it into a 'itemtap' event on the list item.

I was not able to delete until I added an id field without a datatype to my model. I don't know why as it should know which record to delete via the index.
Ext.regModel('Setting', {
fields: [
{name: 'id'}, // delete works after adding
{name: 'name', type: 'string'}
],
proxy: {
type: 'localstorage',
id: 'settings'
}
Kevin

Related

Refresh list in form panel

I have a list
Ext.define('EvaluateIt.view.RemoveList', {
extend: 'Ext.dataview.List', //'Ext.tab.Panel',
alias : 'widget.removeList',
config: {
width: Ext.os.deviceType == 'Phone' ? null : 300,
height: Ext.os.deviceType == 'Phone' ? null : 500,
xtype: 'list',
store: 'SiteEvaluations', //getRange(0, 9),
itemTpl: [
'<div><strong>Address: {address}</strong></div> '
],
variableHeights: false
}
});
that is rendered in a form panel:
Ext.define('EvaluateIt.view.Remove', {
extend: 'Ext.Container',
fullscreen: true,
config: {
layout: 'vbox',
items: [
{
xtype : 'toolbar',
docked: 'top'
},
{
flex: 1,
xtype: 'removeList'
}
]
}
});
How do I refresh the list if it changes? For example, I have a method that onTap of an item in the list a MsgBox will give the option to delete that record from the store by given index. I tried a load() after doing the removal of the record, but this does not do the refresh (in a browser a refresh does it and in simulator, I have to exit and go back into the app).
Here is the MsgBox:
Ext.Msg.show({
title:'Are you sure?',
buttons: Ext.MessageBox.YESNO,
//animateTarget: 'mb4',
//icon: Ext.MessageBox.WARNING,
fn: function(buttonId) {
//alert('You pressed the "' + buttonId + '" button.');
if (buttonId === 'yes') {
evaluationsStore = Ext.getStore(evaluationStore);
index = evaluationStore.findExact('id', id); // get index of record
console.log('index: ' + index);
evaluationStore.removeAt(index); // remove record by index
evaluationStore.sync();
alert('It is gone!');
Ext.getStore(evaluationStore).load();
}
else {
alert('Unscathed!');
}
}
});
Grazie!
After the record is deleted from the database your list should update automatically. Store will generate 'update' event and list that has store will refresh it content. Are you saying it doesn't work this way for you?

Not getting record on itemtap sencha list

When Taping items the listener is getting invoked but the value is null. My Code:
Ext.define('tablet.SelectCategories', {
extend: 'Ext.navigation.View',
xtype: 'selectcategorypanel',
id: 'SelectCategories',
requires:[
],
initialize:function(){
this.callParent();
var jsonObject = Ext.create('Tablet')
.make_webservice_call_post('get_categories');
Ext.getCmp('select_category_list')
.setData(jsonObject.info);
console.log(jsonObject.info);
},
config: {
//title : 'Select Categories',
//iconCls: 'team',
//styleHtmlContent: true,
// scrollable: true,
layout: {
type: 'card'
},
items: [
{
fullscreen: true,
mode: 'MULTI',
xtype: 'list',
itemTpl: '{name}',
autoLoad: true,
id:'select_category_list',
store: {
fields: ['active','created','description','name']
},
listeners: {
itemtap: function (list, records) {
console.log('Sel');
console.log(records.name);
var names = [];
Ext.Array.each(records, function (item) {
names.push('<li>' + item.data.name + '</li>');
}); // each()
Ext.Msg.alert('You selected ' + records.length + ' item(s)',
'<ul>' + names.join('') + '</ul>');
} // selectionchange
}
// handler:self.itemClick
}
Getting undefined in console.log(records.name);
Your method signature for itemtap is also wrong. It should be -
itemtap: function(list, index, target, record) {
console.log('Item tapped');
console.log(record.get('name'));
// and your rest of the code.
}
Check the documentation for the itemtap event here, and read up more about stores here.

Sencha 2.0 change title toolbar dynamically

I would dynamically change the title of my list titlebar. This is my app.js where you can view my app (see var list where there is the toolbar item and title that I would change)
How can I do this?
Thanks.
var mainForm ;
var mainFormPanel={};
var myStore = Ext.create('Ext.data.Store', {
storeId: 'MyStore',
fields: ['txt']
}); // create()
var list= Ext.create('Ext.List', {
fullscreen: true,
store: 'MyStore',
itemTpl: '{txt}',
items: [{
xtype: 'titlebar',
docked: 'top',
title:'change dinamically this title!!!!'
}] // items (list)
}); // create()
Ext.application({
glossOnIcon: false,
autoMaximize: false,
icon: {
57: 'lib/sencha-touch/resources/icons/icon.png',
72: 'lib/sencha-touch/resources/icons/icon#72.png',
114: 'lib/sencha-touch/resources/icons/icon#2x.png',
144: 'lib/sencha-touch/resources/icons/icon#114.png'
},
phoneStartupScreen: 'lib/sencha-touch/resources/loading/Homescreen.jpg',
tabletStartupScreen: 'lib/sencha-touch/resources/loading/Homescreen~ipad.jpg',
requires: [
'Ext.tab.Panel',
'Ext.form.*',
'Ext.field.*',
'Ext.Button',
'Ext.data.Store'
],
launch: function() {
mainForm = Ext.create('Ext.form.Panel', {
xtype:'formpanel',
items: [
{
xtype: 'textfield',
name : 'distance',
label: 'Distance'
},
{
xtype: 'textfield',
name : 'quantity',
label: 'Quantity'
}
]
});
mainFormPanel={
xtype: 'toolbar',
docked: 'bottom',
layout: {
pack: 'center'
},
items: [
{
xtype: 'button',
text: 'Set Data',
handler: function() {
mainForm.setValues({
distance: '300',
quantity: '25'
})
}
},
{
xtype: 'button',
text: 'Get Data',
handler: function() {
Ext.Msg.alert('Form Values', JSON.stringify(mainForm.getValues(), null, 2));
}
},
{
xtype: 'button',
text: 'Clear Data',
handler: function() {
mainForm.reset();
}
}
]
};
Ext.Viewport.add({
xtype: 'tabpanel',
items: [
{
//each item in a tabpanel requires the title configuration. this is displayed
//on the tab for this item
title: '1-tab',
layout:'fit',
//next we give it some simple html
items: [mainForm,mainFormPanel],
//then a custom cls so we can style it
cls: 'card1'
},
{
//title
title: '2-tab',
layout:'fit',
items: [list],
cls: 'card2'
},
{
//title
title: '3-tabs',
//the items html
items: {
html: 'mia auto',
centered: true
},
//custom cls
cls: 'card3'
}
]
});
}
});
Ext.List component's superclass is Ext.DataView., not Ext.Panel.
Hence, it is not possible to directly add / dock any item inside the Ext.List component.
So, I recommend you to wrap your Ext.List component inside Ext.Panel.
Do it like this,
var myStore = Ext.create('Ext.data.Store', {
storeId: 'MyStore',
fields: ['txt']
}); // create()
var listpanel = new Ext.Panel({
layout: 'fit', // important to make layout as 'fit'
items: [
{
xtype: 'titlebar',
id: 'myTitle',
docked: 'top',
title: 'Before Change title',
items: [
{
xtype:'button',
text:'Change Title',
align:'right',
listeners : {
tap : function() {
Ext.getCmp('myTitle').setTitle('After Title Change');
}
}
}
]
},
{
//Definition of the list
xtype: 'list',
itemTpl: '{txt}',
store: myStore,
}]
});
....
....
{
title: '2-tab',
layout:'fit',
items: [listpanel],
cls: 'card2'
},
....
....
Sample Output :-
Before changing the title
After changing the title
FYI, If you look inside the Ext.List class in Sencha Docs, you will see the following code inside initComponent() method,
if (Ext.isDefined(this.dockedItems)) {
console.warn("List: List is not a Panel anymore so you can't dock items to it. Please put this list inside a Panel with layout 'fit'");
}

Show panel below list sencha touch

I want to place a panel below a list but it seems the panel is always over top of the list. I dont want to set a height on the list as it might have no records or 20 so the height varies.
I simply want my panel to show right below my list. Also note I have disabled scrolling.
Rad.views.testList = Ext.extend(Ext.List, {
fullscreen: true,
layout: 'hbox',
scroll: false,
align: 'top',
store: Rad.stores.test,
itemTpl: Rad.views.testTemplate(),
});
topPanel = new Ext.Panel({
fullscreen: true,
scroll: true,
layout: {
type: 'vbox',
pack: "start",
align: "start"
},
defaults: {
width: '100%',
},
items: [
{
layout: 'hbox',
items: [testList]
},
{
html: 'This will be on the list not below!'
]
});
Ext.apply(this, {
layout: {
type: 'vbox',
pack : 'top',
},
dockedItems: [titlebar],
items : [ topPanel]
});
I should have been using Panels with Data View.
var tpl = new Ext.XTemplate(
'<tpl for=".">',
'<div class="photosLocation">{pic}{name}',
'</div>',
'</tpl>'
);
var panel = new Ext.Panel({
width:535,
autoHeight:true,
layout:'hbox',
title:'Simple DataView',
listeners: {
click: {
element: 'el', //bind to the underlying el property on the panel
fn: function(){ alert('click'); }
}
},
items: new Ext.DataView({
store: Rad.stores.deals,
tpl: tpl,
autoHeight:true,
itemSelector:'div.photosLocation',
})
});

How to select a card in a carousel by menu item in a docked menu list in Sencha touch?

I cannot figure out how I can retrieve a given Data item in a store (id-number) to send it to the "setActiveItem" method in a listener:
So I have a store - model:
Ext.regModel('PictureItem', {
fields: ['id', 'titel', 'url']
});
var pictureItems = new Ext.data.Store({
model: 'PictureItem',
data: [
{id:1, titel:'page 1', url:'http://placekitten.com/1024/768'},
{id:2, titel:'page 2', url:'http://placekitten.com/1024/768'},
{id:3, titel:'page 3', url:'http://placekitten.com/1024/768'},
]
});
Here is my menuList called "leftList":
var leftList = new Ext.List({
dock: 'left',
id:'list1',
width: 135,
overlay: true,
itemTpl: '{titel}',
singleSelect: true,
defaults: {
cls: 'pic'
},
store: pictureItems,
listeners:{
selectionchange: function (model, records) {
if (records[0]) {
Ext.getCmp('karte').setActiveItem(!!!Here the number of the selected Item
or respondend "id" in the data store!!!);
}
}
}
});
and the carousel....
var carousel = new Ext.Carousel({
id: 'karte',
defaults: {
cls: 'card'
},
items: [{
scroll: 'vertical',
title: 'Tab 1',
html: '<img class="orientation" alt="" src="img_winkel/titel_v.jpg">'
},
If I call
Ext.getCmp('karte').setActiveItem(2);
it works with the called card - but how can I get the number from the id of the selected item in the menu List /store????
By the way: what does mean:
if (records[0]) {
why [0]?
I FOUND THE ANSWER FOR MYSELF - IT'S EASY:
Ext.getCmp('karte').setActiveItem(records[0].get('id'), {type: 'slide', direction: 'left'});
The secret to get the record-entry is ".get()":
records[0].get('arrayfield')
So now I can change the activeItem in the carousel easely...