首页 > 解决方案 > 数据表在每一行添加按钮调用控制器功能

问题描述

我正在尝试在数据表的每一行上添加一个“编辑”和“删除”按钮。当我单击“编辑”时,它会调用一个控制器函数,并且需要将该行的 id 作为参数传递。我做对了有问题。有什么建议么?

我的控制器

`

def usuarioUpdate():
       return dict(formUsuarioUpdate=crud.update(db.Users,request.args(0)))`

我的观点

    <script>
        var tabla;
    $(document).ready(function(){
           tabla=  $('#tablaGenerica').DataTable({
                    "data":  {{=formListar}},
                    "scrollX": false,
                     "dom": 'lrtip',
                     "searching": true,
                     "sRowSelect": 'single',
                     "language": {
                     "url": "{{=URL('static','js/tradutorTable.json')}}",
                    },
                   "columns": [
                                  {
                                     "class":"details-control",
                                     "orderable":false,
                                     "data":null,
                                     "defaultContent": ""
                                  },
                                  { data: 'users.first_name' },
                                  { data: 'users.last_name' },
                                  { data: 'users.email' },
                                  { data: 'users.username' },
                                  {
                                     "orderable":false,
                                     "data":null,
                                     "defaultContent": "<div class='btn-group btn-group-justified JpositionA'><a class='btn btn-success Jview btn-xs' href='{{=URL('Herramientas','usuarioUpdate',args=["users.id"])}}'><span class='glyphicon glyphicon-pencil'></span></a><a class='btn btn-warning Jview btn-xs' href=><span class='glyphicon glyphicon-remove'></span></a></div>",
                                  },
                              ]
                });
    </script>

<table id="tablaGenerica" class="tablaC table-striped hover cell-border" cellspacing="0" width="100%" >
<thead>
    <tr>
        <th></th>
        <th>Nombre</th>
        <th>Apellido</th>
        <th>Correo Electrónico</th>
        <th>Nombre de Usuario</th>
        <th></th>
    </tr>
</thead>
</table>

标签: datatablesweb2py

解决方案


作为您的代码,我建议您使用渲染功能来实现您的目标

 "columns": [
                              {
                                 "class":"details-control",
                                 "orderable":false,
                                 "data":null,
                                 "defaultContent": ""
                              },
                              { data: 'users.first_name' },
                              { data: 'users.last_name' },
                              { data: 'users.email' },
                              { data: 'users.username' },
                              {
                                 "orderable":false,
                                 "data":null,
                                 "render": function(data,type,row,meta){
                                         return "<div class='btn-group btn-group-justified JpositionA'><a class='btn btn-success Jview btn-xs' href='{{=URL('Herramientas','usuarioUpdate',args=["+row.users.id+"])}}'><span class='glyphicon glyphicon-pencil'></span></a><a class='btn btn-warning Jview btn-xs' href=><span class='glyphicon glyphicon-remove'></span></a></div>"
                                 },
                              },
                          ]

关于渲染细节你可以访问 api https://datatables.net/reference/option/columns.render


推荐阅读