2014-06-12 43 views
0

如何使用新選項卡和默認選項卡之間的焦點切換來實現TabPanel中的替代添加和刪除選項卡? 目前,我做的:使用焦點切換在TabPanel中添加和刪除選項卡

//adding new tab and set active 
    var tabs = Ext.getCmp('tabs'); 
    var albumTab = { 
     id: 'albumTab', 
     title: 'New album', 
     items: [ 
     { 
      xtype: 'panel', 
      layout: { 
       type: 'vbox', 
       align: 'stretch' 
      }, 
      items: [ 
       uploadForm, 
       albumPhotos 
      ] 
     } 
     ] 
    }; 

    tabs.add(albumTab); 
    var albumTabPanel = tabs.items.findBy(function (i) { 
     return i.id === 'albumTab'; 
    }); 
    tabs.setActiveTab(albumTabPanel); 
    tabs.down('#albumsTab').setDisabled(true); 
    tabs.down('#photosTab').setDisabled(true); 

//removing a new tab and switching on default tab 
    var tabs = Ext.getCmp('tabs'); 
    var albumsTabPanel = tabs.items.findBy(function (i) { 
     return i.id === 'albumsTab'; 
    }); 
    tabs.down('#albumsTab').setDisabled(false); 
    tabs.down('#photosTab').setDisabled(false); 
    tabs.setActiveTab(albumsTabPanel); 
    tabs.remove('albumTab', true); 

當我加入的第一次工作的新標籤。但是,如果我在刪除新標籤頁後執行此操作,我會收到錯誤: 錯誤:el爲空 TypeError:el爲空 el.addCls.apply(el,arguments); 在66799線EXT-全dev.js 錯誤的發生而設置的活動選項卡:

tabs.setActiveTab(albumTabPanel); 

由於

回答

0

全碼:

Ext.application({ 
    name: '', 
    launch: function() { 
     var previews = Ext.create('Ext.Panel',{ 
      layout: 'column', 
      height: 500 
     }); 
     var albumPhotos = Ext.create('Ext.Panel', { 
      layout: 'column', 
      border: 0 
     }); 
     var itemModelStore = Ext.create('ItemModelStore'); 
     itemModelStore.load(function() { 
      itemModelStore.each(function(record){ 
       previews.add({ 
        'id': record.get('id'), 
        'xtype': 'image', 
        'src': record.get('src'), 
        'style' : 'width:171px;height:196px;padding-left:5px;padding-top:5px;cursor:pointer;', 
        listeners: { 
         el: { 
          click: function() { 
           albumId = record.get('id'); 
           fillAlbum(albumId); 
           var tabs = Ext.getCmp('tabs'); 
           var albumTab = { 
            id: 'albumTab', 
            title: 'New album', 
            items: [ 
            { 
             xtype: 'panel', 
             layout: { 
              type: 'vbox', 
              align: 'stretch' 
             }, 
             items: [ 
              uploadForm, 
              albumPhotos 
             ] 
            } 
            ] 
           }; 

           tabs.add(albumTab); 
           var albumTabPanel = tabs.items.findBy(function (i) { 
            return i.id === 'albumTab'; 
           }); 
           tabs.setActiveTab(albumTabPanel); 
           tabs.down('#albumsTab').setDisabled(true); 
           tabs.down('#photosTab').setDisabled(true); 
          } 
         } 
        } 
       }); 
      }); 
      previews.doLayout(); 
      albumPhotos.doLayout(); 
     }); 

     Ext.create('Ext.container.Viewport', { 
      layout: { 
       type: 'vbox', 
       align: 'stretch' 
      }, 
      items: [ 
       { 
        title: '', 
        items: [ 
        { 
         xtype: 'tabpanel', 
         id: 'tabs' 
        } 
        ] 
       } 
      ] 
     }); 
     var albumsTab = { 
      title: 'Album', 
      id: 'albumsTab', 
      items: [ 
      { 
       xtype: 'panel', 
       layout: { 
        type: 'vbox', 
        align: 'stretch' 
       }, 
       items: [ 
       { 
        html: '<b><a onclick="return newAlbumWindowShow()" href="">Create album</a></b>' 
        + '<br/><br/>' 
       }, previews 
       ] 
      } 
      ] 
     }; 
     var photosTab = { 
      title: 'Photos', 
      id: 'photosTab', 
      html: 'blabla 2<br/><b><a onclick="newAlbumWindowShow()" href="#">Create album</a></b>' 
     }; 
     displayStartPage = function() { 
      var tabs = Ext.getCmp('tabs'); 
      tabs.add(albumsTab); 
      tabs.add(photosTab); 
     }(); 

     onStartPage = function() { 
      var tabs = Ext.getCmp('tabs'); 
      var albumsTabPanel = tabs.items.findBy(function (i) { 
       return i.id === 'albumsTab'; 
      }); 
      tabs.down('#albumsTab').setDisabled(false); 
      tabs.down('#photosTab').setDisabled(false); 
      tabs.setActiveTab(albumsTabPanel); 
      tabs.remove('albumTab', true); 
     } 
     var fillAlbum = function(albumId) { 
      Ext.define('AlbumStore', { 
       extend: 'Ext.data.Store', 
       model: 'ItemModel', 
       proxy: { 
        type: 'ajax', 
        url: 'php/readAlbum.php', 
        method: 'POST', 
        reader:{ 
         type:'json', 
         root: 'objs' 
        } 
       } 
      }); 
      var albumStore = Ext.create('AlbumStore'); 
      albumStore.load({ 
       params:{ 
        id: albumId 
       }, 
       callback: function() { 
        albumStore.each(function(record) { 
         albumPhotos.add({ 
          'xtype': 'image', 
          'src': record.get('src'), 
          'style': 'width:200px;height:200px;padding-left:5px;padding-top:5px;' + 
          'padding-bottom:15px;cursor:pointer;' 
         }); 
        }); 
       } 
      }); 
     } 

    } 
}); 
相關問題