kelumKP / Sample-Codes

This is sample code 100% made by me for future references
0 stars 0 forks source link

jQuery DataTable load values from list #3

Open kelumKP opened 8 years ago

kelumKP commented 8 years ago

http://stackoverflow.com/questions/24133689/is-it-possible-to-place-edit-and-delete-buttons-in-jquery-datatables

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>'; }
             }
        ]
    });
});
``