将ExtJS与Grails集成时无输出

时间:2010-10-15 06:39:58

标签: grails extjs

我正在将extJS与Grails集成。

下面是我的music.TuneController中的列表操作。

def list = {
    def tuneInstanceList = new ArrayList<Tune>()
    def tune= new Tune();
    tune.playerId = "ASDF";
    tune.playerPrice= "100";
    tuneInstanceList.add(tune);                     
    def listResult = [ total: tuneInstanceList.size(), items: tuneInstanceList]
    render listResult as JSON           
} 

我的代码在tune-grid.js

/*
 * Ext JS Library 2.0 Beta 1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 *
 * http://extjs.com/license
 */

Ext.onReady(function(){

    // create the Data Store
    var ds = new Ext.data.Store({
       autoLoad: true,
       proxy: new Ext.data.HttpProxy({
       url: 'http://localhost:8080/music'}),
       reader: new Ext.data.JsonReader({
        results: 'total',
        root:'items',
        id:'id'
       },
       [
               {name: 'playerId' },
               {name: 'playerPrice'}               
          ]
       )
    });

    var cm = new Ext.grid.ColumnModel([
        {header: "Player Id", width: 120, dataIndex: playerId },
        {header: "Player Price", width: 180, dataIndex: 'playerPrice'}
    ]);
    cm.defaultSortable = true;

    // create the grid
    var grid = new Ext.grid.GridPanel({
        ds: ds,
        cm: cm,
        renderTo:'grid-example',
        width:540,
        height:200
    });
});

的list.gsp:

<%@ page import="music.Tune"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="layout" content="test" />
        <g:set var="entityName" value="${message(code: 'song.label', default: 'Song')}" />
        <title><g:message code="default.list.label" args="[entityName]" /></title>
           <g:javascript library="tune-grid"/>
    </head>
    <body>

        <div class="body">
        <!--<g:javascript library="examples"/>-->
        <!-- EXAMPLES -->

         <div id="grid-example"></div>


        </div>

    </body>
</html>

当调用动作时,我得到以下作为输出。似乎控件根本没有到达list.gsp。

{"total":1,"items":[{"class":"music.Tune","playerId":"ASDF", playerPrice":"100","playerDep":null}]}

你能告诉我这里做错了吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

好的,我的代码已经开始了,但是有一些问题:

您需要一个动作来渲染GSP,另一个动作需要渲染JSON,例如

  def list = {

  }

  def listData = {
    def tuneInstanceList = new ArrayList<Tune>()
    def tune = new Tune();
    tune.playerId = "ASDF";
    tune.playerPrice = "100";
    tuneInstanceList.add(tune);
    def listResult = [total: tuneInstanceList.size(), items: tuneInstanceList]
    render listResult as JSON
  }

然后在你的网格设置中:

var ds = new Ext.data.Store({
        autoLoad: true,
        proxy: new Ext.data.HttpProxy({
            url: 'http://localhost:8080/music/tune/listData'}),
<snip>

您的JS导入中也存在拼写错误(您的文件名为tune-grid.js,但您的GSP正在寻找test-grid.js。

我还必须修复你的列设置(你需要将playerId放在引号中):

var cm = new Ext.grid.ColumnModel([
        {header: "Player Id", width: 120, dataIndex: 'playerId' },
        {header: "Player Price", width: 180, dataIndex: 'playerPrice'}
    ]);

最后,我必须将ExtJS文件和资源放在正确的位置并包含它们:

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>      
  <g:javascript src="adapter/ext/ext-base.js"/>
  <g:javascript src="ext-all.js"/>
  <g:javascript src="test-grid.js"/>
  <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" />
</head>

欢呼声