2017-02-22 65 views
1

想知道有人可以帮助我这个。csv html表与多个超链接

我使用这个例子CSV to HTML Table

我试图格式的追加列作为一个超链接,但老实说没有JS技能将不胜感激的任何援助。下面的例子显示了一列的代码。

<script> 

    //my custom function that creates a hyperlink 
    function format_link(link){ 
    if (link) 
     return "<a href='" + link + "' target='_blank'>" + link + "</a>"; 
    else 
     return ""; 
    } 

    //initializing the table 
    CsvToHtmlTable.init({ 
    csv_path: 'data/Health Clinics in Chicago.csv', 
    element: 'table-container', 
    allow_download: true, 
    csv_options: {separator: ',', delimiter: '"'}, 
    datatables_options: {"paging": false}, 
    custom_formatting: [[4, format_link]] //execute the function on the 4th column of every row 
    }); 
</script> 
+0

那究竟是什么问题?当你运行这段代码时你会得到一个错误吗? –

+0

代码是完美的。我只是要求协助修改它以在csv中将2列格式化为超链接,而不是像例子中的1那样。希望有所帮助。谢谢回复。 我的csv数据有两个超链接数据字段,我需要将结果表格格式化为这样。 –

+0

该文档说要使用数组的数组,所以它可能会是这样的:custom_formatting:[[4,format_link],[6,format_link]] - 我添加了一个额外的数组来格式化第6列中的链接 –

回答

0

为CSV到HTML表的文档指出:

如果你想要做自定义格式的一个或多个列,您可以 传中数组​​的数组包含该列的索引以及用于格式化的自定义函数 。您可以传入多个格式化程序 ,它们将按顺序执行。

在下面的例子中,我演示了多个链接的数组数组的样子。在我的例子中,第4列和第6列有链接。

<script> 

    //my custom function that creates a hyperlink 
    function format_link(link){ 
    if (link) 
     return "<a href='" + link + "' target='_blank'>" + link + "</a>"; 
    else 
     return ""; 
    } 

    //initializing the table 
    CsvToHtmlTable.init({ 
    csv_path: 'data/Health Clinics in Chicago.csv', 
    element: 'table-container', 
    allow_download: true, 
    csv_options: {separator: ',', delimiter: '"'}, 
    datatables_options: {"paging": false}, 
    custom_formatting: [[4, format_link], [6, format_link]] //execute the function on the 4th column of every row 
    }); 
</script>