datatables - 数据表在每一行添加按钮调用控制器功能
问题描述
我正在尝试在数据表的每一行上添加一个“编辑”和“删除”按钮。当我单击“编辑”时,它会调用一个控制器函数,并且需要将该行的 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>
解决方案
作为您的代码,我建议您使用渲染功能来实现您的目标
"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
推荐阅读
- python - 在 Python 中建立 HTTPS 连接时出现 HTTP 400 错误请求?
- symfony4 - 如何在 Doctrine 查询生成器中使用函数
- python - 将一个巨大的列表拆分为更小的列表并将项目附加到这些子列表中
- java - 如果用户输入了地址,则仅显示地址格式信息
- html - Carousel Bootstrap 不能用作幻灯片
- java - 将 Java Spark Sql 连接到 Mysql
- amazon-web-services - 你能指导我有关专用 AWS 的信息吗?
- python - 让 dev_appserver.py 监听变化
- python-3.x - python中列表理解输出的问题
- ubuntu - udev 规则问题 - 在 ubuntu 20.04 上安装 STMicroelectronics STlink 工具