【问题标题】:why my combo's store does not delete record为什么我的组合商店不删除记录
【发布时间】:2014-07-14 18:20:46
【问题描述】:

我有一个树面板。树中的每个节点都有一个复选框。当用户检查节点时,我将节点的文本作为选项添加到组合框中。这就像一个魅力。

当用户取消选中某个节点时,我想从组合框中删除相应的选项。有时它会删除,有时则不会。这几天我一直在拔头发。我究竟做错了什么?谢谢。

这是我的控制器中的初始化函数:

 init: function () {
     this.control({
         "#problemsTree": {
             load: this.selectFirstProblem,
             select: this.showProblemDetail,
             checkchange: this.handleCheckChange
         },
         "#run-problems-button": { click: this.runSelectedProblems },
         "#stop-problems-button": { click: this.stopSelectedProblems }
     });
 }

这是同一个控制器中的handleCheckChange函数:

toggleLogOption: function(record, isChecked) {
         var logStore = Ext.StoreManager.lookup("logs-store");
         if(isChecked && logStore.find("text", record.data.text) == -1) {
             logStore.add(record)
         } else if(!isChecked) {
             logStore.remove(record)
         }
         logStore.sync();
     },

 handleCheckChange: function(node, isChecked) {
     if(node.isLeaf()) { 
         var record = Ext.create("GiipIq.model.Log", {id: node.data.id, text: node.data.text});
         this.toggleLogOption(record, isChecked);
     } else {
         node.cascadeBy(function(nd) { 
                 nd.set("checked", isChecked); 
                 if(nd.isLeaf()) {
                     var record = Ext.create("GiipIq.model.Log", {id: nd.data.id, text: nd.data.text});
                     this.toggleLogOption(record, isChecked);
                 }
             }, 
             this    
         );
     }
 },

这是我的日志组合:

Ext.define("GiipIq.view.Log", {
         extend: "Ext.window.Window",
         alias: "widget.logwindow",
         titleAlign: "center",
         closable: false,
         maximizable: true,
         draggable: false,
         resizable: false,
         overflowX: "hidden",
         border: false,
         layout: 'fit',
         x: (Ext.getBody().getViewSize().width/2) + 2,
         y: 0,
         width: (Ext.getBody().getViewSize().width/2) - 5,
         height: Ext.getBody().getViewSize().height/2,

         initComponent: function () {
             this.items = [{
                 xtype: "panel",
                 itemId: "logPanel",
                 title: "Live Logs ",
                 tools:[{
                     xtype:"combo",
                     width: 250,
                     emptyText: "Filter logs",
                     id: "logFilter",
                     store: Ext.create("GiipIq.store.Logs"),
                     queryMode: "local",                                                                                                                                                                                                     
                     displayField: "text",
                     valueField: "id"
                 }]  
             }]; 
             this.callParent(arguments);
         }   
     });

这是我的日志存储:

Ext.define("GiipIq.store.Logs", {
         extend: "Ext.data.Store",
         storeId:"logs-store",
         model: "GiipIq.model.Log",

         sorters: [{ property: "text", direction: "ASC" }]                                                                                                                                                                                   
     });

这是我的日志模型:

Ext.define("GiipIq.model.Log", {                                                                                                                                                                                                        
         extend: "Ext.data.Model",
         idProperty: "text",
         fields: [
             { name: "id", type: "string"},
             { name: "text", type: "string" }
         ],  

         proxy: {
             type: "localstorage",
             id: "proxy-key",
             listeners: {
                 exception: function(proxy, response, operation, opts) {
                     if(typeof(operation.error) == "string") {
                         Ext.Msg.alert("Error", "Connection to server interrupted" + operation.error);
                     }   
                 }   
             }   
         }   
     });

【问题讨论】:

    标签: extjs


    【解决方案1】:

    即使您尝试删除,您总是会创建新记录。逻辑应该依赖于 id,它们在树和组合中似乎是相同的,并且在删除时,您应该尝试通过 id 找到组合中的记录并将其删除。仅在添加时创建新记录。

    【讨论】:

    • 我将 remove(record) 更改为 removeAt(index),一切都像一个魅力。我认为 remove(record) 函数中有一个错误,因为我的调试消息清楚地表明 remove(record) 确实被执行了。感谢您的帮助。
    • 很高兴你把它整理好了。如果有帮助,请采纳答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2013-01-19
    • 1970-01-01
    • 2016-02-28
    相关资源
    最近更新 更多