Calculated Field

Calculated Field

Ed MartinEd Martin Posts: 23Questions: 5Answers: 0

I am trying to update a field based on the value entered into another field. I have a table that tracks the assignment of serialized tags. When the starting tag number is placed in bluetagstart, I would like the bluetagend field to be populated with the value of the starting tag + the quantity minus 1. I was using render to try this and had a couple of issues; it appears I am getting strings instead of numeric values and while the concatenated strings render, I realize that this will not update the database. What is the correct way to handle this calculation?

    {
                "data": "quantity"
            },
            {
                "data": "product"
            },
            {
                "data": "customerid"
            },
            {
                "data": "bluetagstart"
            },
            {
                "data": "bluetagend",
                 render: function (data, type, row) {
                    return ((row.bluetagstart + row.quantity)-1);
                }
            }

This question has an accepted answers - jump to answer

Answers

  • Ed MartinEd Martin Posts: 23Questions: 5Answers: 0

    I am looking at the dependent API now...

  • kthorngrenkthorngren Posts: 21,171Questions: 26Answers: 4,922

    Since you are looking at the dependent() API I assume you are using the Editor.

    it appears I am getting strings instead of numeric values and while the concatenated strings render

    The columns.render is independent of the Editor. When loading the table initially does it contain strings? Trying to understand if this is from the start or after you update the data with the Editor. You could convert the strings to a number by using Number() or multiplying the value by 1, for example return ((row.bluetagstart * 1 + row.quantity * 1)-1);.

    I realize that this will not update the database.

    Are you wanting to update the DB with the calculated value?

    Kevin

  • Ed MartinEd Martin Posts: 23Questions: 5Answers: 0
    edited June 2020

    Thanks for the help Kevin.
    The table has NULL values when loaded but the database fields are integer. I took your advice and used Number() and now it is working perfectly.

    I am getting the proper values in my blueTagEnd field, but the database is not being updated.

    Yes, I would like to update the database fields blueTagStart and blueTagEnd. The calculation is designed to reduce key entry (and errors). Since the tags are sequential, I wanted to calculate the ending number for that item.

    The process is assigning serialized tags to specific products. These tags are required by the state and must be "logged". This is how I planned to track the tags

    The actual tags will be affixed to the picking tag that is sent to the yard. As the product is selected for loading, the tags are placed on the product.

  • kthorngrenkthorngren Posts: 21,171Questions: 26Answers: 4,922
    edited June 2020 Answer ✓

    I would consider using the query that inserts/updates the record in the DB to calculate bluetagend. I think this would be much easier than having Datatables calculate it and then sending the updated value to the DB. You can then eliminate the need for columns.render. Also this way the poper value is there if you choose to use this data elsewhere.

    Kevin

  • Ed MartinEd Martin Posts: 23Questions: 5Answers: 0

    I am thinking I will call a stored procedure after the editor updates. Thanks again, Kevin

This discussion has been closed.