add a duplicate button in-table style

add a duplicate button in-table style

Mike-motiv8Mike-motiv8 Posts: 30Questions: 5Answers: 0
edited March 2019 in Free community support

I am struggling to add a Duplicate button as a in-table style.

the code i have so far is:

$('#songsTable').on("click", "Duplicate", function(e, dt, node, config){

  var data = table1.row$(this).parents('tr')).data();
          delete data.DT_RowId;
          editor2.create().set(data).submit();
});

the button is listed under the colums tag with:

{ targets: -1,
                data: null,
              //  defaultContent: '<a href="" class="Bin">Delete</a>'
              defaultContent: "<button>Duplicate</button>",
             },

Could somebody please point me in the right direction, or even provide me with a correction.

Thanks in advance

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765
    Answer ✓

    I'm guessing the problem is the click event isn't firing?

    The word Duplicate doesn't look like a valid jQuery selector in the click event: .on("click", "Duplicate". See if this example helps:
    http://live.datatables.net/xijecupo/1/edit

    Kevin

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @Mike-motiv8 ,

    Here's the inline version - using a combination of the first solution and Kevin's code.

    Cheers,

    Colin

  • Mike-motiv8Mike-motiv8 Posts: 30Questions: 5Answers: 0

    Thank you Kevin and Colin,
    Support here is amazing.

    I never thought of using a class attribute to fire the code. Which handy as i could use the 'a' attribute and use a small jpg. :smile:

  • Mike-motiv8Mike-motiv8 Posts: 30Questions: 5Answers: 0

    When i click duplicate, i see a edit modal box appear on screen for a short second.
    i have tried using. draw(false) but does not effect it.

    is this normal? As it is annoying on mobiles.

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    edited March 2019

    Hi @Mike-motiv8 ,

    Could you link to a test case, or your page, that's showing that issue, please, as it's not occuring on the links that I've posted.

    Cheers,

    Colin

  • Mike-motiv8Mike-motiv8 Posts: 30Questions: 5Answers: 0

    the database is here:
    https://www.motiv-8.co.uk/clients/clientRequests.php
    press a green button and you will see the modal pop up.

    Also I struggling to find information on custom values with duplicate record.
    I have a DB field called Tableno.
    When the duplicate (green) is pressed, I would like a number "1" passed to the Tableno field in the DB.
    I have tried, def: "1" and value: "1" without success within the column code.
    It does however populate the modal Tableno field, but is not recorded to the DB.

  • Mike-motiv8Mike-motiv8 Posts: 30Questions: 5Answers: 0

    I also can not get the:
    $editor->where( 'Tableno', 1 );
    to work, or even without the $editor

                        Field::inst( 'Tableno' )
                    $editor->where( 'Tableno', 1 );
    

    How do you use a where clause? Where everything in Tableno field = 1

  • allanallan Posts: 61,665Questions: 1Answers: 10,095 Site admin

    It does however populate the modal Tableno field, but is not recorded to the DB.

    Can you show me your PHP code then please?

    . How do you use a where clause?

    The code above is not valid PHP and should be throwing a syntax error.

    You want to add ->where( 'Tableno', 1 ) immediately before the ->process(...) method call.

    Allan

  • Mike-motiv8Mike-motiv8 Posts: 30Questions: 5Answers: 0

    hi Allan,
    yes i realised the where clause goes immediately before the process line.
    I thankfully found the answer on here, tucked within another thread.

    I will show the php later.

    I still can not fathom why I'm seeing a modal flash on screen when I duplicate records?

  • allanallan Posts: 61,665Questions: 1Answers: 10,095 Site admin

    Is that shown on your link above? I don't see a "Duplicate" button there.

    Allan

  • Mike-motiv8Mike-motiv8 Posts: 30Questions: 5Answers: 0
    edited March 2019

    The duplicate buttons are the green, orange and red buttons on each line of the main table. I have used in-table image duplicate buttons. which fill the three tables on the right

  • allanallan Posts: 61,665Questions: 1Answers: 10,095 Site admin

    You are using:

    editor2.create().set(data).submit();

    Which is fine, but create() will show the form by default which is why it flashes up. When the submit completes it is then hidden.

    If you want the form hidden create() has a show option:

    editor2.create(false).set(data).submit();
    

    Allan

  • Mike-motiv8Mike-motiv8 Posts: 30Questions: 5Answers: 0

    I look at that page too. But I missed it. Thank You again Alan.
    How do I apply for a free licence extension until Monday, once i'm back in my office? If this isn't possible - No worries.

  • Mike-motiv8Mike-motiv8 Posts: 30Questions: 5Answers: 0
    edited March 2019

    editor2.create(false).set(data).submit();
    Has broken my validation message for duplicate records, which is on my ajax php page.
    Field::inst( 'ID' )
    ->validator( 'Validate::unique', array(
    "required" => true,
    "message" => "The song has benn requested."
    ) ),
    Is there another way of popping up a modal to inform the user that the record already exists?

  • allanallan Posts: 61,665Questions: 1Answers: 10,095 Site admin

    Listen for submitComplete and check for error messages in the returned data. If there is, then show the error message. The API was designed that if you are hiding the Editor form, then it would expect you to show the error messages if there are any.

    Allan

This discussion has been closed.