octobercmsoctobercms-backendoctober-form-controller

OctoberCMS - Is it possible to do a simple math in backend?


So i have a fields of total_quantity and order_quantity in the backend which are from the database. Is it possible to subtract these two value and the result is displayed in a new fields? Basically the result fields is name as left_quantity and not in the database.


Solution

  • You can use partial field type for this.

    check below demo you can use it to create own partial to show exactly what you want. For demo I already created one table, with field title.

    Now, I want to do some operation/modification on title and show it just like you want.

    fields.yaml file

    fields:
        title:
            label: Title
            span: auto
            type: text
        other_title:
            label: Modification
            span: auto
            type: partial
            path: $/hardik/demo/controllers/items/_my_field.htm
    
    

    make sure to replace your partial path

    _my_field.htm file

    
    <?php
        // for new record
        $data = 'New record';
    
        if($model->title) {
            $data = 'Modified ' . $model->title;
        }
    
    ?>
    <input 
        type="text" 
        value="<?= $data ?>" 
        class="form-control" 
        autocomplete="on" 
    />
    

    Here we are checking if we are creating record it means $model is not having title so we just show New Record. if we have already saved record and we have data in title we perform modification.

    Output

    For new records

    new_record

    For already created records

    already_exist_image

    if any doubts please comment