如何在数据表行中插入按钮?

问题描述 投票:0回答:1

我现在有代码来获取将用于填充数据表的对象列表,但是我想在每行中添加一个按钮。怎么做?

var hCols = [];
model = @Html.Raw(Json.Encode(ViewBag.TableData));
var columns = [];
data = JSON.parse(model);
columnNames = Object.keys(data[0]);
for (var i in columnNames) {
    columns.push({
        data: columnNames[i],
        title: columnNames[i]//capitalizeFirstLetter(columnNames[i])
    });
}
asp.net-mvc datatable
1个回答
0
投票

似乎在您的代码中,数据是通过来自服务器的viewbag传递的。我认为这样做的正确方法是,不是从脚本中渲染数据,而是从服务器(Razor语法)渲染数据,然后初始化数据表。

<html>
  <body>
    <table class="table">
      <thead>
        <tr>
          <th>Property</th>
          <th>Button</th>
        </tr>
      </thead>
      <tbody>
        <!--Server Side Rendering-->
        @foreach(var i in ViewBag.TableData){
          <tr>
            <td>@i.PropertyName</td>
            <td>
              <button href="#">@i.PropertyName</button>
            </td>
          </tr>
        }
      </tbody>
    </table>

    <script>
      // Include jquery and datatable library
      // Initialize datatable

      $(document).ready(function(){
        var table = $('.table').DataTable();
      });
    </script>
  </body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.