tags:

views:

39

answers:

1

Hi,

I am in the process of integrating extJS with Grails.

Below is my list action in my 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           
} 

My code in tune-grid.js

/*
 * Ext JS Library 2.0 Beta 1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * [email protected]
 *
 * 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>

When the action is called , I get the below as the output. It seems that the control doesnt get to the list.gsp at all.

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

Could you tell me what I am doing wrong here?

Thanks!

+1  A: 

Ok, I got your code going but there's a few issues:

You need one action to render the GSP, another to render the JSON e.g.

  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
  }

and then in your grid setup:

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

there is also a typo in your JS import (your file was called tune-grid.js but your GSP is looking for test-grid.js.

I also had to fix your column setup (you need to put playerId in quotes):

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

Lastly, I had to get the ExtJS files and resources in the right place and included them:

<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>

cheers

Lee

leebutts
Corrected that....silly me....edited the qn too.......Bt still i am at the same spot...Thoughts?
Alex
Updated my answer...
leebutts
Amazin....This worked Lee....Thanks so much for the help
Alex
Another chnage that I am making here in my listData.
Alex
to the below code, so that i can query the database.def listData = { def session = sessionFactory.getCurrentSession() def result = session.createSQLQuery("select player_id from w.music where player_id=('530AS')").list(); def tuneInstanceList = new ArrayList<Tune>() result.each { def tune = new Tune() tune.playerId = it(0) tune.playerPrice = "100" tuneInstanceList.add(tune) } def listResult = [total: tunInstanceList.size(), items: tunInstanceList] render listResult as JSON; }
Alex
Here I get the grid display with the values 5(instead of 530AS) and 100 for playerId and playerPrice.If in my listData I put tune.playerId = it(1), then I get a display of 3(instead of 530AS) and 100 for playerId and playerPrice.Could you tell me what I am missing out here?Thanks
Alex
Only if you vote up my answer and give me lots of points :)
leebutts
Why are you using raw SQL and not Gorm? Tune.findByPlaerId("530AS") will return you a list of Tune objects that you can pass straight to the grid
leebutts
You should map the Tune class to your w.music table if you haven't already. Have a good read of the GORM chapter: http://www.grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20(GORM).html
leebutts
Hey Lee....vote up...will do...gotto earn 15 rep for that :)... n ya reg. the query....actually my app is using a customized query and therefore I am using the sql query instaed of GORM. I have just mentioned a sample sql here, the actual sql I have retuens a lot of rows as result and I am to display the data as a grid...Therefore using the above code....thoughts??
Alex
I think it(0) is returning the first character in the string. Try tune.playerId = it
leebutts
Thanks Lee. Perfectly what I wanted. Dont know why that dint strike me. Thanks a ton Lee.
Alex