Update field value if another field value is not empty

Update field value if another field value is not empty

marianadrianmarianadrian Posts: 1Questions: 1Answers: 0

Hi,
I am trying to automatically change the status for a column if another column value is not empty.
For example:
The column status has the following values: 'Invoiced' and 'Not Invoiced';
I want to automatically set the status to 'Invoiced' if the 'Invoice' Column has a value set.

These are my files:
index.js

let manageJobTable;
let editor;

$(document).ready(function () {
    editor = new $.fn.dataTable.Editor({
        "ajaxUrl": "php_action/retrieve.php",
        "domTable": "#manageJobTable",
        // "idSrc": 'idjobs',
        "fields": [
            {
                "label": "STATUS:",
                "name": "status",
                "type":  "select",
                "options": [
                    { "label": "Sent", "value": 'Sent' },
                    { "label": "Invoiced", "value": 'Invoiced' },
                    { "label": "In Progress", "value": 'In Progress' },
                    { "label": "Not Invoiced", "value": 'Not Invoiced' }
                ]
            }, {
                "label": "CLIENT:",
                "name": "client"
            }, {
                "label": "PROJECT:",
                "name": "project"
            }, {
                "label": "CAMPAIGN:",
                "name": "cReff"
            }, {
                "label": "DESCRIPTION:",
                "name": "description",
                "type" : "textarea"
            }, {
                "label": "RECIN:",
                "name": "recin"
            }, {
                "label": "RECOUT:",
                "name": "recout"
            }, {
                "label": "CAMPAIGN DATE:",
                "name": "date",
                "type":  "datetime",
                def:   function () { return new Date(); },
                "format": 'YYYY-MM-DD',
            }, {
                "label": "INVOICE NO:",
                "name": "invoice_no",
            }, {
                "label": "INVOICE DATE",
                "name": "invoice_date",
                "type":  "datetime",
                def:   function () { return new Date(); },
                "format":  'YYYY-MM-DD HH:mm:ss',
                "fieldInfo": "Europe Clock with date and 24 hours style"
            }, {
                "label": "TYPE",
                "name": "type",
                "default" : "MANUAL JOB",
                "placeholder" : "E.G: MANUAL JOB",
                "fieldInfo" : "IF EMPTY MANUAL JOB WILL BE AUTOMATICALLY ADDED"
            }
        ]
    });

    manageJobTable = $("#manageJobTable").DataTable({
        // "dom": 'Bfrtip',
        "dom": '<"dom_wrapper fh-fixedHeader"Bf>tip',
        "ajax": "php_action/retrieve.php",
        "columns": [{
                "data": "client"
            },
            {
                "data": "project"
            },
            {
                "data": "cReff"
            },
            {
                "data": "description"
            },
            {
                "data": "recin",
                "render": $.fn.dataTable.render.number(',', '.', 0)
            },
            {
                "data": "recout",
                "render": $.fn.dataTable.render.number(',', '.', 0)
            },
            {
                "data": "date",
                "type":  'datetime',
                def:   function () { return new Date(); },
                "format": 'YYYY-MM-DD',
                "fieldInfo": 'Euro style date without clock'
            },
            {
                "data": "invoice_no"

            },
            {
                "data": "invoice_date",
                "type":  'datetime',
                def:   function () { return new Date(); },
                "format": 'YYYY-MM-DD HH:mm:ss',
                "fieldInfo": 'Euro style date with 24 hour clock'
            },
            {
                "data": "status",
                "render" : function(data, type){
                    if(type === 'display' && data !== null){
                        if(data === "Sent") {
                            data = '<p class="sent">' + data + '</p>';
                        }
                        if(data === "Invoiced") {
                            data = '<p class="invoiced">' + data + '</p>';
                        }
                        if(data === "Not Invoiced") {
                            data = '<p class="not_invoiced">' + data + '</p>';
                        }
                        if(data === "In Progress") {
                            data = '<p class="draft">' + data + '</p>';
                        }
                    }

                    return data;
                }
            },
            {
                "data": "type"
            },
            {
                "data": "url_details",
                "type" : "weblink",
                "render": function(data, type){
                    if(type === 'display' && data !== null){
                        data = '<a href="' + data + '" target="_blank">' + data + '</a>';
                    }
                    return data;
                }
            }
        ],
        "select": true,
        "paging": true,
        "buttons": [{
                "extend": 'collection',
                "text": 'Export As',
                "buttons": [
                    'copy',
                    'excel',
                    'csv'
                ]
            }, {
                "extend": "create",
                "editor": editor
            }, {
                "extend": "edit",
                "editor": editor
            }, {
                "extend": "remove",
                "editor": editor
            },
            'pageLength'
        ],
        "lengthMenu": [
            [10, 25, 50, -1],
            ['10 rows', '25 rows', '50 rows', 'Show all']
        ],
        "stateSave": true,
        "order": [
            [6, 'desc']
        ],
        "displayLength": 50
    });
});

PHP

<?php 
include '../custom/editor/lib/DataTables.php';
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Validate;

Editor::inst( $db, 'jobs', 'idjobs' )
    ->fields(
        Field::inst( 'status' ),
        Field::inst( 'client' ),
        Field::inst( 'project' ),
        Field::inst( 'cReff' ),
        Field::inst( 'description' ),
        Field::inst( 'recin' ),
        Field::inst( 'recout' ),
        Field::inst( 'date' ),
        Field::inst( 'invoice_no' ),
        Field::inst( 'invoice_date' )
            ->validator( 'Validate::dateFormat', [
                'empty' => true,
                'format' => 'Y-m-d H:i:s'
            ])
            ->getFormatter('Format::datetime', [
                'from' => 'Y-m-d H:i:s',
                'to' =>   'Y-m-d H:i:s'
            ])
            ->setFormatter('Format::datetime', [
                'from' => 'Y-m-d H:i:s',
                'to' =>  'Y-m-d H:i:s'
            ]),
        Field::inst( 'type' ),
        Field::inst( 'url_details' )
    )
    ->process( $_POST )
    ->json();

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    Answer ✓

    Hi @marianadrian ,

    Do you want to make that change back in the DB, or just in the table view? If in the table view, you can use columns.render. If you want it changed when editing begins, you could do something like this.

    Hope that helps,

    Cheers,

    Colin

This discussion has been closed.