Tabulator: format type link to open url in a new tab.

Created on 5 May 2018  路  1Comment  路  Source: olifolkerd/tabulator

Hi Oli,

I have a question, how can I modify the link format type to allow that when an user clicks on a link field, the page would be open in a new tab rather that in the same tab. I would rather prefer not to use the html format to create the tag

Most helpful comment

Hey @cemmons

At the moment you cant do that with the existing link formatter, but i think that is a great idea so i will add it to the roadmap for the next release.

in the mean time here is a customised version of the link formatter you could use to do just that:

//clickable anchor tag
var linkFormatter = function(cell, formatterParams){
    var value = this.sanitizeHTML(cell.getValue()),
    urlPrefix = formatterParams.urlPrefix || "",
    label = this.emptyToSpace(value),
    data;

    if(formatterParams.labelField){
        data = cell.getData();
        label = data[formatterParams.labelField];
    }

    if(formatterParams.label){
        switch(typeof formatterParams.label){
            case "string":
            label = formatterParams.label;
            break;

            case "function":
            label = formatterParams.label(cell);
            break;
        }
    }

    if(formatterParams.urlField){
        data = cell.getData();
        value = data[formatterParams.urlField];
    }

    if(formatterParams.url){
        switch(typeof formatterParams.url){
            case "string":
            value = formatterParams.url;
            break;

            case "function":
            value = formatterParams.url(cell);
            break;
        }
    }

    return "<a href='" + urlPrefix + value + "' target='_blank'>" + label + "</a>";
},

I hope that helps,

Cheers

Oli :)

>All comments

Hey @cemmons

At the moment you cant do that with the existing link formatter, but i think that is a great idea so i will add it to the roadmap for the next release.

in the mean time here is a customised version of the link formatter you could use to do just that:

//clickable anchor tag
var linkFormatter = function(cell, formatterParams){
    var value = this.sanitizeHTML(cell.getValue()),
    urlPrefix = formatterParams.urlPrefix || "",
    label = this.emptyToSpace(value),
    data;

    if(formatterParams.labelField){
        data = cell.getData();
        label = data[formatterParams.labelField];
    }

    if(formatterParams.label){
        switch(typeof formatterParams.label){
            case "string":
            label = formatterParams.label;
            break;

            case "function":
            label = formatterParams.label(cell);
            break;
        }
    }

    if(formatterParams.urlField){
        data = cell.getData();
        value = data[formatterParams.urlField];
    }

    if(formatterParams.url){
        switch(typeof formatterParams.url){
            case "string":
            value = formatterParams.url;
            break;

            case "function":
            value = formatterParams.url(cell);
            break;
        }
    }

    return "<a href='" + urlPrefix + value + "' target='_blank'>" + label + "</a>";
},

I hope that helps,

Cheers

Oli :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sphynx79 picture sphynx79  路  3Comments

tomvanlier picture tomvanlier  路  3Comments

alainpannetier picture alainpannetier  路  3Comments

KES777 picture KES777  路  3Comments

tomheaps picture tomheaps  路  3Comments