how to summarize two fields?

how to summarize two fields?

CathMulderCathMulder Posts: 5Questions: 1Answers: 0
edited February 2020 in Free community support

I feel a bit stupid, but i can't get it working.
I have 2 int fields, (purchase, marge) and i want to display this computed value:
purchase*((100+marge)/100)

Trying:

        { data: null,
                render: function ( data, type, row ) {
                return ( row.tbl_article.purchase + row.tbl_article.marge )}},

        { data: null,
                render: function ( data, type, row ) {
                return ( row.tbl_article.purchase * row.tbl_article.marge )}},

        { data: null,
                render: function ( data, type, row ) {
                return ( row.tbl_article.purchaseb*b((b100 + row.tbl_article.marge) / 100) )}}

When purchase would be 40 and marge would be 10:
outcome:
first example = 4010
second example = 400 = what to expect
third example = 4004

it seems the + sign combines the field instead of computing the value, but the multiply works correct.
What am i doing wrong here?

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921

    Sounds like your data are strings not numbers. You will need to convert them. This article discusses some options.

    Kevin

  • CathMulderCathMulder Posts: 5Questions: 1Answers: 0
    edited February 2020

    Thanks for answering but they are both numbers. Thats why i added the multiply.

    purchase int(11)
    marge int(11)

    The multiply is doing the expected job. The plus on the same data isn't.

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921

    row.tbl_article.purchase + row.tbl_article.marge

    This is a simple Javascript statement. Datatables does not do anything with or affect this statement. In order to understand the problem we will need to see the data. Please post a link to your page or a test case showing the problem.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • haliorishalioris Posts: 9Questions: 2Answers: 0
    edited February 2020

    You say they are numbers but you said that you expected the result of 40 + 10 to be 4010. If they are numbers the result would be 50. If they are strings the result would be 4010. However if they are strings then 40*10 is definitely not going to be 400. Hence we need to see your data and example.

  • CathMulderCathMulder Posts: 5Questions: 1Answers: 0
    edited February 2020

    Sorry, i can't post a link to my page. This is my script and below are two printscreens.
    One of my sql database and one from the browser.
    And yes, i know this is a simple statement but i don't understand why the multiply works correct and the sum doesn't.

    $(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
    ajax: '../../controllers/article.php',
    // volgorde in de crud
    table: '#example',
    fields: [ {
    label: 'Artikel:',
    name: 'tbl_article.name',
    type: 'text'
    }, {
    label: 'Leverancier:',
    name: 'tbl_article.user_id',
    type: 'select'
    }, {
    label: 'Categorie:',![]

                name: 'tbl_article.site_id',
                type: 'select'
            }, {
                label: 'Inkoopprijs:',
                name: 'tbl_article.purchase'
            }, {
                label: 'Marge:',
                name: 'tbl_article.marge'
            }
        ]
    } );
    
    $('#example').DataTable( {
        dom: 'Bfrtip',
        ajax: {
            url: '../../controllers/article.php',
            type: 'POST'
        },
        // volgorde in de tabel
        columns: [
            { data: 'tbl_article.name' },
            { data: 'tbl_supplier.last_name' },
            { data: 'tbl_category.name' },
            { data: 'tbl_article.purchase' },
            { data: 'tbl_article.marge' },
            { data: null,   // simple sum only for test
                    render: function ( data, type, row ) {
                    return ( row.tbl_article.purchase + row.tbl_article.marge )}},
    
            { data: null,  //simple multiply only for test
                    render: function ( data, type, row ) {
                    return ( row.tbl_article.purchase * row.tbl_article.marge )}},
    
            { data: null,
                    render: function ( data, type, row ) {
                    return ( row.tbl_article.purchase*((100 + row.tbl_article.marge)/100) )}}
    
        ],
        select: true,
        buttons:  [
            { extend: 'create', editor: editor },
            { extend: 'edit',   editor: editor },
            { extend: 'remove', editor: editor }
        ]
    } );
    

    } );

    (https://datatables.net/forums/uploads/editor/tv/lwwd70uhj9vv.png "")

    (https://datatables.net/forums/uploads/editor/vn/vg1hvsedtnq2.png "")

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921
    Answer ✓

    I'm not a JS expert but its a feature of Javascript called Type Coercion. Here is one explanation. You can . see an example here where two strings containing numbers result in a numeric value:
    http://live.datatables.net/tecapiqa/1/edit

    Datatables tries to automatically discover the data type of the column, explained in the columns.type docs. It could be Datatables is typing the column as string. Is there a non-numeric value in either of those columns?

    Kevin

  • CathMulderCathMulder Posts: 5Questions: 1Answers: 0

    Oke, thanks for the example. I think you are right by thinking datatables is typing the columns as string. But how can i make datatables typing it as numeric? There is no non-numeric value in either one. To be sure I tried in PHP with the same database and colums and it behaves correctly.

  • CathMulderCathMulder Posts: 5Questions: 1Answers: 0

    Yes, it' solved now. I added the Number function and now it's works correctly.
    // simple sum only for test
    data: null,
    render: function ( data, type, row ) {
    return Number(row.tbl_article.purchase) + Number(row.tbl_article.marge) }},

    Thanks kthorngren for letting me think in the right direction!

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921
    Answer ✓

    But how can i make datatables typing it as numeric?

    You can try columns.type.

    You can build us an example with your data. Grab the JSON response using the browser's Developer Tools. If using chrome use the Response tab. Steps for this can be found in this technote. Or you can use the Debugger to get the data to provide to the developers to look at.

    Worst case you can use parseInt().

    Kevin

This discussion has been closed.