mardi 22 septembre 2015

How to create and set checkbox in DataTables

I have a table that is loaded with an AJAX call and rendered with the DataTables extension for jQuery. This is my code:

 $("#tblSelectedUserRoles").dataTable({
        bProcessing: true,
        sAjaxSource: '@Url.Action("GetAllRolesForUser")?' + "userId=" + $("#txtSelectedUserId").val(),
        bJQueryUI: true,
        sProcessing: "<img src='~/Images/spinner.gif' />",
        dom: 'T<"clear">rtip',
        bAutoWidth: false,
        "aoColumns": [
            { "sWidth": "1%", sClass: "smallFonts" },
            { "sWidth": "20%", sClass: "smallFonts" },
            { "sWidth": "20%", sClass: "smallFonts" },
            {
                "sWidth": "20%", sClass: "smallFonts", "sName": "UserRoleId", "mRender": function (data, type, row) {
                    return "<input type='checkbox' onchange=defaultrole('" + row[0] + "'); >";
                }},
            { "sWidth": "20%", sClass: "smallFonts" },
            {
                "sName": "UserRoleId", "sWidth": "20%", sClass: "smallFonts", "mRender": function (data, type, row) {
                    return "<button class='gridButton' onclick=deleterole('" + row[0] + "');>Delete Role</button>";
                }}
        ]
    });

I'm trying to put a checkbox in the table. The checkbox show up and when I click on any of them I invoke the function with the proper user_role_id without a problem. My problem is the data is coming from this in the controller:

 [HttpGet]
    public ActionResult GetAllRolesForUser(string userId)
    {
        List<UserRoleModel> userroles = DataRepository.GetSelectedUserRoles(userId);
        return Json(new
        {
            aaData = userroles.Select(x => new String[] {
                x.UserRoleId,
                x.Role,
                x.RoleDesc,
                x.Default.ToString(),
                x.UarApproved.ToString()})
        }, JsonRequestBehavior.AllowGet);
    }

the default and approved valued are Boolean and I have to convert them to a string to pass then in the aaData. In any case, none of the checkboxes are set when at least one is. How do you do this?




Aucun commentaire:

Enregistrer un commentaire