简体   繁体   English

Sencha touch - 试图从列表中删除行

[英]Sencha touch - trying to delete row from 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....如果我按下 EDIT 按钮,则没有删除图像,因此没有删除的项目....

There are 3 things that I can see:我可以看到 3 件事:

  1. The Template is rendered once, you will need to call.refresh() or.refreshNode() on the list to update any item templates.模板渲染一次,您需要在列表上调用 .refresh() 或 .refreshNode() 来更新任何项目模板。 The better way to accomplish this would be to hide the delete button via CSS and display it when the 'edit' button is clicked.完成此操作的更好方法是通过 CSS 隐藏删除按钮,并在单击“编辑”按钮时显示它。

  2. There is probably a naming conflict between the isEditing variable declared at the top and the isEditing function reference.顶部声明的 isEditing 变量和 isEditing function 引用之间可能存在命名冲突。 It is very confusing to have these two things named the same, and can lead to problems with variable scoping.将这两个东西命名相同是非常令人困惑的,并且可能导致变量范围的问题。

  3. 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.您正在查找的单击事件可能会被父列表项拦截,Sencha Touch 正在将其转换为列表项上的“itemtap”事件。

I was not able to delete until I added an id field without a datatype to my model.在我向 model 添加一个没有数据类型的 id 字段之前,我无法删除。 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凯文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM