On Create, populate a field with a value from a parent table

On Create, populate a field with a value from a parent table

Adrian ChallinorAdrian Challinor Posts: 21Questions: 8Answers: 0
edited October 2020 in Free community support

In my use case I have a table, lets call it "Master". Once a record has been selected from Master, the "New" button on the child table is available. On the Master table row I have a field called quantity. I also have a field called quantity on the child table row.

The desired UI should be that the child table row field Quantity should default to the Master table row field called quantity, but the user should be able to edit this. The normal action is that they wiull use the same quantity, but in about 5% of cases they will split the quantity over two rows in the child table.

I tried using a function on default, but this applies the default on form submission, so it only works if the user enters nothing. Its not a great UI, because the user needs to know that a blank field doesn't mean blank, it means the complete quantity.

My next try was to use an action: function() on the extend : "create" button. This does find and select the quantity field, but the action button then overrides the creation of the editor window. So now I can populate the field, but I can't actually edit or submit it.

I think what I need to do is precreate the row in the child table before offering to be edited, but I'm not sure how I do this.

I should say that is all in client side JavaScript.

This question has an accepted answers - jump to answer

Answers

  • Adrian ChallinorAdrian Challinor Posts: 21Questions: 8Answers: 0

    I DID IT (go me!). The "Master" table is called Voyage, and the child table called "Port". So the following code does what I need.

                portEditor.on('preOpen', function(e, mode, action) {
                    if (action == "create") {
                        var selected = voyageTable.row( { selected: true } );
                        var qty = selected.data().voyage.quantity;
                        portEditor.field("quantity").set(qty);
                    }
                });
    
  • LapointeLapointe Posts: 430Questions: 81Answers: 4
    Answer ✓

    hi @adrianChallinor

    Why not use .def, so if user do not wand to validate record, nothing is done

    portEditor.on('preOpen', function(e, mode, action) {
        if (action == "create") {
            var selected = voyageTable.row( { selected: true } );
            var qty = selected.data().voyage.quantity;
            portEditor.field("quantity").def(qty);
        }
    });
    
    

    Else to set value use .val

    portEditor.on('preOpen', function(e, mode, action) {
        if (action == "create") {
            var selected = voyageTable.row( { selected: true } );
            var qty = selected.data().voyage.quantity;
            portEditor.field("quantity").val(qty);
        }
    });
    
    
This discussion has been closed.