Calculate the value of a field "b" in GetFormatter function of a field "a"

Calculate the value of a field "b" in GetFormatter function of a field "a"

jgcaudetjgcaudet Posts: 77Questions: 5Answers: 0
edited April 2014 in DataTables 1.9
Hi Allan. I use datatables 1.9.4
In the get.php I use a getFormatter function to calculate the value of a field

Field::inst( 'etl_product_desc' )->getFormatter( function($val, $data, $field) {
..... here I make a select and return the value of 'etl_product_desc' ...
}

Well, I need to calculate inside this function the value of another field that I retrive in this get.php with another "Field::inst"

Is possible ? How can I do ?

Thanks

Replies

  • allanallan Posts: 54,881Questions: 1Answers: 8,601 Site admin
    The second parameter ( `$data` in your above example) contains the data for the whole row. So you might use `$data['myField1']` for example.

    Allan
  • jgcaudetjgcaudet Posts: 77Questions: 5Answers: 0
    It doesn,t Works. When I saw rows of datatables this field doesn,t reflect changes I made.
  • allanallan Posts: 54,881Questions: 1Answers: 8,601 Site admin
    Can you show me the code you used please?

    Allan
  • jgcaudetjgcaudet Posts: 77Questions: 5Answers: 0
    edited April 2014
    [code]
    $editor = Editor::inst( $db, 'vista_etl' )
    ->fields(
    Field::inst( 'id' ),
    Field::inst( 'may_id' ),
    Field::inst( 'productid' )->set(false),
    Field::inst( 'etl_product_desc' )
    ->getFormatter( function($val, $data, $field) {
    global $rdo ;
    $psDescripcionDetallada = '' ;
    $piProductId = $data['productid'] ;
    if (!$piProductId || $piProductId == "") {
    //
    }
    else {
    //
    $query = "SELECT `a`.*, `b`.`name`, `c`.`headerid`, `c`.`defaultdisplayorder` as `Attributedefaultdisplayorder`, `d`.`name` as `headername`, `e`.`defaultdisplayorder` FROM `productattribute` `a`
    WHERE ... ";

    //
    if (bd_query($query)) {
    $psDescripcionDetallada = ' ..... ' ;

    $piHeaderId = 0 ;
    $i = 0;
    while($row = $rdo->fetch_assoc()) {
    if ($row['headerid'] != $piHeaderId) {
    //
    $psDescripcionDetallada = $psDescripcionDetallada . ' ....'

    $piHeaderId = $row['headerid'] ;
    }

    $psDescripcionDetallada = $psDescripcionDetallada . ' ....'

    $i++;
    } ;

    $psDescripcionDetallada = $psDescripcionDetallada . '' . '';

    }
    else {
    };

    }; // Del if (!$piProductId || .....

    if ($psDescripcionDetallada != "" ) { $data['tiene_desc_etl'] = 'Si' ;} // HERE I CHANGE
    return $psDescripcionDetallada ;

    })
    ->set(false),
    Field::inst( 'tiene_desc_etl' )->set(false)
    )
    [/code]
  • allanallan Posts: 54,881Questions: 1Answers: 8,601 Site admin
    That's a belter of a formatter!

    So I understand correctly, is it this part which isn't working as you expected: `$piProductId = $data['productid'] ;` ? I'll try to create an example here that reproduces that error can get back to you.

    Allan
  • jgcaudetjgcaudet Posts: 77Questions: 5Answers: 0
    No.

    I want that if this field 'etl_product_desc' which value I calculate in its getFormatter function (that I show you here) is not empty, then I change the value of 'tiene_desc_etl' with

    $data['tiene_desc_etl'] = 'Si' ;

    in this line if ($psDescripcionDetallada != "" ) { $data['tiene_desc_etl'] = 'Si' ;} // HERE I CHANGE

    I test directly $data['tiene_desc_etl'] = 'Si' without the "if ...." and if doesn´t work too.

    Thanks.
  • jgcaudetjgcaudet Posts: 77Questions: 5Answers: 0
    sorry I mean "it doesn´t work too".
    The getformatter function works ok (calculate the value for 'etl_product_desc' correctly), but my datatables shows me column 'tiene_desc_etl' with all values to 'No'.
  • jgcaudetjgcaudet Posts: 77Questions: 5Answers: 0
    Could you reproduce it ?
This discussion has been closed.