The first render works, the second does not

The first render works, the second does not

klermannklermann Posts: 277Questions: 67Answers: 1

What may be wrong here, because the first render works fine, but the second one appears on the console: TypeError: data [1] is undefined.
My code:

{
data: "minhasContas",
mRender: function (data, type, full){
return data[0].nome;
},
sDefaultContent: ''
},
{
data: "tipoReceitas",
mRender: function (data, type, full){
return data[1].nome;
},
sDefaultContent: ''
},

e may json:

{
"iTotalRecords" : 0,
"iTotalDisplayRecords" : 0,
"data" : [ {
"id" : 3,
"descricaoReceita" : "Entrada 3",
"dataReceita" : {
"year" : 2016,
"month" : "DECEMBER",
"dayOfMonth" : 22,
"dayOfWeek" : "THURSDAY",
"era" : "CE",
"dayOfYear" : 357,
"leapYear" : true,
"monthValue" : 12,
"chronology" : {
"id" : "ISO",
"calendarType" : "iso8601"
}
},
"valorReceita" : 12.00,
"receitaFixa" : false,
"receitaFixaQuantidade" : 0,
"receitaFixaTempo" : null,
"repetirLancReceita" : false,
"repetirPorPeriodo" : null,
"pagamento" : true,
"addObservacao" : "",
"tipoReceitas" : [ {
"id" : 1,
"nome" : "Tipo de receita",
"corTipoReceita" : "#677ae4",
"nomeTipoReceita" : "Tipo de receita"
} ],
"minhasContas" : [ {
"id" : 5,
"nome" : "Banco D",
"saldoAnterior" : 0.00,
"saldoAtual" : 13012.00,
"saldoFinal" : 16000.00,
"cor" : "#568689",
"dashBoard" : true
} ]
}, {
"id" : 4,
"descricaoReceita" : "Entrada 4",
"dataReceita" : {
"year" : 2016,
"month" : "DECEMBER",
"dayOfMonth" : 20,
"dayOfWeek" : "TUESDAY",
"era" : "CE",
"dayOfYear" : 355,
"leapYear" : true,
"monthValue" : 12,
"chronology" : {
"id" : "ISO",
"calendarType" : "iso8601"
}
},
"valorReceita" : 20.00,
"receitaFixa" : false,
"receitaFixaQuantidade" : 0,
"receitaFixaTempo" : null,
"repetirLancReceita" : false,
"repetirPorPeriodo" : null,
"pagamento" : true,
"addObservacao" : "",
"tipoReceitas" : [ {
"id" : 2,
"nome" : "Tipo de receita",
"corTipoReceita" : "#a58add",
"nomeTipoReceita" : "Tipo de receita"
} ],
"minhasContas" : [ {
"id" : 3,
"nome" : "Banco B",
"saldoAnterior" : 200.00,
"saldoAtual" : 24000.00,
"saldoFinal" : 10000.00,
"cor" : "#345349",
"dashBoard" : true
} ]
}, {
"id" : 5,
"descricaoReceita" : "Entrada 5",
"dataReceita" : {
"year" : 2017,
"month" : "JANUARY",
"dayOfMonth" : 11,
"dayOfWeek" : "WEDNESDAY",
"era" : "CE",
"dayOfYear" : 11,
"leapYear" : false,
"monthValue" : 1,
"chronology" : {
"id" : "ISO",
"calendarType" : "iso8601"
}
},
"valorReceita" : 45.45,
"receitaFixa" : false,
"receitaFixaQuantidade" : 0,
"receitaFixaTempo" : null,
"repetirLancReceita" : false,
"repetirPorPeriodo" : null,
"pagamento" : true,
"addObservacao" : "",
"tipoReceitas" : [ {
"id" : 1,
"nome" : "Tipo de receita",
"corTipoReceita" : "#677ae4",
"nomeTipoReceita" : "Tipo de receita"
} ],
"minhasContas" : [ {
"id" : 3,
"nome" : "Banco B",
"saldoAnterior" : 200.00,
"saldoAtual" : 24000.00,
"saldoFinal" : 10000.00,
"cor" : "#345349",
"dashBoard" : true
} ]
}, {
"id" : 6,
"descricaoReceita" : "Entrada 6",
"dataReceita" : {
"year" : 2017,
"month" : "JANUARY",
"dayOfMonth" : 18,
"dayOfWeek" : "WEDNESDAY",
"era" : "CE",
"dayOfYear" : 18,
"leapYear" : false,
"monthValue" : 1,
"chronology" : {
"id" : "ISO",
"calendarType" : "iso8601"
}
},
"valorReceita" : 23.42,
"receitaFixa" : false,
"receitaFixaQuantidade" : 0,
"receitaFixaTempo" : null,
"repetirLancReceita" : false,
"repetirPorPeriodo" : null,
"pagamento" : true,
"addObservacao" : "",
"tipoReceitas" : [ {
"id" : 1,
"nome" : "Tipo de receita",
"corTipoReceita" : "#677ae4",
"nomeTipoReceita" : "Tipo de receita"
} ],
"minhasContas" : [ {
"id" : 2,
"nome" : "Banco A",
"saldoAnterior" : 100.00,
"saldoAtual" : 68805.74,
"saldoFinal" : 46423.42,
"cor" : "#a58add",
"dashBoard" : true
} ]
}, {
"id" : 10,
"descricaoReceita" : "Receita 0205",
"dataReceita" : {
"year" : 2017,
"month" : "APRIL",
"dayOfMonth" : 15,
"dayOfWeek" : "SATURDAY",
"era" : "CE",
"dayOfYear" : 105,
"leapYear" : false,
"monthValue" : 4,
"chronology" : {
"id" : "ISO",
"calendarType" : "iso8601"
}
},
"valorReceita" : 19000.00,
"receitaFixa" : false,
"receitaFixaQuantidade" : 0,
"receitaFixaTempo" : null,
"repetirLancReceita" : false,
"repetirPorPeriodo" : null,
"pagamento" : false,
"addObservacao" : "",
"tipoReceitas" : [ {
"id" : 2,
"nome" : "Tipo de receita",
"corTipoReceita" : "#a58add",
"nomeTipoReceita" : "Tipo de receita"
} ],
"minhasContas" : [ {
"id" : 3,
"nome" : "Banco B",
"saldoAnterior" : 200.00,
"saldoAtual" : 24000.00,
"saldoFinal" : 10000.00,
"cor" : "#345349",
"dashBoard" : true
} ]
}, ]
} ]
}

Replies

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    looking at your data structure, they both should be [0]

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

    In the first one refer to minhasContas and in the second tipoReceitas, therefore different data ...

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Right. one is minhasContas[0].nome, the other is tipoReceitas[0].nome.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Having said that, if that sub array is always only going to be a length of one, I recommend changing your data structure to

    "minhasContas" : {
    "id" : 3,
    "nome" : "Banco B",
    "saldoAnterior" : 200.00,
    "saldoAtual" : 24000.00,
    "saldoFinal" : 10000.00,
    "cor" : "#345349",
    "dashBoard" : true
    } ,

    then you can change your columns to

    columns:[{data:"minhasContas.nome"}, ...

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

    You say take this child out of the signs of [ ]? Structure

  • klermannklermann Posts: 277Questions: 67Answers: 1

    Can you tell me why this conflict happens? And if you can not solve it without changing the structure?

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Have you tried using [0] in both places? I am not telling you to changing the structure. It should work the way it is when you put [0] in both places. I am just making a suggestion that would simplify your columns:[]

  • klermannklermann Posts: 277Questions: 67Answers: 1

    Perfect, but do I need to understand why it works on an array?

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    In the name of simplicity

    {data:"minhasContas[0].nome"},
    {data: "tipoReceitas[0].nome"}
    

    also works as show here http://jsbin.com/yiguper/168/edit

    The reason why is that minhasContas has its on child array and tipoReceitas has its own child array. In each case there is only one record so the [0].

    Do not confuse this sub arrays with your overall array at data:[]

    Does that make sense?

  • klermannklermann Posts: 277Questions: 67Answers: 1

    Thank you very much for the explanation.

This discussion has been closed.