Editor - serverside: Copy a value to another table.

Editor - serverside: Copy a value to another table.

JanNL_FRJanNL_FR Posts: 47Questions: 15Answers: 2

Is it possible to copy the value of an 'id' field in a selected row to another related table (with a button)?

Thanks in advance.
Jan.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    another related table

    Sorry its not clear to me what you are asking for. Is the table client side or is it a server side database table?

    Are you looking for something like this parent/child editing example?

    If you want to send the value of the ID field to the server you can create a [custom button] like this example. You can get the selected row with row().data() using the selector-modifier of {selected:: true}. See this example. Then use jQuery ajax() to send the value to the server. with the data object.

    If this doesn't then please provide more details of what you are trying to do.

    Kevin

  • JanNL_FRJanNL_FR Posts: 47Questions: 15Answers: 2

    Thanks Kevin,

    I'll try to explain.
    In the datatables_demo table we have Tiger Nixon with id = 1.
    Now I want to select the row, press the button that adds the 1 in another table on the server.
    If that works, I'd also like to add a session user_id to the same record.
    I am already successful in applying the records in a parent/child construction to link them to another table.

    Jan

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited December 2023 Answer ✓

    Here are the steps I would employ to send data from the selected row to the server:

    -1. Create a Custom button.
    -2. Use the following code to get the selected row:

    var data = dt.row( {selected: true} ).data();
    var id = dt.row( {selected: true} ).id();
    

    If using the rowId option the id will contain the row's id. Otherwise you will need to get the id from the data varaiable.

    -3. Send the id to the server using using the data object of jQujery ajax(). For example:

        $.ajax( {
            url: '/my_url',  // URL pointing to server script to insert new row
            data: {
                id: data.id  // or data: id depending on what is used in step 2
            },
            type: 'post',
            success: function ( data ) {
              // Follow up code 
            }
        } 
    

    The above code will reside in the custom button function.

    I'd also like to add a session user_id to the same record.

    Not sure where or what this is but add it to the data option of the ajax request.

    Kevin

  • JanNL_FRJanNL_FR Posts: 47Questions: 15Answers: 2

    After selecting a record, two values are now written to another table.
    This works well for a single record.
    Would it also be possible to do this with multiple selected records at the same time?

    buttons: [
        {
            text: 'Add to Portfolio',
            action: function () {
                if (selectedID) {
                    portfolioEditor.create({
                        title: 'Add to Portfolio',
                        buttons: 'Add to Portfolio'
                    })
                    .set('esg_portfolio.company_p_id', selectedID)
                    .set('esg_portfolio.member_id', user_id) 
                    .submit(function (json) {
                        // Check for duplicates
                        if (json.fieldErrors && 
                                                    json.fieldErrors['esg_portfolio.company_p_id']) {
                            alert('Duplicate record! This combination already exists 
                                                    in the portfolio.');
                        }
                    });
                        } else {
                           alert('Please select a row to add to your portfolio.');
                        }
               }
        },  
    
    

    Jan

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

    Yes, with create() you can create multiple rows at a time. Pass in the number of rows to create as the first argument to create(), then use the multi-row editing API to set the values for each of the fields per new row.

    Allan

Sign In or Register to comment.