Javascript jQuery datatable-选择列中的列表
我目前正在一个jQuery数据表中提取数据,这个数据表非常大!但我对角色专栏有意见 我试图在该列中插入一个选择列表,该列表将有两个选项“admin”和“User”,以及数组中的任何数据在选择列表中设置该值 但我无法将选择列表呈现到表中,然后根据数组中的内容将其设置为admin或userJavascript jQuery datatable-选择列中的列表,javascript,jquery,datatables,Javascript,Jquery,Datatables,我目前正在一个jQuery数据表中提取数据,这个数据表非常大!但我对角色专栏有意见 我试图在该列中插入一个选择列表,该列表将有两个选项“admin”和“User”,以及数组中的任何数据在选择列表中设置该值 但我无法将选择列表呈现到表中,然后根据数组中的内容将其设置为admin或user jQuery(函数($){ 风险值数据=[ [“test1”、“admin”、“yes”], [“test2”,“admin”,“yes”], [“test3”、“用户”、“否”], [“test4”、“用户”
jQuery(函数($){
风险值数据=[
[“test1”、“admin”、“yes”],
[“test2”,“admin”,“yes”],
[“test3”、“用户”、“否”],
[“test4”、“用户”、“否”]
]
函数authusers(){
t=$('#authTable').DataTable({
检索:对,
分页:false,
数据:数据,
栏目:[{
“标题”:“电子邮件”,
“呈现”:函数(数据、类型、行、元){
返回行[0];
}
}, {
“标题”:“角色”,
“呈现”:函数(数据、类型、行、元){
返回行[1];
}
}, {
“标题”:“活动”,
“呈现”:函数(数据、类型、行、元){
var复选框=$(“”{
“键入”:“复选框”
});
checkbox.attr(“选中”,第[2]行==“是”);
checkbox.addClass((第[2]行==“是”)?“checkbox\u checked”:“checkbox\u unchecked”)
return checkbox.prop(“outerHTML”)
}
}, ],
订单:[]
});
}
//将值以及该值是否已启用或禁用传递给服务器端。
$(文档).on('单击',')。复选框未选中,.复选框未选中,'函数(){
如果($(this).is(':checked')){
var rowIndex=$(this).closest('tr').find('td:eq(0)').text();
风险值状态='是';
}else if($(this).prop('checked',false)){
var rowIndex=$(this).closest('tr').find('td:eq(0)').text();
风险值状态='否';
}
});
authusers();
});代码>
电子邮件
活跃的
角色
这些是代码中的几个问题,例如数据表选项不应位于“”。在这里检查校正的小提琴手
jQuery(函数($){
var sdata=[
[“test1”、“admin”、“yes”],
[“test2”,“admin”,“yes”],
[“test3”、“用户”、“否”],
[“test4”、“用户”、“否”]
]
函数authusers(数据){
t=$('#authTable').DataTable({
检索:对,
分页:false,
数据来源:sdata,
栏目:[{
“标题”:“电子邮件”,
“呈现”:函数(数据、类型、行、元){
返回行[0];
}
}, {
“标题”:“角色”,
“呈现”:函数(数据、类型、行、元){
返回行[1];
}
}, {
“标题”:“活动”,
“呈现”:函数(数据、类型、行、元){
var复选框=$(“”{
“键入”:“复选框”
});
checkbox.attr(“选中”,第[2]行==“是”);
checkbox.addClass((第[2]行==“是”)?“checkbox\u checked”:“checkbox\u unchecked”)
return checkbox.prop(“outerHTML”)
}
}, ],
订单:[]
});
}
//将值以及该值是否已启用或禁用传递给服务器端。
$(文档).on('单击',')。复选框未选中,.复选框未选中,'函数(){
如果($(this).is(':checked')){
var rowIndex=$(this).closest('tr').find('td:eq(0)').text();
风险值状态='是';
console.log(rowIndex);
控制台日志(状态);
}else if($(this).prop('checked',false)){
var rowIndex=$(this).closest('tr').find('td:eq(0)').text();
风险值状态='否';
console.log(rowIndex);
控制台日志(状态);
}
});
authusers();
});
对于“角色”列中的“选择”,可以执行以下操作
"title": "Role",
"render": function(data, type, row, meta) {
var a = sdata.indexOf(row);
var select = $("<select id='role_"+a+"'><option value ='admin'>admin</option><option value ='user'>user</option</select>");
$("#role_"+a).val(row[1]);
return select.prop("outerHTML")
“标题”:“角色”,
“呈现”:函数(数据、类型、行、元){
var a=sdata.indexOf(行);
var select=$(“adminuser以下是我如何实现这一目标的,可能对某些人有帮助:)
{
“标题”:“呈现”:函数(数据、类型、完整、元){
var html='';
变量$select=$(“”{
“id”:“EmployeeId”+完整的.id,
“值”:全名
});
$select.append(“”);
$.each(full.EmployeeList,函数(索引,元素){
var$选项=$(“”{
“text”:element.Id,
“值”:元素名
});
if(full.Id==element.Id){
$option.attr(“选定”、“选定”)
}
$select.append($option);
});
html+=$select[0].outerHTML;
html+='';
html+='';
返回html;
}
}
小提琴和我的一样,只是去掉了“"实际上没有回答如何在列中有一个选择框OK,让我检查一下a部分,我检查了你共享的提琴不起作用,所以只是纠正了它使其起作用我已经根据你的要求更新了代码,希望这能帮助你。有一些输入选择的例子我已经看过了这些例子,我的问题是渲染它们基于数组中的数据在表中。
"title": "Role",
"render": function(data, type, row, meta) {
var a = sdata.indexOf(row);
var select = $("<select id='role_"+a+"'><option value ='admin'>admin</option><option value ='user'>user</option</select>");
$("#role_"+a).val(row[1]);
return select.prop("outerHTML")
{
"title": "", "render": function (data, type, full, meta) {
var html = '<div>';
var $select = $("<select></select>", {
"id": "EmployeeId_"+ full.Id,
"value": full.Name
});
$select.append("<option value =''></option>");
$.each(full.EmployeeList, function (index, element) {
var $option = $("<option></option>", {
"text": element.Id,
"value": element.Name
});
if (full.Id === element.Id) {
$option.attr("selected", "selected")
}
$select.append($option);
});
html += $select[0].outerHTML;
html += '<a href="#" onclick="changeName(this,' + full.Id + ')" id="tag' + full.Id + '">Change</a>';
html += '</div>';
return html;
}
}