Add Info To Editor On Open

Add Info To Editor On Open

dataman123dataman123 Posts: 11Questions: 5Answers: 0

I'm trying to get extra values from the database to show up with the editor values - mhlSent and mhlReceived.

Link to test case:

<?php
// This editor instance writes info to the consumers table.

require 'editor.php';

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Options,
    DataTables\Editor\Format,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

// Build our Editor instance and process the data coming from _POST
$editorInstance = Editor::inst( $db, 'employees' )
->fields(
    Field::inst('employees.id as id'),
    Field::inst('employees.fname as fname')
    ->validator( Validate::notEmpty())
)
->on( 'preEdit', function ( $editor, $id, $values ) use ($db)  {
    $getDates = $db->sql("select * from employee_dates where employee_id = " . $values["id"]. " order by record_date desc")->fetchAll();
    function filterDate($array, $keyValue)
    {
        $filter = array_values(array_filter($array, function($value) use ($keyValue) {
           return $value["date_type"] == $keyValue; 
        }));
        if(count($filter) > 0) {
            return $filter[0]["activity_date"];
        } else {
            return false;
        };
    }
    $values["mhlSent"] = filterDate($getDates, "11");
    $values["mhlReceived"] = filterDate($getDates, "0");

    return $values;
    } )

I added values from the database on preEdit and I see them in the $values array, but when I do

documentationEditor.on('initEdit', function(e, node, data, items, type) {
  console.log(e, node, data, items, type);
})

It's not in the "data" array with the other fields.
How can I get that info that was sent?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,230Questions: 1Answers: 10,417 Site admin
    Answer ✓

    Hi,

    preEdit on the server-side is executed once the data has been submitted to the server, but before it is written to the database.

    It sounds like you want to make an Ajax request to get the extra data to show in the form when the form is initially opened - would that be right? If so, your use of initEdit in the Javascript is correct, but in there, you would need a call to a PHP script which would return the information that is needed for those two extra fields, which you can then set with field().val().

    Allan

  • dataman123dataman123 Posts: 11Questions: 5Answers: 0

    I'm going to put an ajax call into the initEdit. Thank you very much!

Sign In or Register to comment.