Bootstrap Table URL 的格式化程序
Bootstrap Table Formatter for URL
Javascript:
function LinkFormatter(value, row, index) {
return "<a href='"+row.url+"'>"+value+"</a>";
}
HTML:
<th data-field="snum" data-sortable="true" data-formatter="LinkFormatter" >LINK</th>
<th data-sortable="true">DATA</th>
JSON:
{
data: [
[
"https://www.whosebug.com",
"Whosebug"
]
]
}
对于这个组合,我只在 table 的第一列中得到一个未定义的条目,并且还链接到 /undefined。然而,我只想要一列显示 Whosebug 并且是 URL 到 Whosebug。
我错过了什么?
您将不得不更改您的 JSON。
应该是这样的:
[
{
"url": "https://www.whosebug.com",
"nice_name": "Whosebug"
},
{
"url": "https://www.facebook.com",
"nice_name": "Facebook"
}
];
var data = [{
"url": "https://www.whosebug.com",
"nice_name": "Whosebug"
}, {
"url": "https://www.facebook.com",
"nice_name": "Facebook"
}];
function linkFormatter(value, row) {
return "<a href='" + row.url + "'>" + row.nice_name + "</a>";
}
$(function() {
$('#table').bootstrapTable({
data: data
});
});
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://unpkg.com/bootstrap-table@1.14.2/dist/bootstrap-table.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.14.2/dist/bootstrap-table.min.css">
<table data-toggle="#table" id="table">
<thead>
<tr>
<th data-field="url" data-formatter="linkFormatter" data-sortable="true">Link</th>
</tr>
</thead>
</table>
Example on bootstrap-table site 其中通过数据字段初始化 table 时需要相同的 JSON 格式。示例:
<table id="table"
data-toggle="table"
data-height="460"
data-url="../json/data1.json">
<thead>
Javascript:
function LinkFormatter(value, row, index) {
return "<a href='"+row.url+"'>"+value+"</a>";
}
HTML:
<th data-field="snum" data-sortable="true" data-formatter="LinkFormatter" >LINK</th>
<th data-sortable="true">DATA</th>
JSON:
{
data: [
[
"https://www.whosebug.com",
"Whosebug"
]
]
}
对于这个组合,我只在 table 的第一列中得到一个未定义的条目,并且还链接到 /undefined。然而,我只想要一列显示 Whosebug 并且是 URL 到 Whosebug。
我错过了什么?
您将不得不更改您的 JSON。
应该是这样的:
[
{
"url": "https://www.whosebug.com",
"nice_name": "Whosebug"
},
{
"url": "https://www.facebook.com",
"nice_name": "Facebook"
}
];
var data = [{
"url": "https://www.whosebug.com",
"nice_name": "Whosebug"
}, {
"url": "https://www.facebook.com",
"nice_name": "Facebook"
}];
function linkFormatter(value, row) {
return "<a href='" + row.url + "'>" + row.nice_name + "</a>";
}
$(function() {
$('#table').bootstrapTable({
data: data
});
});
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://unpkg.com/bootstrap-table@1.14.2/dist/bootstrap-table.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.14.2/dist/bootstrap-table.min.css">
<table data-toggle="#table" id="table">
<thead>
<tr>
<th data-field="url" data-formatter="linkFormatter" data-sortable="true">Link</th>
</tr>
</thead>
</table>
Example on bootstrap-table site 其中通过数据字段初始化 table 时需要相同的 JSON 格式。示例:
<table id="table"
data-toggle="table"
data-height="460"
data-url="../json/data1.json">
<thead>