我的 DataTables 配置是这样的:
(function ($) {
"use strict";
$(document).ready(function () {
$('#data-table-users').DataTable({
"processing": true,
"serverSide": true,
"filter": true,
"orderMulti": false,
"ajax": {
"url": "/Users/GetUsersData",
"type": "POST",
"datatype": "json"
},
"columnDefs":
[{
"targets": [0],
"visible": false,
"searchable": false
}],
"columns": [
.............................
{
"data": "Image",
"orderable": false,
"render": function (data, type, row, meta) {
var imgSrc;
if (data != null) {
imgSrc = '"data:image/jpeg;base64,' + atob(data) + '"';
}
else {
imgSrc = "/svg/" + "A" + ".svg";
}
return '<div><img class="small-img" src=' + imgSrc + '></div>';
}
},
{
"data": "UserName",
"render": function (data, type, row, meta) {
return '<div class="positioned"> <div class="editable" data-name="userName" contenteditable="true">'
+ data + '</div > <i class="fa fa-pencil pushright" aria-hidden="true"></i> </div>';
}
},
{
"data": "FirstName",
"render": function (data, type, row, meta) {
if (data == null)
data = "";
return '<div class="positioned"> <div class="editable" data-name="userName" contenteditable="true">'
+ data + '</div > <i class="fa fa-pencil pushright" aria-hidden="true"></i> </div>';
}
},
............................
],
"order": [[8, "desc"]]
});
});
})(jQuery);
对于 Render 函数中的图像列:
else {
imgSrc = "/svg/" + "A" + ".svg";
}
我想要的不是 imgSrc = "/svg/A.svg"而是像这样基于 UserName 列构建它:
imgSrc = "/svg/" + userName[0].toUpperCase() + ".svg";
如何在不更改服务器端代码的情况下做到这一点?在我看来,DataTables 稍后会呈现 UserName 单元格,因此图像单元格已经呈现。当呈现 UserName 以更改图像单元格内容时,可以以某种方式添加触发器吗?
我可以在呈现整个表格后更改 Image 列中所有单元格的值,但不知何故我不喜欢这个想法,因为它可能看起来很丑。所以,请不要建议。
最佳答案
刚收到。我可以使用行参数访问当前行的所有数据。
所以它只是:
imgSrc = "/svg/" + row['UserName'][0].toUpperCase() + ".svg";
关于javascript - 在 jQuery DataTables 中使用 "later"变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56608352/