ExtJS4學習筆記十--Grid使用

一、grid的例: 
Javascript代碼  收藏代碼
  1. //配置表格列  
  2. {header: "姓名", width: 50, dataIndex: 'name'},  
  3. {header: "組長", width: 50, dataIndex: 'leader',   
  4.     xtype: 'booleancolumn',//Ext.grid.column.Boolean布爾列  
  5.     trueText: '是',  
  6.     falseText: '否'  
  7. },  
  8. {header: "生日", width: 100, dataIndex: 'birthday',   
  9.     xtype : 'datecolumn',//Ext.grid.column.Date日期列  
  10.     format : 'Y年m月d日'//日期格式化字符串  
  11. },  
  12. {header: "薪資", width: 50, dataIndex: 'salary',   
  13.     xtype: 'numbercolumn',//Ext.grid.column.Number數字列  
  14.     format:'0,000'//數字格式化字符串  
  15. }  
Javascript代碼  收藏代碼
  1. xtype: 'actioncolumn',//Ext.grid.column.Action動作列  
  2. items: [{  
  3.     icon: 'images/edit.gif',//指定編輯圖標資源的URL   
  4.     handler: function(grid, rowIndex, colIndex) {  
  5.         //獲取被操作的數據記錄  
  6.         var rec = grid.getStore().getAt(rowIndex);  
  7.         alert("編輯 " + rec.get('name'));  
  8.     }  
  9. },{  
  10.     icon: 'images/del.gif',//指定編輯圖標資源的URL   
  11.     handler: function(grid, rowIndex, colIndex) {  
  12.         var rec = grid.getStore().getAt(rowIndex);  
  13.         alert("刪除 " + rec.get('name'));  
  14.     }                  
  15. },{  
  16.     icon: 'images/save.gif',//指定編輯圖標資源的URL   
  17.     handler: function(grid, rowIndex, colIndex) {  
  18.         var rec = grid.getStore().getAt(rowIndex);  
  19.         alert("保存 " + rec.get('name'));  
  20.     }                  
  21. }]  
Javascript代碼  收藏代碼
  1. {  
  2.     header: "描述", width: 100,  
  3.     xtype: 'templatecolumn',//Ext.grid.column.Template數字列  
  4.     tpl : '{name}<tpl if="leader == false">不</tpl>是組長'  
  5. }  
Javascript代碼  收藏代碼
  1. Ext.create('Ext.grid.RowNumberer',{text : '行號', width : 35})  


二、自定義渲染函數示例: 
Javascript代碼  收藏代碼
  1. //定義渲染函數,格式化性別顯示  
  2. function formatSex(value){        
  3.     return value=='man'?'男':'<font color=red>女</font>';  
  4. }  
  5. //定義渲染函數,格式化年齡顯示  
  6. function formatAge(value,metadata){           
  7.     if(value < 30){  //年齡小於30的設置背景顏色爲#CCFFFF  
  8.         metadata.style = 'background-color:#CCFFFF;';  
  9.     }  
  10.     return value;  
  11. }  
Javascript代碼  收藏代碼
  1. columns: [//配置表格列  
  2.                 {header: "id", width: 30, dataIndex: 'id'},  
  3.                 {header: "姓名", width: 50, dataIndex: 'name'},  
  4.                 {header: "性別", width: 50, dataIndex: 'sex',renderer:formatSex},  
  5.                 {header: "生日", width: 100, dataIndex: 'birthday',   
  6.                     //格式化生日顯示  
  7. renderer:Ext.util.Format.dateRenderer('Y年m月d日')             },  
  8.                 {header: "年齡", width: 50, dataIndex: 'age',renderer:formatAge}  
  9.             ]  


三、Ext.selection.CellModel(單元格選擇模式)示例 
Javascript代碼  收藏代碼
  1. //創建表格數據  
  2. var datas = [  
  3.     [100,'張三',24], [200,'李四',30], [300,'王五',29]  
  4. ];  
  5. //創建Grid表格組件  
  6. var grid = Ext.create('Ext.grid.Panel',{  
  7.     title : '單元格選擇模式示例',  
  8.     renderTo: Ext.getBody(),  
  9.     width:200,  
  10.     height:170,  
  11.     frame:true,  
  12.     selType: 'cellmodel',//設置爲單元格選擇模式Ext.selection.CellModel  
  13.     tbar : [{  
  14.         text : '取得所選單元格',  
  15.         handler : function(){  
  16.             var cell = grid.getSelectionModel().getCurrentPosition();  
  17.             alert(Ext.JSON.encode(cell));  
  18.         }  
  19.     }],  
  20.     store: {//配置數據源  
  21.         fields: ['id','name','age'],//定義字段  
  22.         proxy: {  
  23.             type: 'memory',//Ext.data.proxy.Memory內存代理  
  24.             data : datas,//讀取內嵌數據  
  25.             reader : 'array'//Ext.data.reader.Array解析器  
  26.         },  
  27.         autoLoad: true//自動加載  
  28.     },  
  29.     columns: [//配置表格列  
  30.         {header: "id", width: 30, dataIndex: 'id', sortable: true},  
  31.         {header: "姓名", width: 80, dataIndex: 'name', sortable: true},  
  32.         {header: "年齡", width: 80, dataIndex: 'age', sortable: true}  
  33.     ]  
  34. });  


四、Ext.selection.RowModel(行選擇模式)示例--主要相關代碼 
Javascript代碼  收藏代碼
  1. simpleSelect : true,//啓用簡單選擇功能   
  2. multiSelect : true,//支持多選  
  3. selType: 'rowmodel',//設置爲單元格選擇模式Ext.selection.RowModel  
  4. tbar : [{  
  5.     text : '取得所選行',  
  6.     handler : function(){  
  7.         var msg = '';  
  8.         var rows = grid.getSelectionModel().getSelection();  
  9.         for(var i = 0; i < rows.length; i++){  
  10.             msg = msg + rows[i].get('name') + '\n';  
  11.         }  
  12.         alert(msg);  
  13.     }  
  14. }]  


五、Ext.selection.CheckboxModel(複選框選擇模式)示例--關鍵代碼 
Javascript代碼  收藏代碼
  1. //註冊複選框選擇模式別名爲selection.checkboxmodel  
  2. Ext.ClassManager.setAlias('Ext.selection.CheckboxModel','selection.checkboxmodel');  
Javascript代碼  收藏代碼
  1. multiSelect : true,//支持多選  
  2.             selModel: {  
  3.                 selType : 'checkboxmodel'//複選框選擇模式Ext.selection.CheckboxModel  
  4.             },  
  5.             tbar : [{  
  6.                 text : '取得所選行',  
  7.                 handler : function(){  
  8.                     var msg = '';  
  9.                     var rows = grid.getSelectionModel().getSelection();  
  10.                     for(var i = 0; i < rows.length; i++){  
  11.                         msg = msg + rows[i].get('name') + '\n';  
  12.                     }  
  13.                     alert(msg);  
  14.                 }  
  15.             }]  


六、grid中的features使用 
Ext.grid.feature.RowBody示例--關鍵代碼
Javascript代碼  收藏代碼
  1. features: [Ext.create('Ext.grid.feature.RowBody',{  
  2.     getAdditionalData: function(data, idx, record, orig) {  
  3.         var headerCt = this.view.headerCt,  
  4.             colspan  = headerCt.getColumnCount();//獲取表格的列數  
  5.       
  6.         return {  
  7.             rowBody: record.get('introduce'),//指定行體內容  
  8.             rowBodyCls: 'rowbodyColor',//指定行體樣式  
  9.             rowBodyColspan: colspan//指定行體跨列的列數  
  10.             };  
  11.         }  
  12.     })]  

