Help with download_link

Help with download_link

MontereyDeanMontereyDean Posts: 13Questions: 1Answers: 0

Hello! I'm using the function to create a link from the data source.

This is the code from the DataTables columns.render page:

  "columnDefs": [ {
    "targets": 0,
    "data": "download_link",
    "render": function ( data, type, full, meta ) {
      return '<a href="'+data+'">Download</a>';
    }
  } ]

And this is my code:

"columnDefs": [ {
        "targets": 3,
        "data": "download_link",
        "render": function ( data, type, full, meta ) {
            return '<a href="http://www.mcwd.org/docs/resolutions/'+data+'" target="_blank">Download</a>';
            }
     } ]

It isn't working. the "+data+" becomes "undefined" in a browser.

The URL to the page is http://www.mcwd.org/about_resolutions_alt2.html

Replies

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26

    Hi

    After looking at the data in your table which looks like this for a row.

            ["2016-37", "2016-06-06", "Approving a Professional Services Agreement with Monterey Bay Technologies Inc. to Provide Information Technology Support Services to the District", "Resolution No. 2016-37.pdf"],
    
    

    It seems you are columns.data as an object for data when your data is in array format. You don't need to specify columns.data at all because its automatically set to the column index which in this case is 3.

    Thanks

    Tom

  • MontereyDeanMontereyDean Posts: 13Questions: 1Answers: 0
    edited June 2016

    Thanks. Yes, if I remove the code I showed above, then "Resolution No. 2016-37.pdf" appears in the cell for column 3. But how do I make "Resolution No. 2016-37.pdf" a hyperlink if not using columns.render? Sorry if I'm being dense. I just don't get it.

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26
    edited June 2016

    So download_link doesn't exist anywhere in your data as you are using a 2D array.

    When you are setting data to download_link it will be undefined as your table data is in a 2D array format not an object. This means the data for the column will automatically be applied but you cannot set the data by calling the object's name, only the index of the data inside the array.

    Instead of setting data to be an undefined object, set it to the column index of the data you want.

    Alternatively if you want to call an object for your data, change the data from being a 2D array to be an array of objects, you can then set the data you want to include in the link like you already are above.

    e.g.

    ["2016-37", "2016-06-06", "Approving a Professional Services Agreement with Monterey Bay Technologies Inc. to Provide Information Technology Support Services to the District", "Resolution No. 2016-37.pdf"],
    

    Would become

    ["resolution_no": "2016-37", "date": "2016-06-06", "title": "Approving a Professional Services Agreement with Monterey Bay Technologies Inc. to Provide Information Technology Support Services to the District",  "download_link": "Resolution No. 2016-37.pdf"],
    

    See data for more information.

    Thanks

    Tom

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26
    edited June 2016

    Just to clarify you can have the same code as before but just remove the data: property.

    e.g

    "columnDefs": [ {
            "targets": 3,
            "render": function ( data, type, full, meta ) {
                return '<a href="http://www.mcwd.org/docs/resolutions/'+data+'" target="_blank">Download</a>';
                }
         } ]
    
  • MontereyDeanMontereyDean Posts: 13Questions: 1Answers: 0

    Thank you, Tom!

    My code above was in error. I actually used the column name, "res_img," and I also tried "row[3]." But ... just removing the "data" property did the trick!

    Again, thank you, thank you, thank you.

This discussion has been closed.