Advertisement
HenX

DataTable

Dec 25th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.83 KB | None | 0 0
  1. // View
  2.  [CustomAuthorize(Roles = "admin")]
  3.         public ActionResult Log()
  4.         {
  5.             Log model = new Log();            
  6.  
  7.             return View(model);
  8.         }
  9.  
  10. // List
  11.  [System.Web.Mvc.HttpGet]
  12.         [System.Web.Mvc.Authorize(Roles = "admin")]
  13.         public JsonResult GetLog(DataTablesParam param)
  14.         {
  15.             return Json(
  16.                     new
  17.                     {
  18.                         aaData = new PagedList<Log>(appContext.Logs.OrderByDescending(m => m.Id), (param.iDisplayStart / param.iDisplayLength) + 1, param.iDisplayLength)
  19.                     }
  20.                     , JsonRequestBehavior.AllowGet);
  21.         }
  22.  
  23. // View
  24.   <script type="text/javascript">
  25.         var table;
  26.  
  27.         $(document).ready(function () {
  28.             $("#signalrbutton").click(function () {
  29.                 $("#signalrModal").modal({ show: true });
  30.                 $("#signalrrefresh").click();
  31.             });
  32.  
  33.             $("#signalrrefresh").click(function () {
  34.                 $.ajax({
  35.                     method: "GET",
  36.                     contenttype: "application/x-www-form-urlencoded",
  37.                     url: "@Url.Action("SignalRClients", "Administrace")"
  38.                 })
  39.                .success(function (html) {
  40.                    $("#signalrclients-holder").html(html);
  41.                });
  42.             });
  43.  
  44.             table = $('#logs').dataTable({
  45.                 "bDestroy": true,
  46.                 "bProcessing": true,
  47.                 "bServerSide": true,
  48.                 "sAjaxSource": '@Url.Action("GetLog", "Administrace")',
  49.                 "sScrollX": "100%",
  50.                 "sScrollXInner": "100%",
  51.                 "bScrollCollapse": true,
  52.                 "iDisplayLength": 25,
  53.                 "order": [[0, "desc"]],
  54.                 "aoColumns":
  55.                 [
  56.                     {
  57.                         "mData": "ActionDate",
  58.                         "mRender": function (data, type, full) {
  59.                             if (type === 'set') {
  60.                                 var dtStart = new Date(parseInt(data.substr(6)));
  61.                                 var dtStartWrapper = moment(dtStart);
  62.                                 return dtStartWrapper.format('D.M.YYY H:mm:ss');
  63.                             }
  64.                             else if (type === 'display' || type === 'filter') {
  65.                                 var dtStart = new Date(parseInt(data.substr(6)));
  66.                                 var dtStartWrapper = moment(dtStart);
  67.                                 return dtStartWrapper.format('D.M.YYYY H:mm:ss');
  68.                             }
  69.                             else {
  70.                                 return data;
  71.                             }
  72.                         },
  73.                         "bSearchable": true,
  74.                         "bSortable": true
  75.                     },
  76.                     {
  77.                         "mData": "ActionName",
  78.                         "bSearchable": true,
  79.                         "bSortable": true
  80.                     },
  81.                      {
  82.                          "mData": "ActionMessage",
  83.                          "bSearchable": true,
  84.                          "bSortable": true
  85.                      },
  86.                     {
  87.                         "mData": "User",
  88.                         "bSearchable": true,
  89.                         "bSortable": true
  90.                     },
  91.                     {
  92.                         "mData": "RemoteIP",
  93.                         "bSearchable": true,
  94.                         "bSortable": true,
  95.                     },
  96.                     {
  97.                         "mData": "Browser",
  98.                         "bSearchable": true,
  99.                         "bSortable": true,
  100.                     }
  101.                     @if (Context.User.IsInRole("superadmin"))
  102.                     {
  103.                         <text>
  104.                                 , {
  105.                                     "mData": "Id",
  106.                                     "mRender": function (data, type, full) {
  107.                                         var button = '<button onclick="DeleteLog(' + data + ')" class="btn btn-sm btn-danger"><span class="glyphicon glyphicon-remove"></span> Smazat</button>';
  108.                                         return button;
  109.                                     },
  110.                                     "bSearchable": false,
  111.                                     "bSortable": false,
  112.                                 }
  113.                         </text>
  114.                     }
  115.                 ],
  116.                 "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
  117.                     if (aData.ActionName == "Chyba") {
  118.                         $('td', nRow).addClass("danger");
  119.                     } else if (aData.ActionName == "SignalR") {
  120.                         $('td', nRow).addClass("warning");
  121.                     }
  122.                 },
  123.                 language: {
  124.                     processing: '',
  125.                     search: "Hledat:",
  126.                     lengthMenu: "Počet výsledků na stránku: _MENU_",
  127.                     info: "Zobrazuji _START_ z _END_ z  _TOTAL_ výsledků",
  128.                     infoEmpty: "Nenalezena žádná data",
  129.                     infoFiltered: "(filtrováno z celkového počtu _MAX_ výsledků)",
  130.                     infoPostFix: "",
  131.                     loadingRecords: '<img src="/Images/loading.gif" height=32/> ',
  132.                     zeroRecords: "Nenalezena žádná data",
  133.                     emptyTable: "Nenalezena žádná data",
  134.                     paginate: {
  135.                         first: "První",
  136.                         previous: "Předchozí",
  137.                         next: "Další",
  138.                         last: "Poslední"
  139.                     },
  140.                 },
  141.                 "sPaginationType": "full_numbers"
  142.             });
  143.         });
  144.  
  145.    <table id="logs" class="table table-hover">
  146.         <thead>
  147.             <tr>
  148.                 <th>
  149.                     @Html.DisplayNameFor(model => model.ActionDate)
  150.                 </th>
  151.                 <th>
  152.                     @Html.DisplayNameFor(model => model.ActionName)
  153.                 </th>
  154.                 <th>
  155.                     @Html.DisplayNameFor(model => model.ActionMessage)
  156.                 </th>
  157.                 <th>
  158.                     @Html.DisplayNameFor(model => model.User)
  159.                 </th>
  160.                 <th>
  161.                     @Html.DisplayNameFor(model => model.RemoteIP)
  162.                 </th>
  163.                 <th>
  164.                     @Html.DisplayNameFor(model => model.Browser)
  165.                 </th>
  166.                 @if (Context.User.IsInRole("superadmin"))
  167.                 {
  168.                     <th>
  169.                         Smazat
  170.                     </th>
  171.                 }
  172.             </tr>
  173.         </thead>
  174.     </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement