2013-02-16 76 views
0

我一直试图让一个简单的Ext.js应用程序工作,即从数据库获取其数据并将其显示在ext.js网格中,但唯一我总是得到是“失败:真实”。 也许你可以向我指出错误。如何从数据库中获取ext.js网格

这里是index.php文件:

<html> 
    <head> 
     <script src="extjs/ext-dev.js" type="text/javascript"></script> 
     <script src="mainscript.js" type="text/javascript"></script> 

    </head> 
<body> 
<?php 
$conn = mysql_connect('localhost','admin','1234','cdcol') or die ('Error connecting to mysql'); 

$task = ''; 
if (isset($_POST['task'])){ 
    $task = $_POST['task']; 
} 
switch($task){ 
    case "LISTING":    
     getList(); 
     break; 
    default: 
     echo "{failure:true}"; 
     break; 
} 

function getList(){ 
$query = mysql_query("SELECT * FROM cdcol.cds"); 
$cds = array(); 
while($row = mysql_fetch_array($query)){ 
$cds[] = array('titel' => $row['titel'], 'jahr' => $row['jahr'],'interpret' => $row['interpret'], 'id' => $row['id']); 
} 
$jsonresult = json_encode(array('cds'=>$cds)); 
} 
?> 
</body> 

,这将是mainscript.js:

var cdDataStore;   
var cdColumnModel;  
var cdEditorGrid; 
var cdWindow; 

Ext.onReady(function(){ 
Ext.QuickTips.init(); 

cdDataStore = new Ext.data.Store({ 
    id: 'cdDataStore', 
    proxy: new Ext.data.HttpProxy({ 
      url: 'index.php',  
      method: 'POST' 
     }), 
     baseParams:{task: "LISTING"}, 
    reader: new Ext.data.JsonReader({ 

    root: 'results', 
    totalProperty: 'total', 
    id: 'id' 
    },[ 
    {name: 'titel', type: 'string', mapping: 'titel'}, 
    {name: 'interpret', type: 'string', mapping: 'interpret'}, 
    {name: 'jahr', type: 'int', mapping: 'jahr'}, 
    {name: 'id', type: 'int', mapping: 'id'} 
    ]), 
    sortInfo:{field: 'jahr', direction: "ASC"} 
}); 

cdColumnModel = new Ext.grid.ColumnModel(
[{ 
    header: '#', 
    readOnly: true, 
    dataIndex: 'id', 
    width: 50, 
    hidden: false 
    },{ 
    header: 'titel', 
    dataIndex: 'titel', 
    width: 150, 

    },{ 
    header: 'interpret', 
    dataIndex: 'interpret', 
    width: 150, 

    },{ 
    header: 'jahr', 
    readOnly: true, 
    dataIndex: 'jahr', 
    width: 50,      
    },{ 
    header: 'id', 
    dataIndex: 'id', 
    width: 150, 
    readOnly: true 
    }] 
); 
cdColumnModel.defaultSortable= true; 

cdEditorGrid = new Ext.grid.EditorGridPanel({ 
    id: 'cdListingEditorGrid', 
    store:cdDataStore,  
    cm: cdColumnModel,  
    enableColLock:false, 
    clicksToEdit:1, 
    selModel: new Ext.grid.RowSelectionModel({singleSelect:false}) 
}); 

cdListingWindow = new Ext.Window({ 
    id: 'cdListingWindow', 
    title: 'some cds', 
    closable:true, 
    width:700, 
    height:350, 
    plain:true, 
    layout: 'fit', 
    items: cdListingEditorGrid 
}); 

cdDataStore.load();  
cdListingWindow.show(); 

}); 

任何帮助,或者一个不同的方法会被深深地感激!

回答

0

您使用哪个extjs版本?

你确定参数“任务”是发送? (例如使用Firefox和Firebug - Network - XHR - Post)

我认为proxy本身没有方法属性。

proxy: { 
    type: 'ajax', 
    url: 'index.php', 
    reader: { 
     type: 'json', 
     root: 'cds' 
    }, 
    extraParams: { 
     'task': "LISTING" 
    }, 
    actionMethods: { 
     read: 'POST' 
    } 
} 

你的php脚本必须返回一个json字符串!例如{'success': true, 'cds':[{"id":"27","titel":"xyz"

另外你的架构看起来很奇怪。为什么没有单独的PHP文件?为什么不使用columnmodel?为什么你声明任何东西都是var?

相关问题