render link to absolute path

render link to absolute path

crush123crush123 Posts: 417Questions: 126Answers: 18

i have a page with a datatable which has a column which renders the data as a link to a file in a (documents) folder, like so...

return '<a href="/documents/'+data+'">'+data+'</a>' ;

if i want to link to an external path I tried just omitting the documents path

return '<a href="'+data+'">'+data+'</a>' ;

but this gives me an error, the generated url looks like this, <a href="plugins'+data+'">'+data+'</a>

presumably because the page containing my datatable is a plugin page (in a folder called plugins), one level below the 'parent' page.

is there a way to generate the absolute url ?

Answers

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    but this gives me an error,

    What is the error?

    the generated url looks like this, <a href="plugins'+data+'">'+data+'</a>

    Not sure how return '<a href="'+data+'">'+data+'</a>' ; results in the above URL. Can you 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

    is there a way to generate the absolute url ?

    You should be able to generate any URL you want. Its just a matter of getting the return string to look the way you want. Datatables isn't controlling this.

    Kevin

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited February 2019

    the error is in my console

    Uncaught SyntaxError: Invalid or unexpected token

    the javascript reads as follows (line 6 below)

            { data: "tbldocuments.DocumentPath",
                render: function ( data, type, row ) {
                    if ( row.tbldocuments.DocumentPath.length >0) {
                    return '<a href="/documents/'+data+'">'+data+'</a>' ;
                    } else {
                    return '<a href="plugins"+row.tbldocuments.DocumentLink+'">'+row.tbldocuments.DocumentLink+'</a>';
                    }
                }
            },
    

    which is generated by the code below

            { data: "tbldocuments.DocumentPath",
                render: function ( data, type, row ) {
                    if ( row.tbldocuments.DocumentPath.length >0) {
                    return '<a href="/documents/'+data+'">'+data+'</a>' ;
                    } else {
                    return '<a href="'+row.tbldocuments.DocumentLink+'">'+row.tbldocuments.DocumentLink+'</a>';
                    }
                }
            },
    

    I will try and create a cut-down test page tomorrow

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Line 6 in your first code snippet has the syntax error. You need to add a single quote before the +row:

    return '<a href="plugins"+row

    should be:
    return '<a href="plugins"'+row

    Kevin

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    the first code snippet is the javascript generated when the page is loaded in the browser

    the second code snippet is what generates the javascript, and there is no syntax error in there

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Guess I'm not familiar with how the second code snippet is generating the first. Look forward to seeing this in your test page.

    Kevin

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    i have uploaded sample pages to a test area, (username and password to access are 'nfci'

    the 'parent page' is found at this url

    http://instagram.forthwebsolutions.com/admin/documents/documentsx.php

    the child plugin page is found here

    http://instagram.forthwebsolutions.com/admin/documents/plugins/document_resultsx.php

    the child page works perfectly fine in its own right, but when embedded in the parent page, the url generated by the javascript incorporates the subfolder name at the beginning of the path, (but only if it is left empty)

    so

    return '<a href="/documents/'+data+'">'+data+'</a>' ;
    

    works fine, in both the child page, and when used as a plugin to the parent page

    but

    return '<a href="'+data+'">'+data+'</a>' ;
    

    will incorporate the subfolder in the parent page, throwing an error, but is ok in the child page

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Sorry, I'm still unclear where return '<a href="'+data+'">'+data+'</a>' ; is coming from. I don't see it anywhere in your code.

    However on line 170 of your parent page there is this line:

    `return '<a href="plugins"+row.tbldocuments.DocumentLink+'">'+row.tbldocuments.DocumentLink+'</a>';`
    

    Which is missing the single quote I mentioned above. If you have something generating the Javascript for this page then you need to look at that code to determine why it is not generated correctly. This is outside the scope of Datatables.

    Kevin

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited February 2019

    if you look at the child page, you will see the following javascript generated (the page renders properly and the link works as it should)...

    { data: "tbldocuments.DocumentPath",
                render: function ( data, type, row ) {
                    if ( row.tbldocuments.DocumentPath.length >0) {
                    return '<a href="/documents/'+data+'">'+data+'</a>' ;
                    } else {
                    return '<a href="'+row.tbldocuments.DocumentLink+'">'+row.tbldocuments.DocumentLink+'</a>';
                    }
                }
            },
    

    if you look at the parent page, you will see this

            { data: "tbldocuments.DocumentPath",
                render: function ( data, type, row ) {
                    if ( row.tbldocuments.DocumentPath.length >0) {
                    return '<a href="/documents/'+data+'">'+data+'</a>' ;
                    } else {
                    return '<a href="plugins"+row.tbldocuments.DocumentLink+'">'+row.tbldocuments.DocumentLink+'</a>';
                    }
                }
            },
    

    there is no syntax error, but because the folder in which the child page resides is called 'plugins', this folder name is embedded in the link, which gives an apparent missing single quote

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    ...frustratingly,

    if I add a backslash, i get the site root (as expected) on both parent and child pages

    eg return '<a href="/'+data+'">'+data+'</a>' ;
    

    if i add a folder name, i get the folder name on both parent and child pages

    return '<a href="/folder/'+data+'">'+data+'</a>' ;
    

    the problem demonstrated only occurs if i leave the value blank

    return '<a href="'+data+'">'+data+'</a>' ;
    

    where the child page is fine, but the parent page gives the error

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    there is no syntax error, but because the folder in which the child page resides is called 'plugins', this folder name is embedded in the link, which gives an apparent missing single quote

    When loading the Parent page the browser's console shows this error:

    Uncaught SyntaxError: Invalid or unexpected token documentsx.php:170

    This is a Javascript error. The problem is that line 170 has improper Javascript syntax. The single quote is missing. This has nothing to do with Datatables. You will need to look at the code that is generating the Javascript to determine why it is not providing proper Javascript syntax when it is adding the folder plugins.

    Kevin

This discussion has been closed.