此範例是實作Ext Grid,且Grid內容以Grouping方式程現,其Grouping是在Client side完成。
一、Ext Grid頁面(editGrid3.jsp):
二、測試結果:
1.http://localhost:8080/ExtStudy/Basic/editGrid3.jsp
一、Ext Grid頁面(editGrid3.jsp):
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'combox.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css"> <link rel="stylesheet" type="text/css" href="css/onReady.css"> <script type="text/javascript" src="extjs/adapter/ext/ext-base.js"></script> <script type="text/javascript" src="extjs/ext-all.js"></script> <script type="text/javascript" src="extjs/ext-lang-zh_TW.js"></script> <script type="text/javascript"><!-- Ext.onReady(function(){ /* normal store var store = new Ext.data.Store({ url: 'SysMgrJson!getMovies.action', reader: new Ext.data.JsonReader({root:'results',totalProperty:'totals'}, [ 'id', 'coverthumb', 'title', 'director', {name: 'released', type: 'date', dateFormat: 'Y-m-d'}, 'genre', 'tagline', {name: 'price', type: 'float'}, {name: 'active'} ]) }); */ //Grouping store var store = new Ext.data.GroupingStore({ url: 'SysMgrJson!getMovies.action', sortInfo: { field: 'genre', direction: "ASC" }, groupField: 'genre', reader: new Ext.data.JsonReader({root:'results',totalProperty:'totals'}, [ 'id', 'coverthumb', 'title', 'director', {name: 'released', type: 'date', dateFormat: 'Y-m-d'}, 'genre', 'tagline', {name: 'price', type: 'float'}, {name: 'active'} ]) }); var genres = new Ext.data.Store({ data: [ [1,"Comedy"], [2,"Drama"], [3,"Action"], [4,"Mystery"] ], reader: new Ext.data.ArrayReader({id:'id'}, [ 'id', 'genre']) }); //Combining two columns function function title_tagline(val, x, store){ return '<b>'+val+'</b><br>'+store.data.tagline; } //Generating HTML and graphics function cover_image(val){ return '<img src="images/'+val+'.jpg" width="50%" height="50%">'; } //for edit field of grid var title_edit = new Ext.form.TextField(); var director_edit = new Ext.form.TextField(); var tagline_edit = new Ext.form.TextField({ maxLength: 45 }); var price_edit = new Ext.form.TextField(); var coverthumb_edit = new Ext.form.TextField(); var released_edit = new Ext.form.DateField(); var genre_edit = new Ext.form.ComboBox({ typeAhead: true, triggerAction: 'all', mode: 'local', store: genres, displayField:'genre', valueField: 'genre' }); //for add row to grid var ds_model = Ext.data.Record.create([ 'id', 'title', 'director', {name: 'released', type: 'date', dateFormat: 'Y-m-d'}, 'genre', 'tagline', 'coverthumb', {name: 'price', type: 'float'}, {name: 'active'} ]); // add your data store here var grid = new Ext.grid.EditorGridPanel({ //renderTo: document.body, renderTo:'example-grid', frame:true, title: 'Movie Database', height:300, width:550, stripeRows:true, store: store, //matching Grouping store view: new Ext.grid.GroupingView(), //增加grid event for Row Selection mode sm: new Ext.grid.RowSelectionModel({ //singleSelect: true, listeners: { rowselect: { fn: function(sm,index,record) { index++; Ext.Msg.alert('提示','您選擇了,'+record.data.title+',第 '+index+'筆資料'); } } } }), //Altering the grid at the click of a button tbar: [ //modify title column of grid { text: '修改 Title', handler: function(){ var sm = grid.getSelectionModel(); if (sm.hasSelection()){ var sel = sm.getSelected(); Ext.Msg.show({ title: '修改 Title', prompt: true, buttons: Ext.MessageBox.OKCANCEL, value: sel.data.title, fn: function(btn,text){ if (btn == 'ok'){ sel.set('title', text); } } }); }else{ Ext.Msg.alert('提示','請選擇欲修改Title之列'); } } }, //hidden price column of grid { text: 'Hide Price', handler: function(btn){ var cm = grid.getColumnModel(); var pi = cm.getIndexById('price'); //var pi=5; if (cm.isHidden(pi)){ cm.setHidden(pi,false); btn.setText('Hide Price'); }else{ cm.setHidden(pi,true); btn.setText('Show Price'); } btn.render(); } }, //add first row to grid { text: 'Add Movie(First)', icon: 'images/RowInsert.png', cls: 'x-btn-text-icon', handler: function() { grid.getStore().insert(0,new ds_model({ id:'', title:'New Movie', director:'', genre:'Comedy', tagline:'', active:'1' }) ); grid.startEditing(0,0); } }, //add last row to grid { text: 'Add Movie(Last)', icon: 'images/RowInsert.png', cls: 'x-btn-text-icon', handler: function() { grid.getStore().insert(grid.getStore().getCount(),new ds_model({ id:'', title:'New Movie', director:'', genre:'Comedy', tagline:'', active:'1' }) ); grid.startEditing(grid.getStore().getCount()-1,0); } }, //Deleting data from the server { text: 'Remove Movie', icon: 'images/RowDelete.png', cls: 'x-btn-text-icon', handler: function() { var sm = grid.getSelectionModel(); if (sm.hasSelection()){ //var sel = sm.getSelected(); var sels = sm.getSelections(); var len = sels.length; var ids = new Array(len); for(var i=0;i<len;i++){ ids[i] = sels[i].data.id; if(i==0){ var msgs = '刪除: '+sels[i].data.title; }else{ msgs = msgs +',\n'+sels[i].data.title; } }; Ext.Msg.show({ title: '刪除電影', buttons: Ext.MessageBox.YESNOCANCEL, msg: msgs, fn: function(btn){ if (btn == 'yes'){ var conn = new Ext.data.Connection(); conn.request({ url: 'SysMgrJson!updateMovies.action', method:'post', params: { action: 'delete', id: ids }, success: function(resp,opt) { //grid.getStore().remove(sel); grid.getStore().reload(); }, failure: function(resp,opt) { Ext.Msg.alert('Error', 'Unable to delete movie'); } }); } } }); }else{ Ext.Msg.alert('提示','請選擇欲刪除(多)列之資料'); } } }, //Saving data to the server { text: 'Save Movie', icon: 'images/save-icon.png', cls: 'x-btn-text-icon', handler: function() { var sm = grid.getSelectionModel(); if (sm.hasSelection()){ var sels = sm.getSelections(); var len = sels.length; var values = new Array(len); for(var i=0;i<len;i++){ values[i] = new Array(9); values[i][0] = sels[i].data.id; values[i][1] = sels[i].data.title; values[i][2] = sels[i].data.director; values[i][3] = (sels[i].data.released).format('Y-m-d'); values[i][4] = sels[i].data.genre; values[i][5] = sels[i].data.tagline; values[i][6] = sels[i].data.coverthumb; values[i][7] = sels[i].data.price; values[i][8] = sels[i].data.active; if(i==0){ var msgs = '儲存: '+sels[i].data.title; }else{ msgs = msgs +',\n'+sels[i].data.title; } }; Ext.Msg.show({ title: '儲存電影', buttons: Ext.MessageBox.YESNOCANCEL, msg: msgs, fn: function(btn){ if (btn == 'yes'){ var conn = new Ext.data.Connection(); conn.request({ url: 'SysMgrJson!updateMovies.action', method:'post', params: { action: 'update', values: values }, success: function(resp,opt) { //grid.getStore().remove(sel); grid.getStore().reload(); }, failure: function(resp,opt) { Ext.Msg.alert('Error', 'Unable to save movie'); } }); } } }); }else{ Ext.Msg.alert('提示','請選擇欲儲存(多)列之資料'); } } } ], bbar: new Ext.PagingToolbar({ pageSize: 5, store: store, displayInfo: true, displayMsg: '第 {0} 筆 - {1} 筆 of {2} 筆', emptyMsg: "沒有資料" }), //Defining a Grids column model and want to display column columns: [ {header: "Cover", dataIndex: 'coverthumb',renderer:cover_image}, {header: "CoverThumb", dataIndex: 'coverthumb',editor:coverthumb_edit}, //Combining two columns {header: "Title", dataIndex: 'title',renderer:title_tagline,editor:title_edit}, {header: "Director", dataIndex: 'director',editor:director_edit}, //Client-side sorting {header: "Released", dataIndex: 'released',sortable:true, renderer: Ext.util.Format.dateRenderer('m/d/Y'),editor:released_edit}, {header: "Genre", dataIndex: 'genre',editor:genre_edit}, {header: "Price",dataIndex: 'price',id:'price',editor:price_edit}, //setup hidden column {header: "Tagline", dataIndex: 'tagline',hidden:true,editor:tagline_edit} ] }); //需在gridPanel建立後才可以Load資料 store.load({params:{start:0, limit:5}}); }); // --></script> </head> <body><br> <center> <h3><font color="red">Movie Database Grid</font></h3><br> </center> <table align="center" border="0"> <tr> <td> <div id="example-grid"></div> <td> </tr> </table> </body> </html>
二、測試結果:
1.http://localhost:8080/ExtStudy/Basic/editGrid3.jsp
沒有留言:
張貼留言