Calculated columns - and read only

Calculated columns - and read only

ShaneBrennanShaneBrennan Posts: 47Questions: 17Answers: 1

I've got a screen/ Editor setup to accept Sample results for various type of moulds and germs. I can get this working fine to enter the data, but I require 2 extra columns called "Mould Total" and "Overall Total" - which will update when I tab/click out of the cell.

The Datatable is setup as follows:

     var table = $('#example').DataTable( {
        dom: "Bfrtip",
        ajax: "../server_side/scripts/ET_AQIairstipsResultsForProjectID.php?projectID=<?=$projectID?>",
        iDisplayLength: 25,
        responsive: true,
        columns: [
           { data: "tblairLocation.locationNo",  width:"10%"},
           { data: "tblairLocation.locationName",  width:"30%"},
           { data: "tblairSamples.sampleNo",  width:"10%"},
           { data: "tblairSamples.sample1", className: "editable",  width:"10%" },
           { data: "tblairSamples.sample2", className: "editable",  width:"10%" },
           { data: "tblairSamples.sample3", className: "editable",  width:"10%"},
           { data: "tblairSamples.sample4", className: "editable",  width:"10%"},
           { data: "tblairSamples.sample5", className: "editable",  width:"10%"}
        ],
        columnDefs: [
           { orderable: true, targets: '_all' }
        ],       
        order: [ 1, 'asc' ],
        keys: {
           columns: ':not(:first-child)',
           keys: [ 7 ]
        },
        select: {
           style:    'os',
           selector: 'td:first-child'
        },
        buttons: [

        ]
     } ); 

The editor is setup as follows:

     editor = new $.fn.dataTable.Editor( {
        ajax: "../server_side/scripts/ET_airstips.php?projectID=<?=$projectID?>",
        table: "#example",
        fields: [ { name: "tblairLocation.locationNo" },
                  { name: "tblairLocation.locationName" }, 
                  { name: "tblairSamples.sampleNo" }, 
                  { name: "tblairSamples.sample1" },
                  { name: "tblairSamples.sample2" },
                  { name: "tblairSamples.sample3"},
                  { name: "tblairSamples.sample4"},
                  { name: "tblairSamples.sample5"}
        ],
        formOptions: {
           inline: {
              onBlur: 'submit'
           }
        }
     } );

and the data is send over from the server using:

<?php

/*
* Example PHP implementation used for the index.html example
*/

// DataTables PHP library
include( "../../php/DataTables.php" );

// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate;

$reqID = 0;
if (isset($_GET['projectID'])) {
$reqID = $_GET['projectID'];
// ToCleaned($reqID);
}

// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'tblAQIlocationSamples' )
->field(
Field::inst( 'tblairSamples.id' ),
Field::inst( 'tblairLocation.locationNo' ),
Field::inst( 'tblairLocation.locationName' ),
Field::inst( 'tblairSamples.sampleNo' ),
Field::inst( 'tblairSamples.sample1' ),
Field::inst( 'tblairSamples.sample2' ),
Field::inst( 'tblairSamples.sample3' ),
Field::inst( 'tblairSamples.sample4' ),
Field::inst( 'tblairSamples.sample5' )
)
->where('tblairSamples.projectID',$reqID)
->where('tblairSamples.sampleType','Airstrip')
->leftjoin( 'tblairLocation', 'tblairLocation.id', '=', 'tblairSamples.AQIlocationID' )
->process( $_POST )
->json();

I want to place the 2 extra colmns after Sample 5.

The 1st column needs to add Sample 1, 2 and 3 together
The 2nd column needs to add att 5 samples together.

Thank you inadvance for any help you can offer.

Shane Brennan

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 8,085Questions: 0Answers: 1,358
    Answer ✓

    You can use columns.render to do that - something like this example. There, it's modifying the final column to be the age field plus a string - you would use the same principle to update your columns.

    Colin

  • ShaneBrennanShaneBrennan Posts: 47Questions: 17Answers: 1
    edited January 13

    Thanks Colin, I changed the columnDeps to:

         columnDefs: [
               { orderable: true, targets: '_all' },
               { targets: 8,
                 data: "totalMould",
                 render: function(data, type, row, meta){
                     theTotal =  Number(row['tblairSamples']['sample1']) + Number(row['tblairSamples']['sample2']) + Number(row['tblairSamples']['sample3']) ;
                     return  theTotal.toFixed(1) ;
                 }
               } ,
               { targets: 9,
                 data: "totalCFU",
                 render: function(data, type, row, meta){
                     theTotal =  Number(row['tblairSamples']['sample1']) + Number(row['tblairSamples']['sample2']) + Number(row['tblairSamples']['sample3']) + Number(row['tblairSamples']['sample4']) + Number(row['sample5']['penicilliumCFU']);
                     return  theTotal.toFixed(1) ;
                 }
               }
    

    and it worked a treat..

Sign In or Register to comment.