gridtest.php //中间一部分用ajax的代码没用,但是不影响
<HEAD>
<TITLE>Ext.grid.column.Column示例</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="ext-4.0/resources/css/ext-all.css" />
</HEAD>
<BODY STYLE="margin: 10px,30px">
<form name="form1" method="get" action="">
<tr>
<td height="103" align="center" background="Images/query.gif">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>关键字:<input name="content" type="text" id="content" size="20"> <a href="#"
onClick="checkName();">[查询]</a> </td>
</tr>
</table>
</td>
</tr>
</form>
<!-- 让 grid 自动适应上级组件的高度和宽度 -->
<style type="text/css">
.x-grid3-header-offset {width: auto;}
</style>
<!--media=print 这个属性说明可以在打印时有效-->
<!--希望打印时不显示的内容设置class="Noprint"样式-->
<!--希望人为设置分页的位置设置class="PageNext"样式-->
<style media="print">
<!--
.Noprint{display:none;}
.PageNext{page-break-after:always;}
-->
</style>
<!--IE内置打印控件IEWebBrowser-->
<OBJECT id=Web height=0 width=0 classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2
name=Web></OBJECT>
<div id='gridDiv'></div>
<script type="text/javascript" src="ext-4.0/bootstrap.js"></script>
<script type="text/javascript" src="ext-4.0/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="gridToExcel.js"></script>
<script type="text/javascript">
Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath('Ext.ux', 'ext-4.0/examples/ux');
Ext.require([
'Ext.grid.*',
'Ext.toolbar.Paging',
'Ext.util.*',
'Ext.data.*',
'Ext.ux.form.SearchField'
]);
function checkName() {
var content= form1.content.value;
if(content=="") {
window.alert("请填写查询条件!");
form1.content.focus();
return false;
}
else {
createRequest('page.php?content='+content+'&nocache='+new Date().getTime());
}
}
var http_request = false;
function createRequest(url) {
//初始化对象并发出XMLHttpRequest请求
http_request = false;
if (window.XMLHttpRequest) {
//Mozilla等其他浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType("text/xml");
}
} else if (window.ActiveXObject) {
//IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert("不能创建XMLHTTP实例!");
return false;
}
http_request.onreadystatechange = alertContents;
//指定响应方法
http_request.open("GET", url, true);
//发出HTTP请求
http_request.send(null);
}
function alertContents() {
//处理服务器返回的信息
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var jsondate=http_request.responseText;
alert(jsondate);
} else {
alert('您请求的页面发现错误');
}
}
}
Ext.onReady(
function(){
var itemsPerPage = 3;//指定分页大小
var store = Ext.create('Ext.data.Store', {
autoLoad: {start: 0, limit: itemsPerPage},
fields:['id', 'name', 'age'],
pageSize: itemsPerPage, //设置分页大小
proxy: {
method: 'GET',
type: 'ajax',
url: 'page.php', //请求的服务器地址
reader: {
type: 'json',
root: 'results',
totalProperty: 'totalProperty'
}
}
});
//创建Grid表格组件
var gridConacterInfo =Ext.create('Ext.grid.Panel',{
title : '查询数据演示',
renderTo: Ext.getBody(),
width:1200,
height:610,
frame:true,
store: store,
columns: [//配置表格列
{header: "id", width: 30, dataIndex: 'id', sortable: true},
{header: "name", width: 80, dataIndex: 'name', sortable: true},
{header: "age", width: 80, dataIndex: 'age', sortable: true}
],
bbar: [{
xtype: 'pagingtoolbar',
store: store, //这里需要指定与表格相同的store
displayInfo: true
}],
dockedItems: [{
dock: 'top',
xtype: 'toolbar',
items: {
width: 300,
fieldLabel: '搜索',
labelWidth: 50,
xtype: 'searchfield',
store: store
}
}] ,
tbar:
[
{
xtype:"button",
text:"打印",
pressed: true,
handler:function(){
printit();
}
},
{
xtype:"button",
text:"打印页面设置",
pressed: true,
handler:function(){
printsetup();
}
},
{
xtype:"button",
text:"打印预览",
pressed: true,
handler:function(){
printpreview();
}
},
{
xtype:"button",
text:"导出EXCEL",
pressed: true,
handler:function(){
downloadViewData(gridConacterInfo);
}
}
]
});
function downloadViewData(grid)
{
try {
var xls = new ActiveXObject ("Excel.Application");
}catch(e) {
alert( "要打印该表,您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,
//您的浏览器须允许执行控件。 请点击【帮助】了解浏览器设置方法!");
return "";
}
xls.visible =true; //设置excel为可见
var xlBook = xls.Workbooks.Add();
var xlSheet = xlBook.ActiveSheet;
// var cm = grid.getColumnModel();
// var colCount = grid.getColumnModel().getColumnCount();
var temp_obj = [];
var c=grid.columns;
//var d=grid.getStore().fields.items;
// alert(d);
//只下载没有隐藏的列(isHidden()为true表示隐藏,其他都为显示)
//临时数组,存放所有当前显示列的下标
// for(i=0;i <colCount;i++){
//
// if(cm.isHidden(i) == true){
//
// }else if(cm.getColumnId(i).indexOf("CM")!=-1){
// // Header Name 带 "CM" 的表示为 grid 的列
// temp_obj.push(i);
// }
// }
//
for(i=0;i <3;i++){
temp_obj.push(i);
}
//
//
// for(i=1;i <=temp_obj.length;i++){
//显示列的列标题
// alert("ceshi5");
// alert(
// xlSheet.Cells(1,i).Value = cm.getColumnHeader(temp_obj[i - 1]);
// }
var store = grid.getStore();
var t=store.getAt(1).get('id');
var recordCount = store.getCount();
var view = grid.getView();
// for(i=1;i <=recordCount;i++){
// for(j=1;j <=temp_obj.length;j++){
//EXCEL数据从第二行开始,故row = i + 1;
// alert("ceshi6");
// xlSheet.Cells(i + 1,j).Value = view.getCell(i - 1,temp_obj[j - 1]).innerText;
// }
// }
xlSheet.Cells(1,1).Value='id';
xlSheet.Cells(1,2).Value='name';
xlSheet.Cells(1,3).Value='age';
for(i=0;i <3;i++){
//EXCEL数据从第二行开始,故row = i + 1;
xlSheet.Cells(i + 2,1).Value =store.getAt(i).get('id');
xlSheet.Cells(i + 2,2).Value = store.getAt(i).get('name');
xlSheet.Cells(i + 2,3).Value = store.getAt(i).get('age');
}
//xlSheet.Cells(1,2).Value = view.getCell(2,2).innerText;
//xlSheet.Columns.AutoFit;
xls.ActiveWindow.Zoom = 120 ;
xls.UserControl = true; //很重要,不能省略,不然会出问题 意思是excel交由用户控制
//xlSheet.Cells(1,1)="<?php echo $a ?>";
xls=null;
xlBook=null;
xlSheet=null;
}
function printsetup(){
Web.execwb(8,1); // 打印页面设置
}
function printpreview(){
Web.execwb(7,1);// 打印页面预览
}
function printit() {
if (confirm('确定打印吗?')) {
Web.execwb(6,1);
}
}
});
</script>
</BODY>
page.php//有些代码也没有用,需要接受上面gridtest.php 传递的查询条件进行修改
<?php
$arrstore;
$conn=mssql_connect("localhost","sa","1") or die("Could not connect"); ;
mssql_select_db("rmis",$conn) or die("Could not select database");
//get count
$sql_count = "SELECT * FROM ceshiphp";
$content=$_GET[content];
$start = isset($_GET['start']) ? $_GET['start']+1:1;
$limit = isset($_GET['limit']) ? $_GET['limit']:3;
$end=$start+$limit-1;
if($content==""){
$sql_count = "SELECT * FROM ceshiphp ";
}else{
$sql_count = "SELECT * FROM ceshiphp where name='".$content."'";
}
if($content==""){
$sql = "select * from (
select top ".$limit." * from
(select top ".$end. " * from ceshiphp order by id) a order by id desc)b order by id ";
}else{
$sql = "select * from (
select top ".$limit." * from
(select top ".$end. " * from ceshiphp where name='".$content."' order by id) a order by id desc)b order by id ";
}
$rs_count = mssql_query($sql_count);
$rows = mssql_num_rows($rs_count);
$rs = mssql_query($sql);
$n=mssql_num_rows($rs);
for($i=0;$i<$n;$i++){
$j=0;
$obj = mssql_fetch_object($rs);
$arrstore[$i][$j]=$obj->id;
$arrstore[$i][$j+1]=$obj->name;
$arrstore[$i][$j+2]=$obj->age;
$arr[] = $obj;
}
//while($obj = mssql_fetch_object($rs))
//{
// $arr[] = $obj;
//}
//get result
echo '{"totalProperty":"'.$rows.'","results":'.json_encode($arr).'}';
?>