Access the attribute inside my json

Access the attribute inside my json

klermannklermann Posts: 277Questions: 67Answers: 1
edited August 2017 in Free community support

Hello, how can I access the attribute inside my json. I tried in many ways, but not achieved so far ...


{
    "draw": 0,
    "recordsTotal": 8,
    "recordsFiltered": 7,
    "dataReceitas": null,
    "data": [{
        "id": 9,
        "descricaoReceita": "Receita 9999",
        "dataReceita": {
            "year": 2017,
            "month": "AUGUST",
            "dayOfMonth": 8,
            "dayOfWeek": "TUESDAY",
            "era": "CE",
            "dayOfYear": 220,
            "leapYear": false,
            "monthValue": 8,
            "chronology": {
                "id": "ISO",
                "calendarType": "iso8601"
            }
        },
        "valorReceita": 5000.00,
        "receitaFixa": true,
        "receitaFixaQuantidade": 1,
        "receitaFixaTempo": "DIAS",
        "repetirLancReceita": false,
        "repetirPorPeriodo": "MENSAL",
        "pagamento": false,
        "addObservacao": "dgdg",
        "**tipoReceitas**": [{
            "id": 5,
            "**nome**": "Tipo 5",
            "corTipoReceita": "#AFFJKL",
            "nomeTipoReceita": "Tipo 5"
        }],
        "minhasContas": [{
            "id": 1,
            "nome": "Contas 1",
            "dataAcesso": {
                "year": 2017,
                "month": "JANUARY",
                "dayOfMonth": 1,
                "dayOfWeek": "SUNDAY",
                "era": "CE",
                "dayOfYear": 1,
                "leapYear": false,
                "monthValue": 1,
                "chronology": {
                    "id": "ISO",
                    "calendarType": "iso8601"
                }
            },
            "saldoAnterior": 0.00,
            "saldoAtual": 5600.00,
            "saldoFinal": 2520.00,
            "tipo": "POUPANCA",
            "cor": "#5d4aas",
            "dashBoard": false
        }],
        "etiquetas": [{
            "id": 156,
            "nome": "fghfhfgh",
            "minhasReceitas": null
        }],
        "cor": "rgb(255, 24, 01, 0.8)"
    }, {
        "id": 225,
        "descricaoReceita": "gfgsd",
        "dataReceita": {
            "year": 2017,
            "month": "AUGUST",
            "dayOfMonth": 9,
            "dayOfWeek": "WEDNESDAY",
            "era": "CE",
            "dayOfYear": 221,
            "leapYear": false,
            "monthValue": 8,
            "chronology": {
                "id": "ISO",
                "calendarType": "iso8601"
            }
        },
        "valorReceita": 5000.00,
        "receitaFixa": true,
        "receitaFixaQuantidade": null,
        "receitaFixaTempo": null,
        "repetirLancReceita": false,
        "repetirPorPeriodo": null,
        "pagamento": false,
        "addObservacao": "werwe",
        "tipoReceitas": [{
            "id": 5,
            "nome": "Tipo 5",
            "corTipoReceita": "#AFFJKL",
            "nomeTipoReceita": "Tipo 5"
        }],
        "minhasContas": [{
            "id": 2,
            "nome": "Contas 2",
            "dataAcesso": {
                "year": 2017,
                "month": "FEBRUARY",
                "dayOfMonth": 1,
                "dayOfWeek": "WEDNESDAY",
                "era": "CE",
                "dayOfYear": 32,
                "leapYear": false,
                "monthValue": 2,
                "chronology": {
                    "id": "ISO",
                    "calendarType": "iso8601"
                }
            },
            "saldoAnterior": 0.00,
            "saldoAtual": 2600.00,
            "saldoFinal": 1500.00,
            "tipo": "POUPANCA",
            "cor": "#5d4aas",
            "dashBoard": false
        }],
        "etiquetas": [{
            "id": 157,
            "nome": "xcswfswe",
            "minhasReceitas": null
        }],
        "cor": null
    }]
}

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Which attribute specifically, and where in the code? I'm not certain what it is exactly you are trying to do I'm afraid.

    Allan

  • klermannklermann Posts: 277Questions: 67Answers: 1

    I was able to access, it was the "Etiquetas" attribute

  • klermannklermann Posts: 277Questions: 67Answers: 1
    edited August 2017

    How do I access an object inside another object by placing that object accessed inside a select type list in form editor. The item I speak of is marked in bold, => "MinhaContas" => "Nome"

  • klermannklermann Posts: 277Questions: 67Answers: 1

    How do I access an object inside another object by placing that object accessed inside a select type list in form editor. The item I speak of is marked in bold, => "MinhaContas" => "Nome"

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Use dot notation like you would in plain Javascript - see the columns.data documentation for full details. e.g. MinhaContas.Nome.

    Allan

  • klermannklermann Posts: 277Questions: 67Answers: 1

    Sorry for posts duplicated! My select is displayed without a list!

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Can you post a link to a test case showing the issue please?

    Thanks,
    Allan

  • klermannklermann Posts: 277Questions: 67Answers: 1
    edited August 2017
  • klermannklermann Posts: 277Questions: 67Answers: 1
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Your listTipoReceitas function is resolving to be an empty array - see attached image.

    That is because it is performing an async action:


    function listTipoReceitas(){ var listTR = new Array(); var getDataAjax = $.ajax({ url: "/financeiro/receitas/listAll", }).done(function(obj) { obj.data.forEach(function(data, i) { console.log(data.tipoReceitas[0].nome); console.log(i); var nomeTipoReceita = data.tipoReceitas[0].nome; listTR[i] = { "label": "" + data.tipoReceitas[0].nome + "", "value" : "" + i + ""}; }) //listTipoReceitas(listTR); }); console.log(listTR); return listTR; };

    You could just add async: false, but I would suggest you use the update() method of the select field type to update the options in its callback.

    Regards,
    Allan

  • klermannklermann Posts: 277Questions: 67Answers: 1

    How should i use update() method of the select field type to update the options in its callback. Would it be possible to get the data from this list in the object that populates the datatable to populate this select type list?

  • klermannklermann Posts: 277Questions: 67Answers: 1

    I'm lost, can you help me please!

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Simply:

    editor.field('myFieldName').update( listTR );
    

    inside your done function.

    Allan

  • klermannklermann Posts: 277Questions: 67Answers: 1

    I did as directed and the datatable disappeared, disappeared

  • klermannklermann Posts: 277Questions: 67Answers: 1

    I did as directed and the datatable disappeared, disappeared

    `function listTipoReceitas(){

                var listTR = new Array();
                var getDataAjax = $.ajax({
                      url: "/financeiro/receitas/listAll",
                    }).done(function(obj) {
    
                        obj.data.forEach(function(data, i) {
    
                            console.log(data.tipoReceitas[0].nome); 
                            console.log(i); 
    
                            var nomeTipoReceita = data.tipoReceitas[0].nome;
    
                            listTR[i] = { "label": "" + data.tipoReceitas[0].nome + "", "value" : "" + i + ""};
                        }) 
    
                        //listTipoReceitas(listTR);
    
    
                    });
                console.log(listTR);
                //editor.field('tipoReceitas').update(listTR);
                return listTR;
            };`
    
  • klermannklermann Posts: 277Questions: 67Answers: 1

    Allan is it common for all users with this difficulty to display the data from a list from within a json without editor or just this happening to me due to my json format generated by the server?

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    Allan suggested you put the editor.field('tipoReceitas').update(listTR); inside the ajax done function. You placed it outside the function. Try placing it on line 17.

    Kevin

  • klermannklermann Posts: 277Questions: 67Answers: 1

    And not inside the editor?
    {
    Name: "tipoReceitas"
    // placeholder: 'Tipos', "type": "select", "ipOpts": listTipoReceita ()
    },

    <?php ??? ?>
  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    edited August 2017

    Your function listTipoReceitas() is getting a list of options via ajax, correct?

    What Allan is saying is that the ajax call is asynchronous meaning the script sends the request to the server and continues executing. The function listTipoReceitas() sends the ajax request and continues execution which is to return listTR on line 22. The return happens before the ajax response is received and processed by the done function. Resulting in an empty array being returned.

    The update method updates the options for the provided field. Placing the command in the done function should update the select options for tipoReceitas after the ajax call is complete.

    Kevin

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Yup - exactly what Kevin says. You must keep in mind what the first letter of Ajax stands for - asynchronous.

    Allan

  • klermannklermann Posts: 277Questions: 67Answers: 1
    edited August 2017

    Unfortunately it has not updated, what can I do more?

    function listTipoReceitas(){
                    var listTR = new Array();
                    var getDataAjax = $.ajax({
                        url: "/financeiro/receitas/listAll",
                        }).done(function(obj) {
                            obj.data.forEach(function(data, i) {
                                var nomeTipoReceita = data.tipoReceitas[0].nome;
                                listTR[i] = { "label": "" + data.tipoReceitas[0].nome + "", "value" : "" + i + ""};
                                editor.field('tipoReceitas').update(listTR);
                            }) 
                        });
                    return listTR;};`
    
    
    `{
                              name: "tipoReceitas.nome",
                              placeholder: 'Tipos', "type": "select", "ipOpts": listTipoReceitas() },
    

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Put it outside the forEach.

    Step back and think about it for a moment :). You want to pass an array of items to the update function. That array is being constructed in the forEach. It won't be ready until the forEach is finished.

    Also you are using: editor.field('tipoReceitas') but you have no field called tipoReceitas. You configure Editor thus:

    name: "tipoReceitas.nome",
    

    You need to use that same name.

    Allan

  • klermannklermann Posts: 277Questions: 67Answers: 1
    edited August 2017

    Now I did exactly as you told me, but now the datatable does not seem

    ` function listTipoReceitas(){

                var listTR = new Array();
                var getDataAjax = $.ajax({
    
                    url: "/financeiro/receitas/listAll",
                    }).done(function(obj) {
    
                        obj.data.forEach(function(data, i) {
                            var nomeTipoReceita = data.tipoReceitas[0].nome;
                            listTR[i] = { "label": "" + data.tipoReceitas[0].nome + "", "value" : "" + i + ""};
                        }); 
    
                        editor.field('tipoReceitas').update(listTR);
                    });
    
                return listTR;
            };`
    

    name: 'tipoReceitas', placeholder: 'Tipos', 'type': 'select', 'ipOpts': listTipoReceitas()

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    I think you should leave your editor field name as:
    name: "tipoReceitas.nome",

    And change the update to this:
    editor.field('tipoReceitas.nome').update(listTR);

    Kevin

  • klermannklermann Posts: 277Questions: 67Answers: 1


    The datatable appeared but the empty select

  • klermannklermann Posts: 277Questions: 67Answers: 1
    edited August 2017

    I discovered the error here. This is happening when assembling the list object.
    How can I mount this object with the ":" and comma in object:

    My code:
    listTR[i] = { {label" : "" + nomeTipoReceita + "", "value" : "" + i + ""};

    Original:
    listEt[0] = { "label": "Etiqueta 1", "value": "1" };

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    edited August 2017 Answer ✓

    It looks like the way you originally built the array of objects is good.

    You can use console.log(listTR) right after editor.field('tipoReceitas.nome').update(listTR); to see what was used for the update.

    I wonder if the return listTR; at the end of your function is causing an issue. I would try removing the return listTR;.

    Kevin

  • klermannklermann Posts: 277Questions: 67Answers: 1

    I discovered the error: The error happened because the object of the relationship was coming empty and was giving an error when doing the each, when I found the empty object burst the error, find the data in the table and now select the appeared. Now, you need to treat this object so when it comes empty do not pop the error and continue as usual! Thank you very much for your attention!

This discussion has been closed.