2013-03-26 25 views
1

在我的應用程序中,我有一個列表和詳細信息(表單)。我想要在單擊列表項時將數據加載到Detail視圖(將數據設置爲表單的textfields)。對於列表和詳細信息,我都從遠程服務器獲取數據。我正在關注MVC。加載數據以形成mvc

現在,當單擊listItem時,我能夠從服務器獲取數據並將其保存到存儲並顯示詳細視圖。但我無法將數據從商店綁定到文本框中。

型號

Ext.define('App.model.Details', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields: [ 
      {name: 'Name', type: 'string'}, 
      {name: 'BillingStreet', type: 'string'}, 
      {name: 'BillingCity', type: 'string'} 

     ] 
    } 
}); 

商店

Ext.define('App.store.Details', { 
    extend: 'Ext.data.Store', 
    config: { 
     model: 'App.model.Details', 
     autoLoad :true, 

     grouper : function(record) { 
      return record.get('Name')[0]; 
     }, 
    } 
}); 

列表視圖

Ext.define('App.view.View', { 
    extend: 'Ext.List', 
    alias:'widget.contactlist', 
    fullscreen: true, 
    id: 'contactlist', 
    config:{ 
     disableSelection:true, 
     store:'Contacts', 
     itemTpl:'{Name}', 
     items:[ 
     { 
      xtype:'toolbar', 
      docked:'top', 
      title:'Leeds List' 
     } 

     ] 
    } 
}); 

詳圖

Ext.define("App.view.ListDetail", { 
    extend: "Ext.form.Panel", 
    requires: "Ext.form.FieldSet", 
    alias: "widget.listDetail", 
    config:{ 
     scrollable:'vertical' 
    }, 
    initialize: function() { 

     this.callParent(arguments); 



     var topToolbar = { 
      xtype: "toolbar", 
      docked: "top", 
      title: "Details" 

     }; 


     this.add([ 
        topToolbar, 
        { xtype: "fieldset", 
         items: [{ 
          xtype: 'textfield', 
          store: 'Details', 
          value : 'Name', 
          label: 'Name' 
         }, 
         { 
          xtype: 'emailfield', 
          store: 'Details', 
          value : 'BillingStreet', 
          label: 'Email' 
         }, 
         { 
          xtype: 'passwordfield', 
          store: 'Details', 
          value : 'BillingCity', 
          label: 'Password' 
         } 

         ] 
        } 
       ]); 


    } 

});

控制器

Ext.define('App.controller.Main', { 

    extend: 'Ext.app.Controller', 

     config: { 
     refs: { 
      // We're going to lookup our views by xtype. 
      contactlist: "contactlist", 
      contactdetail: "listDetail", 
      //f_name:"#f_name" 


     }, 
     control: { 
      contactlist: { 
       // The commands fired by the notes list container. 
       itemtap: "oneditLeadCommand" 

      } 

     }, 

     routes: { 
      'contactlist': 'activateList' 
     } 
    }, 

    activateList: function() 
    { 
     Ext.Viewport.animateActiveItem(this.getContactlist(), this.slideRightTransition); 
    }, 

    slideLeftTransition: { type: 'slide', direction: 'left' }, 
    slideRightTransition: { type: 'slide', direction: 'right' }, 

    oneditLeadCommand: function (list, index, target, record, e, eOpts) 
    { 

     console.log("onEditLead"+record.data.Id); 
     this.activateLeadDetail(record); 


    }, 

    activateLeadDetail: function (record) 
    { 

     var contactDetail = this.getContactdetail(); 
     //console.log("activateLeadDetail"+contactDetail.textfield); 
     //contactDetail.setRecord(record); // load() is deprecated. 
     //this.getF_name().setDisplayField(""); 


     store = Ext.StoreMgr.get('Details'); 
     //console.log("activateLeadDetail"+store); 
     store.setProxy({ 
     type: 'ajax', 
     url : 'http://10.0.2.2:8080/SalesForce/leads/get-lead-details/00D90000000jvoU!AR4AQB6Xcjz4UNBKf12WOcYHWc31QxK2.fXTcbCvOq.oBosCrjBezhqm8Nqc1hrf8MKK5LjLAu8ZC5IqB1kdpWvJGLdWd5pJ/'+record.data.Id, // the json file that holds all our contact info. 
     reader: { 
      type: 'json' 
      } 
     }); 
     store.load(); 
     var record1 = Ext.StoreMgr.get('Details').getAt(0); 
     console.log("activateLeadDetail"+record1); 

     Ext.StoreMgr.get('Details').each(function(test){ 
      console.log("for loop"+test.data); 
     }); 
     contactDetail.setRecord(record1); 
     Ext.Viewport.animateActiveItem(contactDetail, this.slideLeftTransition); 
    }, 
    // Base Class functions. 
    launch: function() { 
     this.callParent(arguments); 

     console.log("launch"); 
    }, 
    init: function() { 
     this.callParent(arguments); 
     console.log("init"); 
    } 
}) 