Ext.grid.feature.Summary示例--關鍵代碼
Javascript代碼  收藏代碼
  1. features: [{  
  2.     ftype: 'summary'//Ext.grid.feature.Summary表格彙總特性  
  3. }],  
  4. columns: [//配置表格列  
  5.     {header: "姓名", flex: 1, dataIndex: 'name',   
  6.         summaryType: 'count',//求數量  
  7.         summaryRenderer: function(value){  
  8.             return '員工總數:'+value  
  9.         }  
  10.     },  
  11.     {header: "薪資", flex: 1, dataIndex: 'salary',   
  12.         summaryType: 'average',//求平均值  
  13.         summaryRenderer: function(value){  
  14.             return '平均薪資:'+value  
  15.         }  
  16.     }  
  17. ]  
Ext.grid.feature.Grouping示例--關鍵代碼
Javascript代碼  收藏代碼
  1. features: [Ext.create('Ext.grid.feature.Grouping', {  
  2.     groupByText : '用本字段分組',  
  3.     showGroupsText : '顯示分組',  
  4.     groupHeaderTpl: '性別: {name} ({rows.length})'//分組標題模版  
  5.     startCollapsed: true //設置初始分組是否收起   
  6. })],  
  7. columns: [//配置表格列  
  8.     {header: "姓名", flex: 1, dataIndex: 'name'},  
  9.     {header: "性別", flex: 1, dataIndex: 'sex'},  
  10.     {header: "年齡", flex: 1, dataIndex: 'age'}  
  11. ]  
Ext.grid.feature.GroupingSummary示例--關鍵代碼
Javascript代碼  收藏代碼
  1. features: [Ext.create('Ext.grid.feature.GroupingSummary', {  
  2.     groupByText : '用本字段分組',  
  3.     showGroupsText : '顯示分組',  
  4.     groupHeaderTpl: '性別: {name} ({rows.length})'//分組標題模版  
  5.     startCollapsed: true //設置初始分組是否收起   
  6. })],  
  7. columns: [//配置表格列  
  8.     {header: "姓名", width: 100, dataIndex: 'name',   
  9.         summaryType: 'count',//求數量  
  10.         summaryRenderer: function(value){  
  11.             return '員工總數:'+value  
  12.         }  
  13.     },  
  14.     {header: "性別", width: 50, dataIndex: 'sex'},  
  15.     {header: "年齡", width: 110, dataIndex: 'age',   
  16.         summaryType: 'average',//求數量  
  17.         summaryRenderer: function(value){  
  18.             return '平均年齡:'+value  
  19.         }  
  20.     }  
  21. ]  


七、Ext.grid.plugin.CellEditing示例--關鍵代碼 
Javascript代碼  收藏代碼
  1. plugins: [  
  2.     Ext.create('Ext.grid.plugin.CellEditing', {  
  3.         clicksToEdit: 1//設置鼠標單擊1次進入編輯狀態  
  4.     })  
  5. ],  
  6. selType: 'cellmodel',//設置爲單元格選擇模式  
  7. columns: [//配置表格列  
  8.   Ext.create('Ext.grid.RowNumberer',{text : '行號', width : 35}),  
  9.   {header: "姓名", width: 50, dataIndex: 'name',  
  10.         editor: {//文本字段  
  11.             xtype:'textfield',  
  12.             allowBlank:false  
  13.         }  
  14.     },  
  15.     {header: "生日", width: 100, dataIndex: 'birthday',   
  16.         xtype : 'datecolumn',//Ext.grid.column.Date日期列  
  17.         format : 'Y年m月d日',//日期格式化字符串  
  18.         editor: {//日期字段  
  19.             xtype:'datefield',  
  20.             allowBlank:false  
  21.         }  
  22.     },  
  23.     {header: "薪資", width: 50, dataIndex: 'salary',   
  24.         xtype: 'numbercolumn',//Ext.grid.column.Number數字列  
  25.         format:'0,000',//數字格式化字符串  
  26.         editor: {//數字字段  
  27.             xtype:'numberfield',  
  28.             allowBlank:false  
  29.         }  
  30.     }  
  31. ]  


