實作Ext grid,data store以Server side JSON file方式程現,並加入grid event:selection mode 續,增加修改Title功能。
開發環境:
IDE:MyEclipse6.6GA
Web Server:Tomcat 6.0.20
Database:MySql 5.1.36
MVC:Struts2.1.8
FrameWork:Spring2.0,Hibernate3.2
Client UI:Ext2.3.0
一、建立grid頁面(grid5.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(){ var store = new Ext.data.Store({ url: 'html/grid0.jsp', reader: new Ext.data.JsonReader({root:'rows',id:'id'}, [ 'id', 'coverthumb', 'title', 'director', {name: 'released', type: 'date', dateFormat: 'Y-m-d'}, 'genre', 'tagline', {name: 'price', type: 'float'}, {name: 'available', type: 'bool'} ]) }); store.load(); //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%">'; } // add your data store here var grid = new Ext.grid.GridPanel({ //renderTo: document.body, renderTo:'example-grid', frame:true, title: 'Movie Database', height:200, width:550, stripeRows:true, store: store, //增加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: [ { 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); } } }); } } }, { 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(); } } ], //Defining a Grids column model and want to display column columns: [ {header: "Cover", dataIndex: 'coverthumb',renderer:cover_image}, //Combining two columns {header: "Title", dataIndex: 'title',renderer:title_tagline}, {header: "Director", dataIndex: 'director'}, //Client-side sorting {header: "Released", dataIndex: 'released',sortable:true, renderer: Ext.util.Format.dateRenderer('m/d/Y')}, {header: "Genre", dataIndex: 'genre'}, {header: "Price",dataIndex: 'price',id:'price'}, //setup hidden column {header: "Tagline", dataIndex: 'tagline',hidden:true} ] }); }); //--> </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>
二、Server side JSON file(grid0.jsp):
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*" pageEncoding="UTF-8"%> { success:true, rows:[ { "id":"1", "title":"Office Space", "director":"Mike Judge(喬治麥克)", "released":"1999-02-19", "genre":"1", "tagline":"Work Sucks", "coverthumb":"OfficeSpace", "price":"19.95", "active":"1" },{ "id":"3", "title":"Super Troopers", "director":"Jay Chandrasekhar", "released":"2002-02-15", "genre":"1", "tagline":"Altered State Police", "coverthumb":"SuperTroopers", "price":"14.95", "active":"1" } //...more rows of data removed for readability...// ] }
三、測試結果:
1.http://localhost:8080/ExtStudy/Basic/grid5.jsp
沒有留言:
張貼留言