請幫忙將數據綁定到詳細信息視圖。

回答

4

所以我猜你的聯繫人商店是在其他地方定義的,但是因爲這個工作正常,所以你沒有在這裏粘貼代碼。

因此,在模型上的一個快速註釋,你應該在總是定義一個idProperty。這是Sencha在內部用於定義商店「主鍵」的功能,因此在您重新加載/刷新商店時可正常使用。

Ext.define('App.model.Details', { 
    extend: 'Ext.data.Model', 
    config: { 
     idProperty: 'Name', // Or something from your server maybe? 
     fields: [ 
      {name: 'Name', type: 'string'}, 
      {name: 'BillingStreet', type: 'string'}, 
      {name: 'BillingCity', type: 'string'} 

     ] 
    } 
}); 

其次,你爲什麼要使用初始化方法在ListDetail視圖當你在你的ListView使用的配置方法?如果指定的配置相反,你將能夠通過執行類似

items: [ 
    { 
     xtype: 'ListDetail', 
     anyOtherAttribute: value 
    } 
] 

重用一些該組件的更簡單的方法在其他地方,但還有點兒超出範圍在這裏。但不管怎麼說。所以我認爲這裏有什麼問題是您爲面板的每個字段定義了一個商店。對不起,我不能測試我的假設,但這裏是我會做什麼:

Ext.define("App.view.ListDetail", { 
    extend: "Ext.form.Panel", 
    requires: "Ext.form.FieldSet", 
    alias: "widget.listDetail", 
    config:{ 
     scrollable:'vertical' 
     items:[ 
      { 
       xtype: "toolbar", 
       docked: "top", 
       title: "Details" 
      }, 
      { 
       xtype: "fieldset", 
       itemId: 'detailedListFiledset', //Overall, prefer itemId 
       items: [ 
        { 
         xtype: 'textfield', 
         value : 'Name', 
         label: 'Name' // may be you want placeholders here? 
        }, 
        { 
         xtype: 'emailfield', 
         value : 'BillingStreet', 
         label: 'Email' // and here.. 
        }, 
        { 
         xtype: 'passwordfield', 
         value : 'BillingCity', 
         label: 'Password' // and here.. 
        } 

        ] 
      } 
     ] 
    } 
}); 

好了,現在的問題似乎是在你的控制器:

  • 添加裁判您的字段集
  • 添加引用您的商店
  • 創建後負荷回調時,你的店被加載(詳細信息)
  • 請清除店每次或追加數據,並適用於過濾,以獲得正確的記錄(這是爲什麼idProperty是非常有用的)
  • 設置字段集的記錄和不是面板

我還沒有嘗試的機會,但我會做到這一點今晚稍後。但儘管如此,你還是可以試試。

- 編輯 -

好,我終於得到了安倍晉三爲你代碼的東西。

有幾個問題出現在您的代碼中。我不知道你爲什麼需要兩家商店,但是讓我們假設你是這樣做的。我會給你我使用的所有文件(兩個商店,兩個模型,三個視圖和控制器)。代碼中存在三個主要錯誤:

  • 您不應加載第二個存儲並嘗試獲取記錄。使用和「加載」或「刷新」事件
  • 表單的SetValues是使用的正確函數
  • 您在表單中缺少名稱屬性,以便表單知道商店/模型的哪個值綁定到該字段。

ContactModel:

Ext.define('App.model.Contact', { 
    extend: 'Ext.data.Model', 
    config: { 
     idProperty: 'id', 
     fields: [ 
      {name: 'id', type: 'int'}, 
      {name: 'name', type: 'string'} 
     ] 
    } 
}); 

ContactStore的:

Ext.define('App.store.ContactStore', { 
    extend: 'Ext.data.Store', 
    requires:['App.model.Contact'], 
    config: { 
     storeId: 'ContactStore', 
     model: 'App.model.Contact', 
     autoLoad :true, 
     data: [ 
      {id: 0, name: 'Foo'}, 
      {id: 1, name: 'Bar'} 
     ] 
    } 
}); 

DetailModel:

Ext.define('App.model.Detail', { 
    extend: 'Ext.data.Model', 
    config: { 
     idProperty: 'id', 
     fields: [ 
      {name: 'id', type: 'int'}, 
      {name: 'name', type: 'string'}, 
      {name: 'billingStreet', type: 'string'}, 
      {name: 'billingCity', type: 'string'} 
     ] 
    } 
}); 

DetailStore:

Ext.define('App.store.DetailStore', { 
    extend: 'Ext.data.Store', 
    config: { 
     model: 'App.model.Detail', 
     autoLoad :true, 
     data: [ 
      {id: 0, name: 'Foo', billingStreet:'here', billingCity: 'Somewhere'}, 
      {id: 1, name: 'Bar', billingStreet:'there', billingCity: 'Somewhere else'} 
     ] 
    } 
}); 

ContactView:

Ext.define('App.view.ContactList', { 
    extend: 'Ext.List', 
    xtype: 'contactList', 
    fullscreen: true, 

    config: { 
     itemId: 'contactList', 
     store:'ContactStore', 
     emptyText: 'test', 
     itemTpl: new Ext.XTemplate(
      '{name}' 
     ), 
     items:[ 
      { 
       xtype:'toolbar', 
       docked:'top', 
       title:'Leeds List' 
      } 
     ] 
    } 
}); 

的DetailView:

Ext.define('App.view.Detail', { 
    extend: 'Ext.form.Panel', 
    requires: ['Ext.form.FieldSet'], 
    xtype: "detail", 
    config:{ 
     scrollable:'vertical', 
     items: [ 
      { 
      xtype: 'toolbar', 
      docked: 'top', 
      title: 'Details' 
      }, 
      { 
      xtype: 'fieldset', 
      itemId: 'detailForm', 
      items: [{ 
       xtype: 'textfield', 
       store: 'Details', 
       name: 'name', 
       placeHolder : 'Name', 
       label: 'Name' 
       }, 
       { 
       xtype: 'textfield', 
       store: 'Details', 
       placeHolder : 'BillingStreet', 
       name: 'billingStreet', 
       label: 'BillingStreet' 
       }, 
       { 
       xtype: 'textfield', 
       store: 'Details', 
       placeHolder : 'BillingCity', 
       name: 'billingCity', 
       label: 'BillingCity' 
       } 
      ] 
      } 
     ] 
    } 
}); 

主要觀點:

Ext.define('App.view.Main', { 
    extend: 'Ext.Container', 
    xtype: 'main', 
    config: { 
     layout: 'hbox', 
     items: [ 
      { 
       xtype: 'contactList', 
       flex:1 
      }, 
      { 
       xtype: 'detail', 
       flex:2.5 
      } 
     ] 
    } 
}); 

主控制器:

Ext.define('App.controller.Main', { 
    extend : 'Ext.app.Controller', 
    requires: [ 
     'Ext.Toolbar', 
     'Ext.List', 
     'App.store.ContactStore', 
     'App.store.DetailStore', 
     'App.view.Detail', 
     'App.view.ContactList' 
    ], 
    config: { 
     //@private 
     detailStore: null, 
     currentListIndex: -1, 
     views : [ 
      'App.view.ContactList', 
      'App.view.Detail' 
     ], 
     refs: { 
      list: 'contactList', 
      detail: 'detail', 
      detailForm: 'detail #detailForm' 
     }, 
     control: { 
      list: { 
       itemtap: 'handleItemTapList' 
      } 
     } 
    }, 
    launch: function() { 
     var store = Ext.getStore('DetailStore'); 
     store.on('refresh', 'handleDetailStoreLoad', this); 
     this.setDetailStore(store); 
    }, 
    handleItemTapList: function(list, index, target, record) { 
     this.setCurrentListIndex(index); 
     this.getDetailStore().load(); 
    }, 
    handleDetailStoreLoad: function (store) { 
     debugger; 
     var record = store.getAt(this.getCurrentListIndex()); 
     this.getDetail().setValues(record.data); 
    } 

}); 

我們可以爭論幾件事情,但我試圖直截了當地讓它工作。如果你有更多的問題,請詢問,但這個例子正在爲我工​​作。在我看來,您可能不需要第二家商店,因爲聯繫詳情可以嵌套在COntact商店中,您可以使用商店的hasMany屬性。

+0

嗨皮埃爾,我是Sencha新手,可以請用源代碼解釋我 – 2013-03-27 04:56:00

+0

請看到上面的編輯,如果這是好的! – Pierre 2013-03-27 21:33:30

+0

感謝你的時間和精力 – 2013-03-28 05:20:28

1

使用form.setRecord()來記錄加載到形式

確保你的表單元素具有匹配的型號名稱性能

因爲你有一個商店,你將擁有該名稱的屬性值請使用getAt()或find()方法在商店中獲取或找到一個模型

+0

他正在做它的控制器「contactDetail.setRecord(record1);」但自從他開始在商店之前的幾行前調用.load(),並且因此商店刪除所有數據之後,該商店中肯定沒有記錄。我最好的選擇是record1是不確定的? – Pierre 2013-03-26 18:17:13

+0

在ExtJS 4.2.2中沒有setRecord()方法,只是loadRecord()http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.form.Basic-method-loadRecord – 2014-06-18 12:31:58