0

I'm working with jqGrid and currently I'm having some issues, mainly because of the config for it.

Currently I want to do this:

    var tableConfigJson = $treeTable.getMappedJson();

    jQuery(function() {
        jQuery("#grid").jqGrid(tableConfigJson);
    });

$treeTable is my object on my server side that I am retrieving with velocity mapping.

Now the JSON that I'm getting from my server is fine, however when I pass it to jqGrid to setup the config for it, it doesn't work. If I set up the config in the javascript but I create the config as a javascript object it works fine, however if I just pass the JSON as the config it doesn't work. Now my question is, is there an easy way to get around this? Or am I going to need to map the config again using the JSON.

if I however do this:

jQuery(function() {


jQuery("#grid").jqGrid({
    treeGrid: true,
    treeGridModel: 'adjacecncy',
    ExpandColumn: 'name',
    datatype: "local",
    mtype: 'Get',
    colNames: ['id','Name','MenuId','Menu Name'],
    colModel: [
        {name:'RowId',index:'RowId',width:300,fixed:true},
        {name:'Name',index:'Name',width:300,fixed:true},
        {name:'MenuId',index:'MenuId',width:300,fixed:true},
        {name:'MenuName',index:'MenuName',width:300,fixed:true},
    ],
    root:[
        {id:"1",Name:"Main Menu", MenuId:"1",MenuName:"Menu1"},
        {id:"2",Name:"Main Menu1",MenuId:"2",MenuName:"Menu2"},
        {id:"3",Name:"Main Menu2",MenuId:"3",MenuName:"Menu3"}
    ],
    pager: '#dvtreegridsamp',
    Caption: 'example'
)};

Then it works.

UPDATE:

This is the output in the console for my object, but I think I know what the problem might be now.

Object {datatype: "local", data: Array[3], colNames: Array[2], colModel: Array[2], height: "auto"…}ExpandColumn: "id"
caption: "I am SAD"
colModel: Array[2]0: Object1: Objectlength: 2__proto__: Array[0]colNames: Array[2]
data: Array[3]
datatype: "local"
height: "auto"
sortname: "id"
treeGrid: "true"
treeGridModel: "adjacency"
treedatatype: "local"

The problem is that the parameters that are used for config, are parsed as array objects in places, and not JSON style strings. So these parameters are being ignored. I need these to be JSON style and then everything works.

If I need to clarify anything just let me know, its late and I've had lots of coffee.

5
  • That part works fine, velocity is cool like that :). Commented Oct 2, 2014 at 15:10
  • let me edit the code so that that isn't misleading Commented Oct 2, 2014 at 15:11
  • Well if you say so. What you haven't explained then is exactly how it "doesn't work". Are there errors reported? Have you tried some console.log() calls to see what's going on? Is tableConfigJson really an object or is it a string? Commented Oct 2, 2014 at 15:11
  • Basically what happens if I just pass tableConfigJson to my function is that it works but only sort of. It doesn't set all of the configs and I'm assuming its because they are double quoted unlike normal java objects which use single quotes. Commented Oct 2, 2014 at 15:23
  • Double versus single quotes make absolutely no difference. Both are parsed as strings, and it's impossible to tell after the fact whether a string constant was expressed with one kind of quote or the other. Commented Oct 2, 2014 at 15:25

1 Answer 1

2

If tableConfigJson is currently a JSON string, like you suggest, then you just need to parse it:

jQuery("#grid").jqGrid(JSON.parse(tableConfigJson));

If you need it to work in older browsers, you'll want to include a shim.

Sign up to request clarification or add additional context in comments.

5 Comments

This is definitely a possibility.
When I've tried parsing it to get a JS object, it throws me an error which occurs when you already have a JS object. The error is "Unexpected token o" from what I've read its when the JSON has already been parsed.
@Daniel that's correct, sounds like you already have an object. Add a console.log(tableConfigJson), which will log the whole object to the browser console – and include it in your question! It sounds like it will differ in some way from the literal object that you tried and succeeded with.
Also just to be safe, console.log(typeof tableConfigJson);
Im upvoting this and accepting it because looking in the console log helped me figure out what was wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.