Displaying text data as a URL instead

Displaying text data as a URL instead

mattheanmatthean Posts: 10Questions: 3Answers: 0

Lets say I have data field covering a record, ie id, title, date, description, etc.. I am trying to display that data, but I also want the ID number to be a URL so a user can click on the ID number and go to the record itself. I feel like I went through the manual enough and didn't really see the option to do it. Is it actually possible?

Answers

  • kthorngrenkthorngren Posts: 21,337Questions: 26Answers: 4,954

    Use columns.render to display a URL containing the ID number. See the forth example in the docs. You may just want to generate the URL for the display operation. Similar to the fifth example. See the Orthogonal data docs for more details.

    Kevin

  • mattheanmatthean Posts: 10Questions: 3Answers: 0

    I went and modified the code. I'm getting no errors with the code and yet all I get is just the ID displayed.

    {
        "data": "ID",
        render: function (data) {
            var s = "<a href=\"results.aspx?\">" + data + "</a>";
            return s;
        }
    },
    
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin

    That looks like it should render a link tag correctly. Can you link to your page so I can take a look at it please?

    Thanks,
    Allan

  • mattheanmatthean Posts: 10Questions: 3Answers: 0

    It is being done via an internal site for a client, so there is no way to link it. Here's the complete column definition.

    columns: [
                    {
                        data: "ID",
                        render: function (data) {
                            data = "<a href=\"results.aspx?\"> + ID + </a>"
                            return data;
                        }
                    },                    
                    { "data": "Assignee" },
                    { "data": "Status" },
                    { "date": "CreateDate" },
                    { "date": "CDate" },
                    { "data": "HLQ" },
                    { "data": "HLQDevCenter" },
                    { "data": "RequestType" },
                    { "data": "Group" },
                    { "data": "Requestor" },
                    { "data": "Title" },
                    { "data": "Description" }
                    ]
                    columnDefs: [{
                        "targets": 3,
                        "type": "date"
                    }]
    
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin

    data = "<a href=\"results.aspx?\"> + ID + </a>"

    ID is undefined there. Use:

     data = "<a href=\"results.aspx?\"> + data + </a>"
    

    since the value from the ID parameter will be passed in to the rendering function as the data parameter.

    Allan

  • kthorngrenkthorngren Posts: 21,337Questions: 26Answers: 4,954

    I copied your code snippet into this test case and it seems to be working:
    https://live.datatables.net/dodobuge/35/edit

    Although the link doesn't work, the name is being displayed as clickable a URL. Can you update the test case to show the issue you are having?

    yet all I get is just the ID displayed.

    Are you expecting something more than the ID to be displayed? If so what do you expect to see?

    Kevin

  • mattheanmatthean Posts: 10Questions: 3Answers: 0

    I changed ID in the URL to data, and I still just get the plain text ID number. I do an inspect and all I see is the ID in text.

    I need a link with the ID so someone can click on it and have it go to a page where I can display just that record.

  • kthorngrenkthorngren Posts: 21,337Questions: 26Answers: 4,954

    I took your code snippet and showed that it works in the above test case:
    https://live.datatables.net/dodobuge/35/edit

    Maybe the change to data is being loaded an you need to clear the browser's cache. Place a break point in the colums.render function to see what is happening.

    To help debug we will need to see a page that replicates the issue. Can you post a link to a test case that shows the issue?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

Sign In or Register to comment.