jqGrid Help ( was: Re: [Catalyst] Nonsensical Problem with DBIx ResultSet )

Kyle Hall kyle.m.hall at gmail.com
Thu Jul 8 17:08:19 GMT 2010


Thanks for the help! It seems very obvious in retrospect. I guess I was just
expecting a to get warns instead of errors. It's working well now. Thanks
again.

Now on to the next problem:

I'm trying to load a jqGrid from json data made via a REST call. But my grid
is just a blank rectangle on the page. Here is my code:

<body>

<div id=3D"tabs">
    <ul>
        <li><a href=3D"#panel-users">Users</a></li>
        <li><a href=3D"#panel-clients">Clients</a></li>

    </ul>
    <div id=3D"panel-users">
        <table id=3D"list"></table>
        <div id=3D"pager"></div>
    </div>
    <div id=3D"panel-clients">

    </div>
</div>

<script type=3D"text/javascript">
    $(function() {
        $("#tabs").tabs();
    });
</script>

<script type=3D"text/javascript">
jQuery(document).ready(function(){
  jQuery("#list").jqGrid({
    url:'http://192.168.20.155:3000/admin/rest/users',
    dataType: 'json',
    mtype: 'GET',
    colNames:['Id','Username',
'Minutes','Status','Message','Notes','Troublemaker'],
    colModel :[
      {name:'id', index:'id', width:11},
      {name:'username', index:'username', width:90},
      {name:'minutes', index:'minutes', width:3, align:'right'},
      {name:'status', index:'status', width:80, align:'right'},
      {name:'message', index:'message', width:80, align:'right',
sortable:false},
      {name:'note', index:'note', width:150, sortable:false},
      {name:"troublemaker", index:'troublemaker', width:1}
    ],
    pager: '#pager',
    rowNum:10,
    rowList:[10,20,30],
    sortname: 'username',
    sortorder: 'desc',
    viewrecords: true,
    caption: 'My test grid',
    jsonReader : {
    root: "rows",
    page: "page",
    total: "total",
    records: "records",
    repeatitems: true,
    cell: "cell",
    id: "id",
        userdata: "userdata"
    }
  });
});
</script>


And here is the JSON data that the url returns:

{"page":0,"records":"3","total":1,"rows":[{"cell":["1","admin","30","enable=
d",null,null,"0"]},{"cell":["2","test1","30","enabled",null,null,"0"]},{"ce=
ll":["3","test2","30","enabled",null,null,"0"]}]}

 I know this isn't specifically a Catalyst question, but I thought someone
here may have experienced this before. I'm using Controller::REST and
Catalyst::TraitFor::Controller::jQuery::jqGrid. Any help would be greatly
appreciated.
Here is the REST controller for posterity:
package LibkiServer::Controller::Admin::REST;
use Moose;
use namespace::autoclean;

with 'Catalyst::TraitFor::Controller::jQuery::jqGrid';

BEGIN {extends 'Catalyst::Controller::REST'; }

=3Dhead1 NAME

LibkiServer::Controller::Admin::REST - Catalyst REST Controller

=3Dhead1 DESCRIPTION

Catalyst REST Controller for Administration.

=3Dhead1 METHODS

=3Dcut


=3Dhead2 index

=3Dcut

sub users : Local : ActionClass('REST') { }

sub users_GET {
    my ( $self, $c ) =3D @_;

    my $user_rs =3D $c->model('DB::User')->search( {}, { prefetch =3D> { se=
ssion
=3D> 'client' } } );
    $user_rs =3D $self->jqgrid_page( $c, $user_rs );

    my $row =3D 0;
    my @row_data;

    while ( my $user =3D $user_rs->next() ) {
        my $user_id =3D $user->id;

        my @cell;
        push( @cell, $user->id );
        push( @cell, $user->username );
        push( @cell, $user->minutes );
        push( @cell, $user->status );
        push( @cell, $user->message );
        push( @cell, $user->notes );
        push( @cell, $user->is_troublemaker );
        push( @cell, defined( $user->session ) ? $user->session->status :
undef );
        push( @cell, defined( $user->session ) ? $user->session->client->id
: undef );
        push( @cell, defined( $user->session ) ?
$user->session->client->clientname : undef );
        my $single_row =3D {
            cell =3D> \@cell
        };
        push( @row_data, $single_row );

    }

    $self->status_ok(
        $c,
        entity =3D> {
            page =3D> $c->stash->{json_data}->{page},
            total =3D> $c->stash->{json_data}->{total},
            records =3D> $c->stash->{json_data}->{records},
            rows =3D> \@row_data
        }
    );
}


http://www.kylehall.info
Mill Run Technology Solutions ( http://millruntech.com )
Crawford County Federated Library System ( http://www.ccfls.org )
Meadville Public Library ( http://www.meadvillelibrary.org )



On Thu, Jul 8, 2010 at 12:02 PM, Jeff Albert <jralbert at uvic.ca> wrote:

>  Some of your users have sessions, and thus will have a row or rows in the
> related =91session=92 resultset =96 but some don=92t. When you attempt to=
 access
> $user->session->client->clientname, you=92re assuming that $user->session=
 is
> defined =96 but in the case of a user row with no related session row,
> $user->session won=92t be defined, and you=92ll raise the error you descr=
ibed
> when you attempt to access the methods of that resultset. Do a quick
> defined() test on $user->session before you try to use its methods, and
> you=92ll be good to go. Hope that helps!
>
>
>
> Cheers,
>
> Jeff Albert
>
>
>
> *From:* Kyle Hall [mailto:kyle.m.hall at gmail.com]
> *Sent:* Thursday, July 08, 2010 7:57 AM
> *To:* catalyst at lists.scsys.co.uk
> *Subject:* [Catalyst] Nonsensical Problem with DBIx ResultSet
>
>
>
> Hello all,
>   I'm the developer of a FOSS kiosk management system, Libki. I'm in the
> early stages of a complete rewrite using Catalyst for the web-based
> administration interface. This is my first time using Catalyst, but I'm v=
ery
> excited by the possibilities! I'm having a very strange issue with my
> resultsets. For example, I have three tables, a users table, a clients
> table, and a sessions table that connects the user to a client.
>
> I grab my users from the database, with any session/client data if the us=
er
> is currently logged in to a client kiosk. I am prefetching the session and
> client table data.
>
> my $user_rs =3D $c->model('DB::User')->search( {}, { prefetch =3D> { sess=
ion =3D>
> 'client' } } );
>
> Now I want to loop through it.
>
>     my $row =3D 0;
>     my @row_data;
>
>     while ( my $user =3D $user_rs->next() ) {
>         my $user_id =3D $user->id;
>
>         warn $user->session->client->clientname;
>
>         my $single_row =3D {
>             cell =3D> [
>                 $user->id,
>                 $user->username,
>                 $user->minutes,
>                 $user->status,
>                 $user->is_troublemaker,
>                 $user->session->client->clientname;
>             ]
>         };
>         push( @row_data, $single_row );
>     }
>
>
> Now, if I try to access any of the other tables, I get an error:
> [error] Caught exception in LibkiServer::Controller::Admin::REST->users
> "Can't call method "client" on an undefined value at
> /home/libki/LibkiServer/script/../lib/LibkiServer/Controller/Admin/REST.pm
> line 40."
>
> I don't know why I get this error, but the really crazy part is *the warn
> still works*:
> Client Name: testclient1 at
> /home/libki/LibkiServer/script/../lib/LibkiServer/Controller/Admin/REST.pm
> line 40.
>
> If anyone can help me out, I'd be eternally grateful.
>
> Thanks,
> Kyle
>
> http://www.kylehall.info
> Mill Run Technology Solutions ( http://millruntech.com )
> Crawford County Federated Library System ( http://www.ccfls.org )
> Meadville Public Library ( http://www.meadvillelibrary.org )
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20100708/9122a=
ff5/attachment.htm


More information about the Catalyst mailing list