skip to Main Content

Using the source code from GitHub https://github.com/derekeder/csv-to-html-table/tree/master?tab=readme-ov-file#readme to parse a CSV to HTML nicely searchable formatted column.

Off the shelf works splendidly, thank you Derek.

Edited to my own source and adapting to suit with additional columns to hyperlink.

Can’t seem to edit the hyperlink format_link so that my hyperlinks go to the page in a new tab I want them to. The original source data has the fields configured as full http://url format so there was no need for additional formatting within the Script. Me on the other hand my CSV has only an IP address as a number eg 192.168.0.10.

This is the original code, I have added the //Comment to help me talk myself through changes and additions I am making for my own personalisation

function format_link(link) {
            if (link)
                return "<a href='" + link + "' target='_blank'>" + link +  "</a>"; //Add http wrapper and port to accommodate our requirements.
            else return "";

I have tried all sorts of variations of adding " or ‘ into the return string, but I either break it entirely or get no different results.

At present the original code without editing gives a link in a New Tab of http://localhost/sourcefolder/192.168.0.10 This obvs is not correct as there is no IP address or reference in the source folder.

What I want it to do is link the text contained in the specified columns, in my case 8 and 9 set as an array and return when clicked a new tab with the following example url:
http://192.168.0.10:8080

So content from csv file is 192.168.0.10 format_link needs to convert it to http://192.168.0.10:8080 and land it in a new Tabs as it does at present. I am thinking a synatx formatting issue?

I feel like I am so close and want to get this right before moving on to adding in the ping code testing that I am planning on using this all for. Based on this video I found. https://www.youtube.com/watch?v=ivB1SpOy4Qg. I have already adapted that to include a secondary IP column as well as making and then I can work on graphics and prettifying and then the database prettiness of it all comes later.

2

Answers


  1. Chosen as BEST ANSWER

    The ending closer } was in the code just not copied to this question.

    In the end I discovered other code that I had used in another PHP related query and used that to form the below alternative code which answers my question adding in the Href functionality desired.

    function format_link(link) {
            if (link)
               return "<a target='_blank' href="http://" + link + ":8080">" + link + "</a>";
            else return "";
        }
    

    While I would agree and appreciated Godot response, since this is my early foray into this project and the above modification directly solved my issue.

    I will keep Godot response on file and consider changing it in the future.

    For the moment, code edited, linked and added to works just fine.


  2. Your function is incomplete. It is missing the end }

    Here is a more modern and simpler version using template literals and a ternary

    const format_link = (link) => link ? `<a href="http://${link}:8080" target="_blank">${link}</a>` : '':
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search