How would you add multiple URLS to a cell

How would you add multiple URLS to a cell

newfullusernewfulluser Posts: 7Questions: 3Answers: 0
edited September 2020 in Free community support

Hello,

How would you add multiple urls to a cell?

I'm currently doing just one URL like this:


{
"data": "links",
"orderable": "true",
"className": "none",
render: function (data, type, row, meta) {
return '<a target="_blank" rel="nofollow" href=" ' + row.link + ' "> ' + row.name + ' Website</a>';
}
},

But this only works for one URL per cell, and if the URL is empty the "Website" shows up anyway. I need a flexible way to just add urls in mysql and they are displayed in one cell just like this:

Social Media
Twitter Facebook YouTube

In each row there will be different urls and names, some rows might have Instagram and YouTube, or only Twitter and Facebook. That's why I can't make a cell for each URL, there would be a ton of empty cells.

Please comment below if you know how to archieve this, I think I'm looking at the problem the wrong way!

Answers

  • kthorngrenkthorngren Posts: 21,307Questions: 26Answers: 4,947
    edited September 2020

    All you are doing in columns.render is building an HTML string that will be displayed in the cell. You can build anything you want as long as its valid HTML. You can use if statements inside the function to add the appropriate links. Example pseudo code:

    render: function (data, type, row, meta) {
      html = '';
      if ( twitter ) {
        html += '<a target="_blank" rel="nofollow" href=" ' + row.link + ' "> ' + row.name + ' Twitter</a>';
      }
      if ( facebook ) {
        html += '<a target="_blank" rel="nofollow" href=" ' + row.link + ' "> ' + row.name + ' Facebook</a>';
      }
      if ( youtube ) {
        html += '<a target="_blank" rel="nofollow" href=" ' + row.link + ' "> ' + row.name + ' YouTube</a>';
      }return html;
    }
    

    Fill in the if statements appropriately and the row.links.

    Kevin

  • newfullusernewfulluser Posts: 7Questions: 3Answers: 0
    edited September 2020

    Thank you for taking the time to reply kthorngren, really appreciate it.

    I'm not sure it can work, if row.link is https://twitter.com, https://facebook.com how do you make it tell between the commas and add the links separately? What if instead of facebook.com it's company1.com, company2.com

    If there was a way to just render html code into the cell, I would customize each cell in the database and then it would display in the right order with the right names.. But I tested it and datatables does not like raw HTML and it didn't work at all.

  • kthorngrenkthorngren Posts: 21,307Questions: 26Answers: 4,947
    edited September 2020

    This example renders two buttons in each cell. They aren't links but maybe it will help you:
    http://live.datatables.net/qemodapi/1/edit

    But I tested it and datatables does not like raw HTML and it didn't work at all.

    Maybe you can update my example to show what you are trying to do.

    Kevin

This discussion has been closed.