Is there a way I can populate a field on load?

Is there a way I can populate a field on load?

orionaseliteorionaselite Posts: 49Questions: 13Answers: 4

Is there a way I can populate a field on load from an remote API and update my db field immediately?

This question has accepted answers - jump to:

Answers

  • orionaseliteorionaselite Posts: 49Questions: 13Answers: 4

    I was thinking renderer but I am not sure how. The API returns json

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    Could you clarify what field for me please? Are we talking about an Editor field, and do you want to populate its options (e.g. in a select) list?

    Allan

  • orionaseliteorionaselite Posts: 49Questions: 13Answers: 4
    edited August 2017

    Assume I have a db with a field called price I have more but I am trying to make it simple to explain. Assuming that my primary key has a value of ABC I need to make an external api call to www.example.com/api/price/{whatever the primary key value was}/ it will return 2 for example in json format. I than need to update the price cell for ABC with that returned value from the external api. This for example need to happen every lets say 24 hours and yes they are editor fields if that matters

  • orionaseliteorionaselite Posts: 49Questions: 13Answers: 4

    I almost have a solution but I have an issue with fnUpdate. It says fnUpdate is not a function. I am sure it's a stupid mistake somewhere I guess. My code is this

    /*do stuff after table renders and redarw it*/
    $('#portfolio').on( 'draw.dt', function () {
        /*do a redraw and show me the time*/
        var mytime = new Date().getTime();
        console.log('event occured at '+new Date(mytime));
        /*end telling me the time*/
       
       /*start make array of coins in portfolio*/
      /*start*/
     var data;
     var table = $('#portfolio').DataTable();
        table.column( 0 ).data().each( function ( value, index ) {
            console.log( 'Data in index: '+index+' is: '+value );
            /*api call start*/
           $.getJSON("https://api.example.com/"+value+"/", data, function (result,table) {
          //var table = $('#portfolio').DataTable();
           console.log("json "+result[0].price_usd)
           //table.fnUpdate( result[0].price_usd, index , 2);
           table.fnUpdate( result[0].price_usd, index, 2,true ); // Single cell
    });
           /*api call end*/
        } );
        /*end*/
      /*end make array of coins in portfolio*/
       /*start grabs from API*/
      /*end grabs from API*/
    } ); 
    
    

    the line
    table.fnUpdate( result[0].price_usd, index, 2,true ); // Single cell

    I assume will update row 0 column 2 with value result[0].price_usd and redraw and
    row 1 column 2 with value result[0].price_usd and redraw

    ideas why table.fnUpdate is not a funct5ion

  • orionaseliteorionaselite Posts: 49Questions: 13Answers: 4
    Answer ✓

    solved

          $('#portfolio').dataTable().fnUpdate( result[0].price_usd, index, 2,true ); // Single cell
    
  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin
    Answer ✓

    I would suggest using cell().data() rather than the legacy API, but its good to hear you have it working now.

    Allan

This discussion has been closed.