八、Ext.grid.plugin.RowEditing示例--關鍵代碼 
Javascript代碼  收藏代碼
  1. plugins: [  
  2.     //行編輯模式  
  3.     Ext.create('Ext.grid.plugin.RowEditing', {  
  4.         clicksToEdit: 1  
  5.     })  
  6. ],  
  7. selType: 'rowmodel',//設置爲單元格選擇模式  
  8. columns: [//配置表格列  
  9.     Ext.create('Ext.grid.RowNumberer',{text : '行號', width : 35}),  
  10.     {header: "姓名", width: 50, dataIndex: 'name',  
  11.         editor: {//文本字段  
  12.             xtype:'textfield',  
  13.             allowBlank:false  
  14.         }  
  15.     },  
  16.     {header: "生日", width: 100, dataIndex: 'birthday',   
  17.         xtype : 'datecolumn',//Ext.grid.column.Date日期列  
  18.         format : 'Y年m月d日',//日期格式化字符串  
  19.         editor: {//日期字段  
  20.             xtype:'datefield',  
  21.             allowBlank:false  
  22.         }  
  23.     },  
  24.     {header: "薪資", width: 50, dataIndex: 'salary',   
  25.         xtype: 'numbercolumn',//Ext.grid.column.Number數字列  
  26.         format:'0,000',//數字格式化字符串  
  27.         editor: {//數字字段  
  28.             xtype:'numberfield',  
  29.             allowBlank:false  
  30.         }  
  31.     }  
  32. ]  


九、Ext.grid.plugin.DragDrop示例--關鍵代碼 
Javascript代碼  收藏代碼
  1. viewConfig: {  
  2.     plugins: [  
  3.         //行編輯模式  
  4.         Ext.create('Ext.grid.plugin.DragDrop',{  
  5.             dragGroup: 'grid1',//拖拽組名稱  
  6.             dropGroup: 'grid2'//拖放組名稱  
  7.         })  
  8.     ]  
  9. }  


十、Ext.grid.property.Grid示例 
Javascript代碼  收藏代碼
  1. //創建屬性表格  
  2. var grid = new Ext.grid.property.Grid({  
  3.     title: 'Ext.grid.property.Grid示例',  
  4.     width: 300,  
  5.     height: 200,  
  6.     renderTo: Ext.getBody(),  
  7.     //自定義屬性編輯器  
  8.     customEditors : {  
  9.         "性別" : new Ext.form.ComboBox({  
  10.             editable : false,  
  11.             displayField:'sex',  
  12.             mode: 'local',  
  13.             triggerAction: 'all',  
  14.             store:new Ext.data.SimpleStore({  
  15.                 fields: ['sex'],  
  16.                 data : [['男'],['女']]  
  17.             })  
  18.         }),  
  19.         "出生日期"new Ext.form.DateField({  
  20.             format : 'Y年m月d日',  
  21.             selectOnFocus:true,  
  22.             allowBlank : false  
  23.         })  
  24.     },  
  25.     //自定義渲染函數  
  26.     customRenderers: {  
  27.         //格式化布爾值顯示  
  28.         "是否已婚"function(v){  
  29.             return v?'是':'否';  
  30.         },  
  31.         //格式化日期顯示  
  32.         "出生日期": Ext.util.Format.dateRenderer('Y年m月d日')  
  33.     },  
  34.     source: {  
  35.         "員工名稱" : "張三",  
  36.         "出生日期" : Ext.Date.parse('10/15/2006''m/d/Y'),  
  37.         "性別" : '男',  
  38.         "是否已婚" : false,  
  39.         "年齡" : 29  
  40.     }  
  41. });  
發佈了8 篇原創文章 · 獲贊 18 · 訪問量 47萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章