JSON网格(JSON Grid)


前一节跟大家介绍了 XML网格(XML Grid)以xml格式的绑定,在这个AJAX流行年代,xml绑定已经满足不了我们的需求。ExtJs又为我们提供了JSON格式数据绑定的方法,下面我们来看看效果

演示(demo)地址在文章最后.

效果图如下:

用到的有三个文件survey.html、xml-grid.html和xml-grid.js。如果没有特别声明,.html跟.js文件名是一样的。

survey.html

[{"appeId":"1","survId":"1","location":"","surveyDate":"2008-03-14","surveyTime":"12:19:47","inputUserId":"1","inputTime":"2008-03-14 12:21:51","modifyTime":"0000-00-00 00:00:00"},{"appeId":"2","survId":"32","location":"","surveyDate":"2008-03-14","surveyTime":"22:43:09","inputUserId":"32","inputTime":"2008-03-14 22:43:37","modifyTime":"0000-00-00 00:00:00"},{"appeId":"3","survId":"32","location":"","surveyDate":"2008-03-15","surveyTime":"07:59:33","inputUserId":"32","inputTime":"2008-03-15 08:00:44","modifyTime":"0000-00-00 00:00:00"},{"appeId":"4","survId":"1","location":"","surveyDate":"2008-03-15","surveyTime":"10:45:42","inputUserId":"1","inputTime":"2008-03-15 10:46:04","modifyTime":"0000-00-00 00:00:00"},{"appeId":"5","survId":"32","location":"","surveyDate":"2008-03-16","surveyTime":"08:04:49","inputUserId":"32","inputTime":"2008-03-16 08:05:26","modifyTime":"0000-00-00 00:00:00"},{"appeId":"6","survId":"32","location":"","surveyDate":"2008-03-20","surveyTime":"20:19:01","inputUserId":"32","inputTime":"2008-03-20 20:19:32","modifyTime":"0000-00-00 00:00:00"}]

json-grid.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>XML Grid Example</title>
<link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />

    <!-- GC -->
 	<!-- LIBS -->
 	<script type="text/javascript" src="../../adapter/ext/ext-base.js"></script>
 	<!-- ENDLIBS -->

    <script type="text/javascript" src="../../ext-all.js"></script>
    
<script type="text/javascript" src="json-grid.js"></script>
<link rel="stylesheet" type="text/css" href="grid-examples.css" />

<!-- Common Styles for the examples -->
<link rel="stylesheet" type="text/css" href="../examples.css" />
</head>
<body>
<!-- EXAMPLES -->
<script type="text/javascript" src="../examples.js"></script>
<h1>XML Grid Example</h1>
<p>This example shows how to load a grid with XML data.</p>

<p>This grid also uses autoHeight and autoWidth to dynamically 
size to fit it's data and columns.</p>

<p>Note that the js is not minified so it is readable. 
See <a href="xml-grid.js">xml-grid.js</a>.</p>

<p>The data in the grid is loaded from <a href="sheldon.xml">sheldon.xml</a>
, which is directly from an Amazon.com search.</p>

<!-- a place holder for the grid. 
requires the unique id to be passed in the javascript function,
 and width and height ! -->
<div id="example-grid"></div>

</body>
</html>

json-grid.js

/*
 * Ext JS Library 2.0.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * 
 * 
 */

Ext.onReady(function(){

	var proxy=new Ext.data.HttpProxy(    {url:'survey.html'});
	//定义reader
		  var reader=new Ext.data.JsonReader(
			{
			},[
				{name: 'appeId', mapping: 'appeId'},
				{name: 'survId'},
				{name: 'location'}, 
				{name: 'surveyDate'},
				{name: 'surveyTime'},
				{name: 'inputUserId'}           
			]
		)
	//构建Store   
		var store=new Ext.data.Store(    {
		  proxy:proxy,
		  reader:reader
	   });
	//载入
	store.load();


    // create the grid
    var grid = new Ext.grid.GridPanel({
        store: store,
        columns: [
            {header: "appeId", width: 60, dataIndex: 'appeId', sortable: true},
            {header: "survId", width: 60, dataIndex: 'survId', sortable: true},
            {header: "location", width: 60, dataIndex: 'location', sortable: true},
            {header: "surveyDate", width: 100, dataIndex: 'surveyDate', sortable: true},
            {header: "surveyTime", width: 100, dataIndex: 'surveyTime', sortable: true},
            {header: "inputUserId", width:80, dataIndex: 'inputUserId', sortable: true}
        ],
        renderTo:'example-grid',
        width:540,
        height:200
    });

});

演示地址:http://extjs.org.cn/extjs/examples/grid/json-grid.html

评论

好多错误

太多错误了。。。你写的这几篇文章里,错误一堆。。。
另外,这些文件全部在示例里面有,你贴这些文件出来,倒不如给大家讲讲他们是如何协同工作的?

我用了,一点错误都没

我用了,一点错误都没有,你说的错误,是你自己盲目的COPY人家的代码,那些引用文件的JS路径,当然要自己修改一下

非常感谢你的建议

是的,我写的文章中全部文章都来自ext2.02中的例子,基本上一个字都不改.

分析这些例子会在后续的文章中写,

现在只是把例子放上来,充实一下网站的内容!

显示不出来数据 为什

显示不出来数据
为什么呢?
路径没有错啊

确实显示不出数据来

确实显示不出数据来,几个同事都试过了。绝对不是某人说的路径问题。

如果你没有把这些东西部署到tomcat或者其他服务器里面怎么可能会有数据?

如果你没有把这些东西部署到tomcat或者其他服务器里面怎么可能会有数据?