我安装了我的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
更多推荐
发布评论