Adding a link in bootstrap-table
Asked Answered
L

3

7

I want to add a link to a column in bootstrap-table. How to do this?

Lickerish answered 7/1, 2015 at 12:7 Comment(1)
These may be of interest too, as they both answer this as well as show wider use cases, such as on cell or row click: github.com/wenzhixin/bootstrap-table/issues/1380, github.com/wenzhixin/bootstrap-table/issues/877Ezarras
J
12

In your HTML table:

<th data-field="snum" data-formatter="LinkFormatter">Computer</th>

in your javascript:

function LinkFormatter(value, row, index) {
  return "<a href='"+row.url+"'>"+value+"</a>";
}
Jackstay answered 4/6, 2015 at 18:0 Comment(0)
L
4

I found the mechanism for this using the 'formatter'object. Below is an example formatter.

function identifierFormatter(value, row, index) {
    return [
            '<a class="like" href="javascript:void(0)" title="Like">',
                value,
            '</a>'].join('');
}

Basically to use this, it has to be added as an HTML data attribute to the Table Header.

<th data-field="identifier" data-align="right" data-sortable="true"  data-formatter="identifierFormatter">Identifier</th>
Lickerish answered 7/1, 2015 at 12:17 Comment(2)
Can you please show me an example on how you did this, I need the same exact thing. How did you incorporate this into your code?Timotheus
@CesarBielich improved the answerLickerish
S
0

html

<table id="table_exemple" data-toggle="table" data-pagination="true" >
    <thead>
        <tr>
            <th data-field="id">Id</th>
            <th data-field="name">Nome</th>
            <th data-field="action" data-formatter="ActionFormatter">Details</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

javascript

var bootstrap_table = $('#table_exemple');

function AddRow(obj){
    bootstrap_table.bootstrapTable('insertRow', {
        index: 0,
        row: {
           id: obj.id,
           name: obj.name,
           action: obj.id
        }
    });
}

function ActionFormatter(value) {
    return '<a href="javascript:void(0)" onclick="Details('+ value +')">Details</a>';
}

function Details(id){
    ...
}
Speller answered 13/9, 2017 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.