Javascript does not accept variable value

Asked

Viewed 237 times

1

I am trying to make a function to call a GRID that will be different depending on the parameter I pass. This is the function:

$JQuery(document).ready(function() {
    $.ajax({
        url: "/main/conciliacao/gera-colunas/tabela/<?php echo $tabela ?>",
        success: function(data, st) {
            criaGrid(data);
        },
        error: function() {
            alert("Erro ao retornar os valores das colunas");
        }
    });
});

function criaGrid(colM) {
    $JQuery("#jqGrid").jqGrid({
        url: "/main/conciliacao/gera-grid/processo/<?php echo $processo ?>/tabela/<?php echo $tabela ?>/id/<?php echo $id ?>",
        datatype: "json",
        colModel: [ colM ],
        viewrecords: true, 
        width: 780,
        height: 200,
        rowNum: 30,
        pager: "#jqGridPager"
    });
}

My problem is that the Colm variable has what I need to create the grid columns, but if I call it in colModel: the function does not recognise the variable value.

What might be going on?

  • console.log(colM) returns what?

  • Returns { label: 'id', name: 'id', width: 75 }, which is what I need. If I copy what comes in the console.log() and paste it into the code works perfectly, but if it stays in the variable it doesn’t

  • 1

    Where exactly are you making this mistake? Inside criaGrid?

  • That, within the criaGrid in part colModel: [ colM ],

1 answer

2


If you look at plugin documentation, will see that colModel does not expect an array as you are passing, but rather an object. You can pass colM directly, without wrapping an array:

function criaGrid(colM) {
    $JQuery("#jqGrid").jqGrid({
        url: "/main/conciliacao/gera-grid/processo/<?php echo $processo ?>/tabela/<?php echo $tabela ?>/id/<?php echo $id ?>",
        datatype: "json",
        colModel: colM,
        viewrecords: true, 
        width: 780,
        height: 200,
        rowNum: 30,
        pager: "#jqGridPager"
    });
}

But that property table that you have in your colM is not one of the valid options for colModel, I’m not sure what your intention is with that figure.


As for your comment that by passing the variable it does not accept, it may be because your JSON is not being interpreted as such. To resolve this, inform in the ajax call that the return will be a JSON:

$.ajax({
    url: "/main/conciliacao/gera-colunas/tabela/<?php echo $tabela ?>",
    dataType: "json", // <--- aqui
    success: function(data, st) {
        criaGrid(data);
    },
    error: function() {
        alert("Erro ao retornar os valores das colunas");
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.