Copy unique id to another table with prior validation check if record already exists

Copy unique id to another table with prior validation check if record already exists

CapamaniaCapamania Posts: 233Questions: 81Answers: 5
edited July 2016 in Editor

Hi All,

I would like to copy a specific cell with a unique id to another table. And I would like to have a validation check if the record already exists. If it exists already, a modal should pop up (with a 'Record already exists' message) and the process should be aborted. It would be great to have an inline button in the row of the record that copies the specific id to the other table.
Is this posssible and how? Does anybody have an example? I'm using Editor and DataTable and serverside.

Many thanks

This question has an accepted answers - jump to answer

Answers

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

    I don't have an example of that as such I'm afraid, but it should be quite possible to do with the API - specifically row().data() to get the data for the row to copy, create() to create it for the second table and remove() to remove it from the first (all assuming that they are two different database tables and two different Editors?).

    Allan

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5
    edited July 2016

    Hi Allan, yes that's correct. I have 'two different database tables and two different Editors. Both with the same structure.

    Table 1 ('all') has all the records and should stay the same ('no remove'). Table 2 ('portfolio') should be a kind of watch list in which records of Table 1 can be added and removed. What works so far is removing records from Table 2. But I can't pass data from Table 1 to Table 2. Plus, since Table 1 should stay the same, I want to make sure there are no duplicates in Table 2. So it would be great to have a kind of a validation prior to sending the data to Table 2. And if it already exist an modal pops up and aborts the process.

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

    Table 2 ('portfolio') should be a kind of watch list in which records of Table 1 can be added and removed.

    So you don't actually want to remove the row from a different database table and into another one? You just want to show it in a different client-side table?

    If that's the case, particularly with server-side processing enabled, you would need to apply a condition to the first table to hide the row, and a connection to the second table to show it. Documentation on how to apply conditions to the PHP Editor code is available here. You'd probably need to pass information to the script to say what row(s) have been selected on the client-side to allow the filter to be correctly applied.

    Allan

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5
    edited July 2016

    Thanks Allan. I got it working like this:

    // Getting row data from table 1 when clicking on inline button of the row:

    var data = table_1.row().data();
    

    // Selecting the values of the columns (name & id) I was looking for:

    var getName = data[ 'name' ]; 
    var getID = data[ 'id' ];
    

    // Sending values to table 2:

    editor_table_2
    .create( {
        title: 'Add to Table 2',
        buttons: 'Add'
    } )
    .set( 'name', getName )
    .set( 'id', getID );
    

    What I haven't figured out yet is the abortion if a record (id) already exists in table 2. Any hint? Do I need to work with https://editor.datatables.net/reference/api/val() ? Many thanks.

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

    You'd probably need to use column().data() (or perhaps data() if the id isn't in a table column). That way you can get a list of ids that are in the second table and use indexOf to see if the id in question is in there or not.

    Allan

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5

    Perfect. That worked. Thanks a lot.

This discussion has been closed.