在学习Ext-2.2时,我们通常会先研究它自带的一些例子,今天发现一个关于paging的例子,它是运用php获取json格式数据的,
而我发现网上很多人都是通过java获取后台数据,所以我写了一个jsp获取json格式数据的例子,可供像我一样的初学者参考:
这里共涉及到几个关键的文件:paging.js 和paging.html(ext-2.2\examples\grid目录下) ,
paging.html 的代码不用变,只需要小改一下paging.js就可以了,
就是把 url: 'http://extjs.com/forum/topics-browse-remote.php' 这一行改成
url: 'paging_json.jsp' :
paging.js
- /*
- * Ext JS Library 2.2.1
- * Copyright(c) 2006-2009, Ext JS, LLC.
- * licensing@extjs.com
- *
- * http://extjs.com/license
- */
- Ext.onReady(function(){
- // create the Data Store
- var store = new Ext.data.JsonStore({
- root: 'topics',
- totalProperty: 'totalCount',
- idProperty: 'threadid',
- remoteSort: true,
- fields: [
- 'title', 'forumtitle', 'forumid', 'author',
- {name: 'replycount', type: 'int'},
- {name: 'lastpost', mapping: 'lastpost', type: 'date', dateFormat: 'timestamp'},
- 'lastposter', 'excerpt'
- ],
- // load using script tags for cross domain, if the data in on the same domain as
- // this page, an HttpProxy would be better
- proxy: new Ext.data.ScriptTagProxy({
- //url: 'http://extjs.com/forum/topics-browse-remote.php'
- url: 'paging_json.jsp'
- })
- });
- store.setDefaultSort('lastpost', 'desc');
- // pluggable renders
- function renderTopic(value, p, record){
- return String.format(
- '<b><a href="http://extjs.com/forum/showthread.php?t={2}" target="_blank">{0}</a></b><a href="http://extjs.com/forum/forumdisplay.php?f={3}" target="_blank">{1} Forum</a>',
- value, record.data.forumtitle, record.id, record.data.forumid);
- }
- function renderLast(value, p, r){
- return String.format('{0}<br/>by {1}', value.dateFormat('M j, Y, g:i a'), r.data['lastposter']);
- }
- var pagingBar = new Ext.PagingToolbar({
- pageSize: 25,
- store: store,
- displayInfo: true,
- displayMsg: 'Displaying topics {0} - {1} of {2}',
- emptyMsg: "No topics to display",
- items:[
- '-', {
- pressed: true,
- enableToggle:true,
- text: 'Show Preview',
- cls: 'x-btn-text-icon details',
- toggleHandler: function(btn, pressed){
- var view = grid.getView();
- view.showPreview = pressed;
- view.refresh();
- }
- }]
- });
- var grid = new Ext.grid.GridPanel({
- el:'topic-grid',
- width:700,
- height:500,
- title:'ExtJS.com - Browse Forums',
- store: store,
- trackMouseOver:false,
- disableSelection:true,
- loadMask: true,
- // grid columns
- columns:[{
- id: 'topic', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
- header: "Topic",
- dataIndex: 'title',
- width: 420,
- renderer: renderTopic,
- sortable: true
- },{
- header: "Author",
- dataIndex: 'author',
- width: 100,
- hidden: true,
- sortable: true
- },{
- header: "Replies",
- dataIndex: 'replycount',
- width: 70,
- align: 'right',
- sortable: true
- },{
- id: 'last',
- header: "Last Post",
- dataIndex: 'lastpost',
- width: 150,
- renderer: renderLast,
- sortable: true
- }],
- // customize view config
- viewConfig: {
- forceFit:true,
- enableRowBody:true,
- showPreview:true,
- getRowClass : function(record, rowIndex, p, store){
- if(this.showPreview){
- p.body = '<p>'+record.data.excerpt+'</p>';
- return 'x-grid3-row-expanded';
- }
- return 'x-grid3-row-collapsed';
- }
- },
- // paging bar on the bottom
- bbar: pagingBar
- });
- // render it
- grid.render();
- // trigger the data store load
- store.load({params:{start:0, limit:25}});
- });
- /**
- * @class Ext.ux.SliderTip
- * @extends Ext.Tip
- * Simple plugin for using an Ext.Tip with a slider to show the slider value
- */
- Ext.ux.SliderTip = Ext.extend(Ext.Tip, {
- minWidth: 10,
- offsets : [0, -10],
- init : function(slider){
- slider.on('dragstart', this.onSlide, this);
- slider.on('drag', this.onSlide, this);
- slider.on('dragend', this.hide, this);
- slider.on('destroy', this.destroy, this);
- },
- onSlide : function(slider){
- this.show();
- this.body.update(this.getText(slider));
- this.doAutoWidth();
- this.el.alignTo(slider.thumb, 'b-t?', this.offsets);
- },
- getText : function(slider){
- return slider.getValue();
- }
- });
- /*
- * Ext JS Library 2.2.1
- * Copyright(c) 2006-2009, Ext JS, LLC.
- * licensing@extjs.com
- *
- * http://extjs.com/license
- */
- Ext.onReady(function(){
- // create the Data Store
- var store = new Ext.data.JsonStore({
- root: 'topics',
- totalProperty: 'totalCount',
- idProperty: 'threadid',
- remoteSort: true,
- fields: [
- 'title', 'forumtitle', 'forumid', 'author',
- {name: 'replycount', type: 'int'},
- {name: 'lastpost', mapping: 'lastpost', type: 'date', dateFormat: 'timestamp'},
- 'lastposter', 'excerpt'
- ],
- // load using script tags for cross domain, if the data in on the same domain as
- // this page, an HttpProxy would be better
- proxy: new Ext.data.ScriptTagProxy({
- //url: 'http://extjs.com/forum/topics-browse-remote.php'
- url: 'paging_json.jsp'
- })
- });
- store.setDefaultSort('lastpost', 'desc');
- // pluggable renders
- function renderTopic(value, p, record){
- return String.format(
- '<b><a href="http://extjs.com/forum/showthread.php?t={2}" target="_blank">{0}</a></b><a href="http://extjs.com/forum/forumdisplay.php?f={3}" target="_blank">{1} Forum</a>',
- value, record.data.forumtitle, record.id, record.data.forumid);
- }
- function renderLast(value, p, r){
- return String.format('{0}<br/>by {1}', value.dateFormat('M j, Y, g:i a'), r.data['lastposter']);
- }
- var pagingBar = new Ext.PagingToolbar({
- pageSize: 25,
- store: store,
- displayInfo: true,
- displayMsg: 'Displaying topics {0} - {1} of {2}',
- emptyMsg: "No topics to display",
- items:[
- '-', {
- pressed: true,
- enableToggle:true,
- text: 'Show Preview',
- cls: 'x-btn-text-icon details',
- toggleHandler: function(btn, pressed){
- var view = grid.getView();
- view.showPreview = pressed;
- view.refresh();
- }
- }]
- });
- var grid = new Ext.grid.GridPanel({
- el:'topic-grid',
- width:700,
- height:500,
- title:'ExtJS.com - Browse Forums',
- store: store,
- trackMouseOver:false,
- disableSelection:true,
- loadMask: true,
- // grid columns
- columns:[{
- id: 'topic', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
- header: "Topic",
- dataIndex: 'title',
- width: 420,
- renderer: renderTopic,
- sortable: true
- },{
- header: "Author",
- dataIndex: 'author',
- width: 100,
- hidden: true,
- sortable: true
- },{
- header: "Replies",
- dataIndex: 'replycount',
- width: 70,
- align: 'right',
- sortable: true
- },{
- id: 'last',
- header: "Last Post",
- dataIndex: 'lastpost',
- width: 150,
- renderer: renderLast,
- sortable: true
- }],
- // customize view config
- viewConfig: {
- forceFit:true,
- enableRowBody:true,
- showPreview:true,
- getRowClass : function(record, rowIndex, p, store){
- if(this.showPreview){
- p.body = '<p>'+record.data.excerpt+'</p>';
- return 'x-grid3-row-expanded';
- }
- return 'x-grid3-row-collapsed';
- }
- },
- // paging bar on the bottom
- bbar: pagingBar
- });
- // render it
- grid.render();
- // trigger the data store load
- store.load({params:{start:0, limit:25}});
- });
- /**
- * @class Ext.ux.SliderTip
- * @extends Ext.Tip
- * Simple plugin for using an Ext.Tip with a slider to show the slider value
- */
- Ext.ux.SliderTip = Ext.extend(Ext.Tip, {
- minWidth: 10,
- offsets : [0, -10],
- init : function(slider){
- slider.on('dragstart', this.onSlide, this);
- slider.on('drag', this.onSlide, this);
- slider.on('dragend', this.hide, this);
- slider.on('destroy', this.destroy, this);
- },
- onSlide : function(slider){
- this.show();
- this.body.update(this.getText(slider));
- this.doAutoWidth();
- this.el.alignTo(slider.thumb, 'b-t?', this.offsets);
- },
- getText : function(slider){
- return slider.getValue();
- }
- });
/* * Ext JS Library 2.2.1 * Copyright(c) 2006-2009, Ext JS, LLC. * licensing@extjs.com * * http://extjs.com/license */ Ext.onReady(function(){ // create the Data Store var store = new Ext.data.JsonStore({ root: 'topics', totalProperty: 'totalCount', idProperty: 'threadid', remoteSort: true, fields: [ 'title', 'forumtitle', 'forumid', 'author', {name: 'replycount', type: 'int'}, {name: 'lastpost', mapping: 'lastpost', type: 'date', dateFormat: 'timestamp'}, 'lastposter', 'excerpt' ], // load using script tags for cross domain, if the data in on the same domain as // this page, an HttpProxy would be better proxy: new Ext.data.ScriptTagProxy({ //url: 'http://extjs.com/forum/topics-browse-remote.php' url: 'paging_json.jsp' }) }); store.setDefaultSort('lastpost', 'desc'); // pluggable renders function renderTopic(value, p, record){ return String.format( '<b><a href="http://extjs.com/forum/showthread.php?t={2}" target="_blank">{0}</a></b><a href="http://extjs.com/forum/forumdisplay.php?f={3}" target="_blank">{1} Forum</a>', value, record.data.forumtitle, record.id, record.data.forumid); } function renderLast(value, p, r){ return String.format('{0}<br/>by {1}', value.dateFormat('M j, Y, g:i a'), r.data['lastposter']); } var pagingBar = new Ext.PagingToolbar({ pageSize: 25, store: store, displayInfo: true, displayMsg: 'Displaying topics {0} - {1} of {2}', emptyMsg: "No topics to display", items:[ '-', { pressed: true, enableToggle:true, text: 'Show Preview', cls: 'x-btn-text-icon details', toggleHandler: function(btn, pressed){ var view = grid.getView(); view.showPreview = pressed; view.refresh(); } }] }); var grid = new Ext.grid.GridPanel({ el:'topic-grid', width:700, height:500, title:'ExtJS.com - Browse Forums', store: store, trackMouseOver:false, disableSelection:true, loadMask: true, // grid columns columns:[{ id: 'topic', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 }) header: "Topic", dataIndex: 'title', width: 420, renderer: renderTopic, sortable: true },{ header: "Author", dataIndex: 'author', width: 100, hidden: true, sortable: true },{ header: "Replies", dataIndex: 'replycount', width: 70, align: 'right', sortable: true },{ id: 'last', header: "Last Post", dataIndex: 'lastpost', width: 150, renderer: renderLast, sortable: true }], // customize view config viewConfig: { forceFit:true, enableRowBody:true, showPreview:true, getRowClass : function(record, rowIndex, p, store){ if(this.showPreview){ p.body = '<p>'+record.data.excerpt+'</p>'; return 'x-grid3-row-expanded'; } return 'x-grid3-row-collapsed'; } }, // paging bar on the bottom bbar: pagingBar }); // render it grid.render(); // trigger the data store load store.load({params:{start:0, limit:25}}); }); /** * @class Ext.ux.SliderTip * @extends Ext.Tip * Simple plugin for using an Ext.Tip with a slider to show the slider value */ Ext.ux.SliderTip = Ext.extend(Ext.Tip, { minWidth: 10, offsets : [0, -10], init : function(slider){ slider.on('dragstart', this.onSlide, this); slider.on('drag', this.onSlide, this); slider.on('dragend', this.hide, this); slider.on('destroy', this.destroy, this); }, onSlide : function(slider){ this.show(); this.body.update(this.getText(slider)); this.doAutoWidth(); this.el.alignTo(slider.thumb, 'b-t?', this.offsets); }, getText : function(slider){ return slider.getValue(); } });
paging_json.jsp
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%
- String json = "{totalCount:2,topics:[{threadid:1,forumid:1,forumtitle:'Ext;Help',title:'How to add hrefs dynamically into accordion panel',author:'venu',lastposter:'venu',lastpost:1235010582,excerpt:'ccccccccccccccccccccc',replycount:3},{threadid:2,forumid:2,forumtitle:'Ext;Help',title:'How to add hrefs dynamically into accordion panel',author:'venu',lastposter:'venu',lastpost:1235010592,excerpt:'dddddddddddddddd.',replycount:3}]}";
- response.getWriter().write(json);
- System.out.print(json);
- %>