Setting The Value Of Hidden Field From Value In Separate Table

Setting The Value Of Hidden Field From Value In Separate Table

franks59franks59 Posts: 16Questions: 2Answers: 1

I have two DataTables and two Editors. One table is a parent and one contains child records. The child table is synchronized with the parent (thanks Crush123 and Alan) and everything works fine except when I want to create a new child record.

The underlying database has a foreign key restraint to be sure that a child is associated with a parent.

My problem is, when creating a new child, how do I populate the (hidden) foreign key in the child with the primary key of the selected parent?

I can, and do get the primary key with an on-click event in the parent, but I can't see how to set the child field to that variable. Nor do I see how to pass that back to the server so that I can assign it in PHP.

Frank

Replies

  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin

    Use the val() method to set the field value :-)

    Allan

  • franks59franks59 Posts: 16Questions: 2Answers: 1

    Thanks, but I think I wasn't clear enough.

    This applies only when Table B (child) TableTools button "New" is selected.
    At that point I need to automatically set the value of the child foreign key to that of the parent's primary key.

    I can get the value I need in this parent's function

        $('#parent_table tbody').on( 'click', 'tr', function () {
            // Grab the value of the parent's primary key
            var parent_key = this.id.substr(4);
    
            // Pass the parent's primary to server to retrieve assoc. child rows
            child_datatable.ajax.url('php/table.child.php?parent_key='+parent_key).load();
    
            }
         );
    

    But how do I access either the child datatable or child editor from here - the parent - in order to set the value in the child?

    Or is this the wrong place? Should I attach the code to an event in either the child datatable or Editor when "New" is selected?

    If so, the same problem arises - When I'm in table/editor B, how do I get a value from a specific column in the currently selected row in table A?

    Frank

  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin

    I presume you have access to the child Editor instance so you can use its API? If so, perhaps the best way would be to set the default value for that field in your function above - field().def() will let you set a default, and that will be used on create.

    Allan

  • franks59franks59 Posts: 16Questions: 2Answers: 1

    Thanks, Allan. That worked great!

    Frank

This discussion has been closed.