基于列值启用或禁用Jquery Datatable Checkbox

时间:2019-01-07 14:45:20

标签: jquery datatables

我需要启用或禁用数据表中“状态”列值的复选框。我希望状态为“已关闭”时禁用该复选框。我知道可以通过更改查询中的where子句来停止显示关闭票证。但我希望用户能够看到所有票证

表格显示

enter image description here

C#代码

 public ActionResult GetChildTickets1(int id)
    {
        _db.Configuration.ProxyCreationEnabled = false;
        IQueryable<VmRequest> results = _db.VmRequests.Where(i => i.ParentId == id) ;
        return Json(results, JsonRequestBehavior.AllowGet);
    }

jQuery代码

 // Datatable value from the database
  var enabletemplateListVM;
    function tchildticket () {
        enabletemplateListVM = {
            dt: null,
            init: function () {
                dt = $('#childtable').DataTable({
                    "pageLength": 10,                                              
                    "ajax": {
                        // Url
                        "url": "/Home/GetChildTickets1?id="+@ViewBag.id,
                        "type": "POST",
                        "datatype": "json",
                        "dataSrc": function (d) {
                            return d
                        }
                    },
                     // Table Columns to display the data
                    "columns": [                            
                   {
                            "targets": [0],
                            "data": "Id", "autoWidth": true,
                            "render": function (data, type, full) {
                                return '<input type="checkbox" id="cticket" name="cticket" value="' + full.Id + '"/>';
                            },
                        },
                        { "title": "Ticket Id", "data": "Id", "name": "Id" },
                        {
                            "title": "Logged On", "data": "CreatedOn", "name": "CreatedOn",
                            // Date Formating
                          render: function (data, type, full, meta) {
                                if (data !== null) {
                                    return (moment(data).format("DD/MM/YYYY"));
                                } else {
                                    return '';
                                }
                            }
                        },
                        { "title": "Ticket Type", "data": "TypeofWork", "name": "TypeofWork" },
                        { "title": "Subject", "data": "Subject", "name": "Subject" },
                        { "title": "Contact", "data": "ContactId", "name": "ContactId" },                         
                        { "title": "Status ", "data": "CurrentStatus", "name": "CurrentStatus" },                        
                        { "title": "Team", "data": "Teamid", "name": "Teamid" },
                    ],
                });
            }
        }
        enabletemplateListVM.init();
    }

2 个答案:

答案 0 :(得分:1)

对包含复选框的列使用下面的代码。

请注意,我以为已关闭的凭单在状态栏中包含“已关闭”,但请相应地更改代码。

{
    "data": "Id", 
    "autoWidth": true,
    "render": function (data, type, full) {
        if(type === 'display'){
             var attrDisabled = '';

             // If ticket is closed
             if(full['CurrentStatus'] === 'Closed'){
                // Disable the checkbox
                attrDisabled = 'disabled';
             }

             data = '<input type="checkbox" id="cticket" name="cticket" value="' + full.Id + '" ' + attrDisabled + '/>';
        }

        return data;
    }
}

答案 1 :(得分:0)

没有完整的代码很难为您提供帮助。但是,如果在创建数据表之后,则使用类似以下内容的

jQuery('table').each(function(data) {
        console.log(data);
});

您可以获取数据表的内容并编辑复选框的可见性。