简体   繁体   English

在DataTables jquery插件中设置复选框值

[英]Set checkbox value in DataTables jquery plugin

I'm using DataTable jquery plugin to show some data in my app... 我正在使用DataTable jquery插件在我的应用程序中显示一些数据...

I have this column definition: 我有此列定义:

 columns: [
  { "data" : "appName" },
  { "data" : "uuid" },
  { 
   "data" : "Enabled",
   'render': function(data, type, row)
   {
   console.log(data)
  return '<input type="checkbox" checked = "' + data + '" >';
   }
   }

and coresponding fields in array of objects passed from jquery. 和从jQuery传递的对象数组中的coresponding字段。 console.log(data) returns proper value, true of false for every record in table, but checkbox is always checked. console.log(data)返回正确的值,表中的每个记录的true为false,但始终选中复选框。

What i'm doing wrong here? 我在这里做错了什么?

return '<input type="checkbox" checked = "' + data + '" >';

On this line you're adding the attribute checked hence it is always checked: 在此行上,您添加了checked的属性,因此始终选中该属性:

return '<input type="checkbox"' + (data ? ' checked="checked"' : '') + '>';

(Note that the value of checked does not need to be "checked" or even present, the attribute is the only thing that needs to be present, but it's generally preferred to use some meaningful value.) (请注意, checked的值不需要"checked"甚至不需要显示,该属性是唯一需要显示的东西,但是通常首选使用一些有意义的值。)

Edit: 编辑:

As your data appears to be a string rather than a boolean you'll need to compare data to "true" : 由于您的数据似乎是字符串而不是布尔值,因此需要将数据与"true"进行比较:

return '<input type="checkbox"' + (data == "true" ? ' checked="checked"' : '') + '>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM