Can't able to get row data after populating table from JSON data

Can't able to get row data after populating table from JSON data

deepdicedeepdice Posts: 7Questions: 2Answers: 0
edited October 2019 in Free community support

$(document).ready(function () { $.getJSON("https://archive.primedice.com/2bd01bb9-7c08-4ac3-b604-41d7fd502632", function( data ) { var companyTable; companyTable = $('#data-table').DataTable({ "data" : data, columns : [ {"data" : "id"}, {"data" : "amount"}, {"data" : "currency"}, {"data" : "statePrimedice.condition"}, {"data" : "payoutMultiplier", render: function ( data, type, row ) { return (data).toFixed(2) + "x"; }}, {"data" : "statePrimedice.result", render: function ( data, type, row ) { return (data).toFixed(2) + ""; } } ] }); $('#data-table tbody').on('click', 'tr', function () { var name = $('td', this).eq().text(); console.log( companyTable.row(this).data()[1]); $("#json bet id").val(companyTable.row(this).data()[0]); $("#result").val(companyTable.row(this).data()[5]); $('#DescModal').modal("show"); }); }); });

can't able to get row.data it keeps giving undefined :|

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586
    Answer ✓

    Hi @deepdice ,

    It's because your data is an object - so change

                     console.log( companyTable.row(this).data()[1]);
    

    to be

                     console.log( companyTable.row(this).data().amount);
    

    See here.

    Cheers,

    Colin

  • deepdicedeepdice Posts: 7Questions: 2Answers: 0

    Before i was using it as an array ?

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    You're using columns.data and referencing into the array, so I don't understand that.

  • deepdicedeepdice Posts: 7Questions: 2Answers: 0

    hey can i ask one more thing like i am not able to do this as i am trying to use the cell data in another function but i am only able to access that inside that getBetId() function and not able to render it into cell.

      <script type="text/javascript">
        //Function to get betid from JSON data
            function getBetId(id, callback) {
            var betid = "default";
            var jsonURL = "https://api.primedice.com/graphql?query={%20bet(betId:%20%22" +id+ "%22)%20{%20iid%20}%20}";
          // console.log(jsonURL);
          $.getJSON(jsonURL,function(data) {
            var text = `${data.data.bet.iid}`;
            var betId = text.substring(6,text.length);
           callback(betId);
          });
        }
      </script>
      <script>
        $(document).ready(function(){
                  var betId;
                  $.getJSON( "https://archive.primedice.com/2bd01bb9-7c08-4ac3-b604-41d7fd502632", function( data ) {
                      $('#data-table').DataTable({
                         "data" : data,
                         columns : [
                          {"data" : "id",
                      "render": function ( data, type, row, meta ) {
                        //I want to call that function here and render it into this cell
                        getBetId(data, function(betId) {
                              console.log(betId); 
                              return betId;
                          });
                         return '<a href="https://primedice.com/?iid=house%3A'+betId+'&modal=bet">'+data+'</a>';
                         }},
                          {"data" : "amount"},
                          {"data" : "currency"},
                          {"data" : "payoutMultiplier"},
                          {"data" : "statePrimedice.result"}
                         ]
                    });
                    });
                });
      </script>
    
This discussion has been closed.