In editor - how can I return a custom field from the server side script based on a function call?

In editor - how can I return a custom field from the server side script based on a function call?

mikducmikduc Posts: 31Questions: 8Answers: 0

Sample code:
DESCRIPTION is a custom field.
GetDesc is a custom function.

    Field::inst( 'DESCRIPTION' )
        ->getFormatter( function($val, $data, $opts) {
            return GetDesc(parm1,parm2,parm3);
        } ),

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    Hi,

    Thanks for the question. That looks like all you need to do - the get formatter will be given the DESCRIPTION from the database, do whatever processing you need to it (in this case GetDesc - I'm not sure what that does) and then use the value the formatter returns.

    I'm not clear on what aspect of that isn't working in your above code?

    Allan

  • mikducmikduc Posts: 31Questions: 8Answers: 0

    Allan,

    In my example the DESCRIPTION is not a field in the current database. I am using the custom function GetDesc(passed parms) to retrieve the description from another database table and return to the front end script.

    Thanks,
    Mike

  • mikducmikduc Posts: 31Questions: 8Answers: 0

    Allan,

    I was able to get the desired result but I'm not sure this will work for multiple fields.

    JavaScript:
    columns: [
    { data: "ITEM_NUM" },
    { data: "" }, // item description
    { data: "REPERCENT" },
    { data: "PERCENTAGE" }
    ],

    Server Side:
    ->fields(
    Field::inst( 'SEQ_NUM' )
    ->getFormatter( 'Format::Trim' )
    ->setFormatter( 'Format::Trim' ),
    Field::inst( 'REC_TYPE' )
    ->getFormatter( 'Format::Trim' )
    ->setFormatter( 'Format::Trim' ),
    Field::inst( 'ITEM_NUM' )
    ->getFormatter( 'Format::Trim' )
    ->setFormatter( 'Format::Trim' ),
    Field::inst( 'ITEM_REV' )
    ->getFormatter( 'Format::Trim' )
    ->setFormatter( 'Format::Trim' ),
    Field::inst( 'LOC_ID' )
    ->getFormatter( 'Format::Trim' )
    ->setFormatter( 'Format::Trim' ),
    Field::inst( NULL )
    ->getFormatter( function($val, $data, $opts) {
    switch ($data['REC_TYPE']) {
    case 'N':
    return GetNPDesc_Alt($data['ITEM_NUM'],$data['ITEM_REV'],$data['LOC_ID']);
    break;
    case 'X':
    return GetNPDesc_Alt($data['ITEM_NUM'],$data['ITEM_REV'],$data['LOC_ID']);
    break;
    case 'F':
    return GetPFDesc($data['ITEM_NUM'],$data['LOC_ID']);
    break;
    case 'R':
    return substr($data['ITEM_NUM'],0,1) != 'Z' ? GetRMDesc($data['ITEM_NUM'],$data['LOC_ID']) : GetRMExpDesc($data['ITEM_NUM'],$data['LOC_ID']);
    break;
    }
    } ),
    Field::inst( 'REPERCENT' )
    ->getFormatter( 'Format::Trim' )
    ->setFormatter( 'Format::Trim' ),
    Field::inst( 'PERCENTAGE' )
    ->getFormatter( 'Format::Trim' )
    ->setFormatter( 'Format::Trim' ),
    Field::inst( 'LB_RATE' )
    ->getFormatter( 'Format::Trim' )
    ->setFormatter( 'Format::Trim' )
    )

    Thanks for any input you can provide.

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    There is a Field->getValue() method that you could use to get the value without Editor's own database call, but making another query for each hit would likely lead to performance issues fairly quickly.

    If you can, a left join is the way to go to get extra information, but that depends on your database schema.

    Allan

  • mikducmikduc Posts: 31Questions: 8Answers: 0

    Allan,

    Thanks for the response.

    One quick follow-up. For one of the fields I need to return data to the client for a non-existent field in the current database table. It seems like I should be able to provide an alias name for the non-existent data to client.

    Hope this makes sense.
    Thanks, Mike

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    The Field->getValue() method is the way to do that as well. The name that you give to the field will be what is used in the JSON set to the server.

    Allan

  • mikducmikduc Posts: 31Questions: 8Answers: 0

    Allan,

    I understand the getValue() concept. However, am I able get a value based on a function call? See the illustration below.

    This is the error I am getting:

    Fatal error: Uncaught ArgumentCountError: Too few arguments to function {closure}(), 0 passed in /www/zendphp7/htdocs/DataTables_Editor/php/Editor/Field.php on line 775 and exactly 3 expected in /www/zendphp7/htdocs/researchdevelopment/formulamanagement/ssp_SampleFormulaDetailDisplay.php:81 Stack trace: #0 /www/zendphp7/htdocs/DataTables_Editor/php/Editor/Field.php(775): {closure}() #1 /www/zendphp7/htdocs/DataTables_Editor/php/Editor/Field.php(593): DataTables\Editor\Field->_getAssignedValue(Object(Closure)) #2 /www/zendphp7/htdocs/DataTables_Editor/php/Editor/Field.php(693): DataTables\Editor\Field->val('get', Array) #3 /www/zendphp7/htdocs/DataTables_Editor/php/Editor/Editor.php(996): DataTables\Editor\Field->write(Array, Array) #4 /www/zendphp7/htdocs/DataTables_Editor/php/Editor/Editor.php(859): DataTables\Editor->_get(NULL, Array) #5 /www/zendphp7/htdocs/DataTables_Editor/php/Editor/Editor.php(661): DataTables\Editor->_process(Array) #6 /www/zendphp7/htdocs/researchdevelopment/formulamanagement/ssp_SampleFormulaDeta in /www/zendphp7/htdocs/researchdevelopment/formulamanagement/ssp_SampleFormulaDetailDisplay.php on line 81

    Unfortunately, I have not been able to locate any example that demonstrate what I am trying to accomplish.

    Thank you for your help,
    Mike


    Field::inst( 'description' ) // ->getValue('This is a test'), ->getValue( function($val, $data, $opts) { switch ($data['REC_TYPE']) { case 'N': return GetNPDesc_Alt($data['ITEM_NUM'],$data['ITEM_REV'],$data['LOC_ID']); break; case 'X': return GetNPDesc_Alt($data['ITEM_NUM'],$data['ITEM_REV'],$data['LOC_ID']); break; case 'F': return GetPFDesc($data['ITEM_NUM'],$data['LOC_ID']); break; case 'R': return substr($data['ITEM_NUM'],0,1) != 'Z' ? GetRMDesc($data['ITEM_NUM'],$data['LOC_ID']) : GetRMExpDesc($data['ITEM_NUM'],$data['LOC_ID']); break; } } ),
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    The ->getValue() method isn't passed any parameters which is why you are getting that error. i.e. $val, $data, $opts shouldn't be there (that's a validator signature).

    If you want to base the value on other data in the row, what to do is use getValue() returning simply an empty string (to bypass the database lookup for that field) and then use a formatter to base it on the data in your row read from the database.

    Allan

This discussion has been closed.