Add and subtract In Editor

Add and subtract In Editor

davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0
edited November 2020 in Free community support

I would like to take column data and add / subtract depending on value.

IE: col1 + col2 - col3
And col2 is a key index of another table.

columnDefs: [
        {
            "targets": 5, #col1
            "data": null,
            "render": function(data, type, full, meta, row) {
                return '$' + data;
            }
        },
        {
            "targets": 6, #col2
            "data": null,
            "render": function(data, type, full, meta, row) {
                return '$' + data;
            }
        },
        {
            "targets": 7, #col3
            "data": null,
            "render": function(data, type, full, meta, row) {
                return '$' + data;
            }
        },
],
    Editor::inst( $db, '_devices' )
        ->fields(
            Field::inst( '_devices.ID' ),
            Field::inst( '_devices.d_modified' )
                ->validator( Validate::dateFormat( 'Y-m-d H:i:s' ) )
                ->getFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) )
                ->setFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) ),
            Field::inst( '_devices.d_dpmax' ),
            Field::inst( '_devices.d_model' ),
            Field::inst( '_devices.d_make' ),
            Field::inst( '_devices.d_upc' ),
            Field::inst( '_devices.d_sku' ),
            Field::inst( '_devices.d_category' ),  #this is column 2
            Field::inst( '_cost.c_sku' ),
            Field::inst( '_cost.c_cost_ingram' ),
            Field::inst( '_cost.c_cost_vzw' ),
                        Field::inst( '_cat.ID' ),
                        Field::inst( '_cat.commission' ),

        )

        ->leftJoin( '_cost', '_cost.c_sku', '=', '_devices.d_sku' )
                ->leftJoin( '_cat', '_cat.ID', '=', '_devices.d_category' )

Column 2 will have a value of 34 which equals $130 or value 22 which equals $90

Thank you

This question has an accepted answers - jump to answer

Answers

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

    I assume that column would be read-only, as it's based on other data. If so, you would do this in columns.render - the third parameter contains the data for the other columns that you can use.

    Colin

  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0
    edited November 2020

    @colin
    Thank you for your feedback. I did manage to get this working last night.

    Here is my code sample.

      {
              data: 'null',
              render: function(data, type, full, meta, row) {
    
                var charge = (full._devices.d_dpmax * .05 >= "50") ? "50":full._devices.d_dpmax * .05;
    
                if(full._cost.c_cost_vzw > full._cost.c_cost_ingram) {
                  var ingram = full._devices.d_dpmax - full._cost.c_cost_ingram - charge + parseFloat(full._commission.co_amount)
                  return "$" + (Math.round(ingram * 100) / 100).toFixed(2);
                }
                else {
                  var vzw = full._devices.d_dpmax - full._cost.c_cost_vzw - charge + parseFloat(full._commission.co_amount)
                  return "$" + (Math.round(vzw * 100) / 100).toFixed(2);
                }
    
              },
            },
    

    Result (Sorry I have to blur some things out. The final result is last column.

This discussion has been closed.