Value not saved

Value not saved

tomleongtomleong Posts: 13Questions: 5Answers: 0
edited December 2019 in Free community support

Hi all,

I use an editor to allow user to edit order lines. if user changes the order quantity, then the order amount will be computed (order quantity * unit price) again. But the amount can not be updated to my MS SQL database after editing.
Can anyone tell me what i am missing? I am using inline editing.

editor.on('preSubmit', function (e, o, action) {
                if (action !== 'remove') {
                    var orderQuantity = this.field('orderQuantity');
                    var unitPrice = this.field('unitPrice');
                    var amount = this.field('amount');
                    
                    amount.val(parseFloat(orderQuantity.val()) * parseFloat(unitPrice.val()));
                    alert(amount.val().toString());
                    editor.submit(); 
                    // If any error was reported, cancel the submission so it can be corrected
                    if (this.inError()) {
                        return false;
                    }
                }
            });

Thanks.

Best regards,
Tom Leong

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    You don't need the editor.submit() on line 9 as Editor is in the process of submitting already. Could you remove that and see if it helps.

    Colin

  • tomleongtomleong Posts: 13Questions: 5Answers: 0

    Dear Colin,

    i removed the line but the script still could not update the calculated fields into SQL table. The following are the script of my editor and datatable. Please advise if any.

    editor = new $.fn.dataTable.Editor({
                    ajax: '/api/SalesOrderLines/' + $("#OrderNo").val() + '/orderNo',
                    table: '#SalesOrderLines',
                    "idSrc": "id",
                    fields: [
                        {
                            "label": "TCH Part No :",
                            "name": "partNo"
                        },
                        {
                            "label": "Customer Part No :",
                            "name": "customerPartNo"
                        },
                        {
                            "label": "Quantity:",
                            "name": "orderQuantity"
                        },
                        {
                            "label": "Request Date:",
                            "name": "requestDate",
                            "type": "datetime"
    
                        },
                        {
                            "label": "Description :",
                            "name": "description"
                        },
    
                        {
                            "label": "Sell Price:",
                            "name": "unitPrice"
                        },
                        {
                            "label": "Amount:",
                            "name": "amount"
                        }
    
                    ],
                    formOptions: {
                        inline: {
                            onBlur: 'submit'
                        }
                    }
                });
    
                orderLineTable = $('#SalesOrderLines').DataTable({
                    dom: "Bfrtip",
                    ajax: '/api/SalesOrderLines/' + $("#OrderNo").val() + '/orderNo',
                    columns: [
                        {
                            data: null,
                            defaultContent: '',
                            className: 'select-checkbox',
                            orderable: false
                        },
                        {
                            "data": "partNo"
                        },
                        {
                            "data": "customerPartNo"
                        },
                        {
                            "data": "orderQuantity"
                        },
                        {
                            "data": "requestDate"
                        },
                        {
                            "data": "description"
                        },
    
                        {
                            "data": "unitPrice"
                        },
                        {
                            "data": "amount"
                        }
                    ],
                    order: [1, 'asc'],
                    keys: {
                        columns: ':not(:first-child)',
                        keys: [9],
                        editor: editor,
                        editOnFocus: true
                    },
                    select: {
                        style: 'os',
                        selector: 'td:first-child'
                    },
    
                    buttons: [
                        // { extend: "create", editor: editor },
                        { extend: "edit", editor: editor },
                        { extend: "remove", editor: editor }
                    ]
    
                });
    

    Thanks.

    Best regards,
    Tom

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I just noticed you're not setting the value of the field - you can use field().set() to do that. This thread here should helpful, as it's demonstrating different ways to set the field.

    Colin

  • tomleongtomleong Posts: 13Questions: 5Answers: 0

    Dear Colin,

    I finally fixed my problem as below.

    1 - Change the submit to 'allIfChanged'

     formOptions: {
                        inline: {
                            onBlur: 'submit',
                            submit: 'allIfChanged'
                        }
                    }
    

    2 - Use $.each to set the value into field amount.

    editor.on('preSubmit', function (e, o, action) {
                    if (action !== 'remove') {
                        var orderQuantity = this.field('orderQuantity');
                        var unitPrice = this.field('unitPrice'); 
                         
                        $.each( o.data, function ( key, value ) {
                        o.data[key].amount = parseFloat(orderQuantity.val()) * parseFloat(unitPrice.val());
                        } );
                        // If any error was reported, cancel the submission so it can be corrected
                        if (this.inError()) {
                            return false;
                        }
                    }
                });
    

    Thanks.

    Best regards,
    Tom

This discussion has been closed.