1

How to populate BootStrap data into jqGrid layout in gsp page I need to show all static data on my gsp page using BootStrap class and then create webservice accordingly.

I'm using Grails 2.3.8, GGTS 3.5.1, jdk 1.7.

BootStrap.groovy file:

class BootStrap {

    def init = { servletContext ->
        // if we have an empty customer database,
        // create some test data
        if (Customer.count() == 0) {
            new Customer(
              firstName:'John', lastName:'Smith',
              age:27,
              emailAddress:'john@somewhere.com'
            ).save()
            // and so on
          }
        }

    def destroy = { }

}

Domain class:

class Customer {
    String firstName
    String lastName
    Integer age
    String emailAddress
}

Controller Class:

class CustomerController {

// return JSON list of customers
    def jq_customer_list = {
      def customers = Customer.list()
      def jsonCells = customers.collect {
            [cell: [it.firstName,
                    it.lastName,
                    it.age,
                    it.emailAddress
                ], id: it.id]
        }
        def jsonData= [rows: jsonCells]
        render jsonData as JSON
    }
}

gsp file:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
 <link rel="stylesheet" href="${resource(dir:'css',file:'main.css')}" />
        <link rel="stylesheet" href="${resource(dir:'css',file:'ui.jqgrid.css')}" />
        <link rel="stylesheet" href="${resource(dir:'css/ui-lightness',file:'jquery-ui-1.7.2.custom.css')}" />
        <g:javascript src="jquery-1.3.2.min.js"/>
        <g:javascript src="jquery-ui-1.7.2.custom.min.js"/>
        <g:javascript src="grid.locale-en.js"/>
        <g:javascript src="jquery.jqGrid.min.js"/>

<title>Customer list</title>
</head>
<body>
  <div class="body">
  <h1>Customer List</h1>

            <!-- table tag will hold our grid -->
            <table id="customer_list" class="scroll jqTable" cellpadding="0" cellspacing="0">

            </table>
            <!-- pager will hold our paginator -->
            <div id="customer_list_pager" class="scroll" style="text-align:center;"></div>

            <script type="text/javascript">
            /* when the page has finished loading.. execute the follow */
            $(document).ready(function () {
                jQuery("#customer_list").jqGrid({
                  url:'jq_customer_list',
                  datatype: "json",
                  colNames:['First Name','Last Name','Age','Email Address','id'],
                  colModel:[
                    {name:'firstName'},
                    {name:'lastName'},
                    {name:'age'},
                    {name:'email'},
                    {name:'id'}
                  ],
                  pager: jQuery('#customer_list_pager'),
                  viewrecords: true,
                  gridview: true
                });
            });
            </script>

  </div>
</body>
</html>
injecteer
  • 20,038
  • 4
  • 45
  • 89
  • `url:'jq_customer_list'` seems me suspected. Can you validate that the action will be really called. You can use additionally Developer Tools of Internet Explorer or Google chrome or use [Fiddler](http://www.telerik.com/fiddler) to trace full HTTP traffic. I recommend you to add add `loadError` callback (see [the answer](http://stackoverflow.com/a/6969114/315935)), `loadonce: true` option because you don't use server side paging, add `autoencode: true` option, replace `pager: jQuery('#customer_list_pager')` to `pager: '#customer_list_pager'` – Oleg Aug 22 '14 at 12:34
  • It is not working for me. Can you explain why we create this "url: ..." its really interesting if you explain – user3913652 Aug 25 '14 at 12:55
  • I'm not grail developer, I use ASP.NET (MVC mostly). "creating URL" seems to me wrong terminology, but in MVC for example the URL to MVC action will be build from base URL, Controller name and the Action name. The usage of just Action name as the URL seems me just suspected. So I suggested to use `loadError` and debug the server code to verify whether you really call the action and the JSON response came back and be correctly parsed. In HTTP traffic you will see which absolute URL will be used and compare it with real URL of your action. – Oleg Aug 25 '14 at 13:35

1 Answers1

0

I'm not an expert in jqgrid, but I can imagine the following solution to your problem:

$(document).ready(function () {
   jQuery("#customer_list").jqGrid({
     url:'${g.createLink( controller:'customer', action:'jq_customer_list' )}',
     ...
injecteer
  • 20,038
  • 4
  • 45
  • 89
  • Thanku...:) It's working. Can you please explain or can you provide links related to this url: creation and mapping. – user3913652 Aug 25 '14 at 12:56
  • Thanks again ...:) This all is for static data what I set in "BootStrap.groovy" But if I want to show my sql server database data into same jqgrid on same gsp then what I have to change. – user3913652 Aug 25 '14 at 14:00
  • nothing. if you have data in your db, it's will be shown on the page – injecteer Aug 25 '14 at 14:14
  • If I add new data into database table it is not reflected on gsp instead if I added in BootStrap file it is reflecting on my gsp. Is there any need to update controller or domain class? – user3913652 Aug 26 '14 at 07:02
  • I'm using SQL Server 2008 , GGTS3.5.1 , Grails 2.3.8, and default hibernate plugin ":hibernate:3.6.10.13" and cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3 inside DataSource.groovy file. – user3913652 Aug 26 '14 at 09:51
  • usually you need to update you gsp, as you insert more records to your db. Make sure, that they DO get inserted – injecteer Aug 26 '14 at 10:01
  • I'm inserting directly in db through sql server management studio and it get reflected in table. But when trying to retrieve that inserted records on gsp it is not reflecting. – user3913652 Aug 26 '14 at 13:24
  • check the caching, hit Ctrl+R in your browser – injecteer Aug 26 '14 at 13:26
  • Hey great explanations... Actually I am not using default Hibernate configuration instead I am using my customized database connection and settings. It all working now. – user3913652 Apr 05 '15 at 11:23