首页 > 解决方案 > 在 bootstrap-table 的 data-formatter 函数中显示 php 数据

问题描述

我在table.php文件中创建了一个引导表,并用这样的数据格式化程序定义了一个表标题

<th data-field='edit' data-formatter="editFormatter">Edit</th>

我为此创建了函数,

function editFormatter(value, row)
{
   return `<a href="<?= URL::to('xyz/label'); ?>"  ><i class="fa fa-pencil sg-edit-btn" ></i></a>`
}

但是当我在table.php页面上检查时,它显示的是这样的锚点,

<a href="<?= URL::to('xyz/label'); ?>

为什么php代码不能像这样转换,

<a href="https://localhost/app/public/xyz/label" class="btn sg-primary-btn">

有什么办法吗?提前致谢

标签: javascriptphphtmlbootstrap-table

解决方案


var data = [{
  "url": "https://www.stackoverflow.com",
  "nice_name": "Stackoverflow"
}, {
  "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>


推荐阅读