jquery datatable将数据属性绑定到显示的内容(jquery datatable binding data property to content that is displayed)

编程入门 行业动态 更新时间:2024-10-14 20:23:49
jquery datatable将数据属性绑定到显示的内容(jquery datatable binding data property to content that is displayed)

我安装了我的jQuery datatable插件,我已经像这样初始化它:

$('#datatable-responsive2').DataTable({ // data: data, // deferRender: true, "pageLength": 25, "bLengthChange": false, "processing": true, "serverSide": true, "filter": false, "orderMulti": false, "ajax": { "url": "/Administrator/LoadData/", "type": "POST", "datatype":"json" }, "columns": [ { "data": "FirstName", "name": "Lela", "autoWidth": true }, { "data": "Email", "name": "Email", "autoWidth": true }, { "data": "Active", "name": "Status", "autoWidth": true }, { "targets": -1, "data": "UserId", "defaultContent": "<button>Click!</button>", "autoWidth": true }, { "data": "FirstName", "name": "Full name", "autoWidth": true } ] });

请注意本专栏:

{ "targets": -1, "data": "UserId", "defaultContent": "<button>Click!</button>", "autoWidth": true }

我已经按照他们的文档介绍了如何在那里呈现HTML元素......但是我现在需要的是无法确定如何在生成数据表时为这个HTML元素设置一个特定属性。 。

你可以看到我已经将数据表的数据源设置为UserId,现在我希望每个按钮“Click”都有值,无论UserId的值是什么......

有人可以帮我吗?

PS所以我想在该列中输出一个HTML元素,其结构将是这样的:

<button values="whatever the value of userId is..?">Click me event</button>

I have my jQuery datatable plugin installed and I've initialized it like this:

$('#datatable-responsive2').DataTable({ // data: data, // deferRender: true, "pageLength": 25, "bLengthChange": false, "processing": true, "serverSide": true, "filter": false, "orderMulti": false, "ajax": { "url": "/Administrator/LoadData/", "type": "POST", "datatype":"json" }, "columns": [ { "data": "FirstName", "name": "Lela", "autoWidth": true }, { "data": "Email", "name": "Email", "autoWidth": true }, { "data": "Active", "name": "Status", "autoWidth": true }, { "targets": -1, "data": "UserId", "defaultContent": "<button>Click!</button>", "autoWidth": true }, { "data": "FirstName", "name": "Full name", "autoWidth": true } ] });

Please note this column:

{ "targets": -1, "data": "UserId", "defaultContent": "<button>Click!</button>", "autoWidth": true }

I've followed their documentation on how to render an HTML element there... But what I need now and wasn't able to figure how do I actually set a certain attribute for this HTML element inside the datatable when its being generated...

As u can see I've set data source for the datatable as UserId, and now I'd like each button "Click" to have value whatever the value of UserId is...

Can someone help me out?

P.S. so I want to output an HTML element in that column whos structure would be something like this:

<button values="whatever the value of userId is..?">Click me event</button>

最满意答案

您需要为列定义render属性,如下所示:

{ "targets": -1, "data": "UserId", "render": function (data, type, full, meta) { return "<button id='"+ data +"'>Click!</button>"; }, "autoWidth": true }

data属性将包含UserId ,可以在render函数中使用。

您可以在这里参考它的文档

You would need to define the render property of the column for that which would be the following :

{ "targets": -1, "data": "UserId", "render": function (data, type, full, meta) { return "<button id='"+ data +"'>Click!</button>"; }, "autoWidth": true }

The data property will be containing UserId in it which can be used in the render function.

You can refer to the documentation of it here

更多推荐

本文发布于:2023-08-04 22:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423428.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   属性   内容   数据   jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!