How to make multiple targets that link to different urls?

How to make multiple targets that link to different urls?

CorbDJCorbDJ Posts: 6Questions: 2Answers: 2

I have a table and I need 3 of the columns to be linked to 3 different URLs. I have tried some variations but I keep getting an error:

_DataTables warning: table id=table_data - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3_

This is my code:

<script>

$(document).ready(function() {
$('#table_data_0').dataTable({
    "order": [[3, "desc"]],
    "columnDefs": [{
        "targets": 1,
        "data": "download_link",
        "render": function (data, type, row, meta) {
            let url = "{{ url_for('customer_page', data='DATA') }}".replace('DATA', data);
            return '<a href="' + url + '">'+data+'</a>';}},
        {
        "targets": 2,
        "data": "download_link",
        "render": function (data, type, row, meta) {
            let url = "{{ url_for('confirmation_page', data='DATA') }}".replace('DATA', data);
            return '<a href="' + url + '">'+data+'</a>';}},
        {
        "targets": 7,
        "data": "download_link",
        "render": function (data, type, row, meta) {
            let url = "{{ url_for('email_page', data='DATA') }}".replace('DATA', data);
            return '<a href="' + url + '">'+data+'</a>';}}]
})});

</script>

I appreciate the guidance! Thank you!

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,325Questions: 26Answers: 4,949
    Answer ✓

    Your code works, see this test case:
    http://live.datatables.net/yicupicu/1/edit

    I commented the line "data": "download_link", to use the data in the column. Its hard to say but you may or may not need to have this line.

    Cannot reinitialise DataTable

    Did you follow the steps in the link?
    It covers what the error is and why your are getting it. Plus has steps to help fix it. Basically you are trying to initialize the Datatable more than one. Note the error you pasted has this table id=table_data while the code example has this ID #table_data_0. Maybe a copy/paste error but they are referring to different table ID's.

    In order to help with this error we will need to see your page. Please post a link to your page or a test case replicating the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • CorbDJCorbDJ Posts: 6Questions: 2Answers: 2

    I did follow the link, but I couldn't see anything in my code that made me think it was wrong. I honestly thought I had a formatting error and just couldn't see it.

    Apparently, I am experiencing technical difficulties. Please stand by... (HaHa)

  • CorbDJCorbDJ Posts: 6Questions: 2Answers: 2
    edited April 2019

    I found the error in the code. I was referencing the wrong table as a result of troubleshooting.

    $('#table_data_0').dataTable({
    

    Should have been:

    $('#table_data').dataTable({
    

    Sorry. I think it is time to walk away from the desk for a while.

  • CorbDJCorbDJ Posts: 6Questions: 2Answers: 2
    Answer ✓

    I needed to remove/comment out the:

    "data": "download_link",
    

    That made all the difference! I apologize. Its been a long day.

This discussion has been closed.