Can I use Editor to add a master/detail combination?

Can I use Editor to add a master/detail combination?

pzh20pzh20 Posts: 66Questions: 15Answers: 0

I wonder if I can add an order master record and one or more order details records associated with the master record.

I can see examples of child records, but not Master/Details

Regards
Pete

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Hi Pete,

    So I understand correctly, you want to be able to have a field in your details record whereby you can select a master record? Sounds like you want to do a left join and assign a value if so. Could you show me your database schema?

    Allan

  • pzh20pzh20 Posts: 66Questions: 15Answers: 0

    Not really, I want to be able to add a new order then as many order details records as are needed. So the order master record will have the order date, supplier etc and the order details will have the line items of the order with the price etc.

    I have a Orders table as follows
    OrderID
    SupplierID
    Date
    OrderBy
    PONum
    BuID

    etc

    OrderDetails table
    ODID
    OrderID
    DepartmentID
    ODDesc
    ODAmount

    etc

    with the OrderID being a foreign key of the orders table

    Regards
    Pete

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Thanks for the clarification.

    That is quite possible - how you might find it easiest to implement, at least initially is as two separate tables. The top table would be for the orders and when you select a row, the bottom table would display all of the order details. Each table can then be manipulated in the normal fashion.

    A more complex implementation would be to show the master / child methods that DataTables has available to show the order details inline with the master table. That would require a bit more custom code, making use of both the Editor and DataTables APIs.

    Possibly a good one for a blog post that...

    Allan

  • pzh20pzh20 Posts: 66Questions: 15Answers: 0

    SOrry for not getting back before.

    SO the first option I would have a table of orders, then somehow click an order which would open another table to display/add/remove the order details?

    If I created a new order, how could I then create a details record?

    Is there any examples of this type of approach?

    Regards
    Pete

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    then somehow click an order which would open another table to display/add/remove the order details?

    Yes, you could simply use ajax.url().load() on the second table to tell it to load new information.

    There isn't an example of that as such, but you would just do something like:

    $('#tableOne').on( 'click', 'tr', function () {
      var rowData = tableOne.row( this ).data();
    
      tableTwo.ajax.url( '/loadTableTwoData?id='+rowData.id ).load();
    } );
    

    Obviously the variable names, urls, table IDs etc would need to be updated, but that's the basic idea.

    Allan

  • bjlapbjlap Posts: 29Questions: 11Answers: 1

    I am using a master detail implementation as above. Works fine, except for one thing, when adding new detail records, I don't know how to add the master primary key value as a foreign key value to the new detail record. (updating records is no problem). I am trying to use the initCreate event but I don't now how to handle the data object. Does the data object contain the new row? If yes, how can I add a value? Something like the below example? (tried but doesn't work, I am missing something. should I use data.row or something?)

    editorActivities.on('initCreate', function (e, json, data) {
    data.ProjectId = projectId;
    }

    Thanks for your help,
    Bert-Jan

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    I would suggest having the value as a hidden field in the form (hidden) which you set the default value for (field().def()).

    Allan

This discussion has been closed.