Update a dependent editor form field

Update a dependent editor form field

johnabeckjohnabeck Posts: 2Questions: 1Answers: 0

I need to access the price which is included in the call back line 1, the set(val) on line 2 is returning with the id or label value of line 1 so I need to figure out how to extract the value.

response: {"label":"98997 (3.95)","value":"3.95"},

    editor.dependent('order_items.bipad', function(val, data, callback) {
    editor.field('order_items.cover').set(val);
    editor.disable('order_items.cover');
});

    Field::inst( 'order_items.bipad' )
    ->options( Options::inst()
    ->table( 'magazines' )
    ->value( 'id' )
    ->label( array('id','magName') )
    ->render(function ( $row ) {
        return $row['id'].' ('.$row['magName'].')';     
    } )),

    Field::inst( 'order_items.cover' )
    ->options( Options::inst()
    ->table( 'magazines' )
    ->value( 'magPrice' )
    ->label(  array('id','magPrice') )
    ->render(function ( $row ) {
        return $row['id'].' ('.$row['magPrice'].')';
    } )),

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406
    edited April 2020 Answer ✓

    You can access the options on xhr when they are loaded from the server, do something with them, save them to a global variable - whatever.

    table
      .on('xhr', function( e, settings, json, xhr ) {
          if ( json != null ) {
              if ( typeof json.options !== 'undefined' ) {
                  var oico= json.options["order_items.cover"];
                  var i = 0;
                  while ( oico[i] ) {
                      oico[i].label   //do something with it
                      oico[i].value  //do something with it
                      i++;
                  }
              }
          }          
      });
    
  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406
    edited April 2020 Answer ✓

    You are trying to save a price in order_items.cover and get this from a different table. I've not seen that before; normally people save foreign keys. But anyway.

    "cover" will eventually contain a price but only AFTER it was selected (if this is a CREATE situation).

    editor.dependent('order_items.bipad', function(val, data, callback) {
        editor.field('order_items.cover').set(val);
        editor.disable('order_items.cover');
    });
    

    Assuming you are in an EDIT situation OR you have already picked a value for "cover" during CREATE you could do this:

    editor.dependent('order_items.bipad', function(val, data, callback) {
        editor.set({'order_items.cover': editor.val('order_items.cover')});
        editor.disable('order_items.cover');
    });
    

    Quite absurd isn't it: But where else do you want to take the price from? "bipad" doesn't contain a price, only "cover" will contain the price after selection ... :smiley:

  • johnabeckjohnabeck Posts: 2Questions: 1Answers: 0

    Thank you,

    a little more code may have made it more clear.
    the cover is only grabbed to allow a new event:

       function multiply () {
       editor.field( 'order_items.total' ).val(
       editor.field( 'order_items.draw' ).val() * editor.field( 'order_items.cover' ).val()
    );}
    
       editor.field( 'order_items.draw' ).input().on( 'keyup', multiply );
       editor.field( 'order_items.cover' ).input().on( 'keyup', multiply );
    

    So only need brought in to allow me to establish a total based on draw.
    The BIPAD is an index on a leftjoin.

This discussion has been closed.