datetime edit field by default is empty

datetime edit field by default is empty

WININFOWININFO Posts: 19Questions: 2Answers: 0

Hi,

I use Editor and i need to set by default a datetime when i edit selected row, but i have just an empty input.
The field :
My code of table :

 <script type="text/javascript" language="javascript" class="init">
        var editor;
        $(document).ready(function() {
            var today = "<?=$today?>";
            editor = new $.fn.dataTable.Editor({
                language: {
                    url: "https://cdn.datatables.net/plug-ins/1.10.22/i18n/French.json"
                },
                ajax: {
                    url: "<?php print $context->getRootUrl(); ?>../../includes/datatable/controllers/point_relais.php",
                    type: 'POST',
                    data: function(d) {
                        d.id_site = "<?= $id_site ?>";
                    }
                },
                table: '#commande',
                fields: [ {
                    type: 'datetime',
                    name: 'date_retrait',
                    def: function () { return new Date(); },
                    displayFormat: 'DD/MM/YYYY',
                    format: 'YYYY-MM-DD HH:mm:ss',
                },]

            });

            var table = $('#commande').DataTable({
                language: {
                    url: "https://cdn.datatables.net/plug-ins/1.10.22/i18n/French.json"
                },
                ajax: {
                    url: "<?php print $context->getRootUrl(); ?>../../includes/datatable/controllers/point_relais.php",
                    type: 'POST',
                    data: function(d) {
                        d.id_site = "<?= $id_site ?>";
                    }
                },
                pageLength: 20,
                dom: 'BPirt',
                columns: [{
                        targets: 0,
                        data: null,
                        defaultContent: '',
                        orderable: false,
                        className: 'select-checkbox'
                    },
                    {
                        data: 'p.ref'
                    },
                    {
                        data: 'comdet.date_start'
                    },
                    {
                        data: 'p.label'
                    },
                    {
                        data: 'id_site',
                        className: "id_site",
                        visible: false
                    }
                ],

                select: {
                    style: 'multi',
                    selector: 'td:first-child'
                },
                order: [
                    [2, 'desc']
                ],
                responsive: true,
                buttons: [{
                    text: "Valider Commande(s) retirée(s)",
                    extend:'edit',
                    editor:editor
                }]
            });


        });

My controller :

<?php
// error_reporting(E_ALL);
// ini_set("display_errors", 1);
$res = 0;
// Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) $res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"] . "/main.inc.php";
// Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
$tmp2 = realpath(__FILE__);
$i = strlen($tmp) - 1;
$j = strlen($tmp2) - 1;
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
    $i--;
    $j--;
}
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1)) . "/main.inc.php")) $res = @include substr($tmp, 0, ($i + 1)) . "/main.inc.php";
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1))) . "/main.inc.php")) $res = @include dirname(substr($tmp, 0, ($i + 1))) . "/main.inc.php";
// Try main.inc.php using relative path
if (!$res && file_exists("../main.inc.php")) $res = @include "../main.inc.php";
if (!$res && file_exists("../../main.inc.php")) $res = @include "../../main.inc.php";
if (!$res && file_exists("../../../main.inc.php")) $res = @include "../../../main.inc.php";
if (!$res) die("Include of main fails");

require_once(DOL_DOCUMENT_ROOT . '/core/class/confparamglobales.class.php');

$conf_param_globales = new ConfParamGlobales();
$conf_param_globales->setValuesDoliDbClass($db);
$conf_kit_cli = $conf_param_globales->global->WI_BLANCH_KIT_CLIENTELE;
$conf_kit_pr = $conf_param_globales->global->WI_BLANCH_KIT_POINT_RELAIS;



// // DataTables PHP library
require_once("../lib/DataTables.php");


global $user;

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

// Build our Editor instance and process the data coming from _POST

$today = date("Y-m-d H:i:s");

$editor = Editor::inst($db, 'llx_commandedet as comdet', 'rowid')
    ->fields(
        Field::inst('com.rowid'),
        Field::inst('p.ref'),
        Field::inst('comdet.date_start')
            ->getFormatter(Format::dateSqlToFormat('d/m/Y')),
        Field::inst('com.ref'),
        Field::inst('soc.nom'),
        Field::inst('p.label'),
        Field::inst('comdetex.kit_specifique')
            ->getFormatter(function ($val, $data) {
                return $val ? 'Oui' : 'Non';
            }),
        Field::inst('comdetex.date_retrait', 'date_retrait')
            ->validator(Validate::notEmpty(ValidateOptions::inst()
                ->message('Une date est requise !')))
            ->getFormatter(Format::dateSqlToFormat('d/m/Y'))
            ->setFormatter(Format::dateFormatToSql('Y-m-d H:m:s'))
            ->set(Field::SET_EDIT),
        Field::inst('comdetex.fk_statut', 'statut')
            ->set(Field::SET_EDIT)
            ->setValue("$conf_kit_cli"),
        Field::inst('comex.id_site', 'id_site'),
    )
    ->leftJoin('llx_commande as com', 'com.rowid', '=', 'comdet.fk_commande')
    ->leftJoin('llx_commande_extrafields as comex', 'comex.fk_object', '=', 'com.rowid')
    ->leftJoin('llx_commandedet_extrafields as comdetex', 'comdetex.fk_object', '=', 'comdet.rowid')
    ->leftJoin('llx_societe as soc', 'soc.rowid', '=', 'com.fk_soc')
    ->leftJoin('llx_societe as point_relais', 'point_relais.rowid', '=', 'comex.fk_point_relais')
    ->leftJoin('llx_product as p', 'p.rowid', '=', 'comdet.fk_product')
    ->where(function ($q) use ($user, $conf_kit_pr) {
        $q->where('comex.fk_point_relais', $user->socid);
        $q->where('comdetex.date_retrait', null);
        $q->where('comex.id_site', $_POST['id_site']);
        $q->where('comdetex.fk_statut', $conf_kit_pr);
    })
    ->debug(true)
    ->process($_POST)
    ->json();

Replies

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I use Editor and i need to set by default a datetime when i edit selected row, but i have just an empty input.

    Is there a date already in that field? Or do you just want to update an existing date field (something like an "update date" field)?

    If you want a default date to go into a new record, then you would use fields.def as you have - see here too: http://live.datatables.net/qadafite/15/edit

    One thing to note is that you're declared displayFormat as well as format. As noted in the documentation, they're the same thing - one is just an alias for another. You probably wanted to use wireFormat instead of format,

    Colin

  • WININFOWININFO Posts: 19Questions: 2Answers: 0
    edited July 2021

    Hi Colin,

    Thanks for your answer. It's an update field and the date is null in database ...

    Clement

  • WININFOWININFO Posts: 19Questions: 2Answers: 0

    And with an update field, the input is already empty ...

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    If it's an update field, it would be worth doing it on the server - see the first example on this page as it's doing just that.

    Otherwise, you could do it in the browser with open, something like this: http://live.datatables.net/bugekaja/1/edit

    Colin

  • WININFOWININFO Posts: 19Questions: 2Answers: 0

    Thanks a lot Colin.
    It's working fine

    Clement

Sign In or Register to comment.