Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to place Edit and Delete buttons in jQuery DataTables?

I am a beginner using jQuery DataTable, and I am trying to place Edit and Delete buttons in a jQuery DataTable with dynamic databinding in bootstrap like in the below image:

enter image description here

But I get an error message:

enter image description here

Controller Action

    public class PhoneNumber
    {
        public string Number { get; set; }
        public string Description { get; set; }
        public string Action { get; set; }
    }

    public ActionResult LoadPhoneNumbers()
    {
        var phoneNumbers = new List<PhoneNumber>(new[] 
        {
            new PhoneNumber { Number = "555 123 4567", Description = "George",Action="" },
            new PhoneNumber { Number = "555 765 4321", Description = "Kevin" ,Action="" },
            new PhoneNumber { Number = "555 555 4781", Description = "Sam",Action=""  }
        });

        return Json(new
        {
            aaData = phoneNumbers.Select(x => new[] { x.Number, x.Description })
        }, JsonRequestBehavior.AllowGet);
    }

HTML

 <table id="tblAdminUsers" class="table table-striped table-bordered table-hover table-highlight table-checkable" 
                            data-info="true"
                            data-search="true"
                            data-paginate="true">
                            <thead>
                                <tr>                                       
                                    <th>Number</th>
                                    <th>Description</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>

Script

$(function () {

    $("#tblAdminUsers").dataTable({
        bProcessing: true,
        sAjaxSource: '@Url.Action("LoadPhoneNumbers", "Admin")',
        aoColumns: [
             { mData: "Number" },
             { mData: "Description" },
             {
                 mData: "Action",
                 bSortable: false,
                 mRender: function (o) { return '<i class="ui-tooltip fa fa-pencil" style="font-size: 22px;" data-original-title="Edit"></i><i class="ui-tooltip fa fa-trash-o" style="font-size: 22px;" data-original-title="Delete"></i>'; }
             }
        ]
    });
});
like image 991
Ragesh S Avatar asked Jun 10 '14 05:06

Ragesh S


2 Answers

I just remove the mData attribute in aoColumns property

in dataTable Script.

$(function () {

$("#tblAdminUsers").dataTable({
    bProcessing: true,
    sAjaxSource: '@Url.Action("LoadPhoneNumbers", "Admin")',
    aoColumns: [
         { bSortable: false, },
         { bSortable: false, },
         {
             bSortable: false,
             mRender: function (o) { return '<i class="ui-tooltip fa fa-pencil" style="font-size: 22px;" data-original-title="Edit"></i><i class="ui-tooltip fa fa-trash-o" style="font-size: 22px;" data-original-title="Delete"></i>'; }
         }
    ]
});});
like image 142
Ragesh S Avatar answered Oct 07 '22 05:10

Ragesh S


just follow this url:http://editor.datatables.net/examples/styling/envelopeInTable.html. This will solve your concern.

like image 33
Mrityunjay Avatar answered Oct 07 '22 06:10

Mrityunjay