Skip to content

Instantly share code, notes, and snippets.

View davidsonsousa's full-sized avatar
🤓
Coding

Davidson Sousa davidsonsousa

🤓
Coding
View GitHub Profile
@model List<string>
@using System.Text
@section Styles {
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
}
@{
var tableHeader = new StringBuilder();
[HttpPost]
public IActionResult GetPeopleData(DataParameters parameters)
{
var items = personService.GetPeopleForDataTable(parameters);
var dataTable = DataTableHelper.BuildDataTable(items.Data, items.Count);
dataTable.Draw = parameters.Draw;
return Ok(dataTable);
}
public IActionResult Index()
{
ViewData["Title"] = "Home Page";
var header = DataTableHelper.BuildDataTableHeader(new PersonTableViewModel());
return View(header);
}
$(document).ready(function () {
$("#table-list").DataTable({
"processing": true,
"serverSide": true,
"ajax": {
type: "POST",
url: "home/getpeopledata"
}
});
});
using ReportsWeb.Attributes;
using ReportsWeb.Models.DataTable;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
namespace ReportsWeb.Helpers
{
public (IEnumerable<PersonTableViewModel> Data, int Count) GetPeopleForDataTable(DataParameters parameters)
{
var items = personRepository.GetPeopleReadOnly()
.Select(x => new PersonTableViewModel
{
FullName = x.FullName,
PreferredName = x.PreferredName,
Phone = x.Phone,
Email = x.Email,
Age = (DateTime.Now.Subtract(x.DateOfBirth).Days / 365).ToString(),
using Newtonsoft.Json;
using System.Collections.Generic;
namespace ReportsWeb.Models.DataTable
{
public class TableViewModel
{
[JsonProperty(PropertyName = "draw")]
public int Draw { get; set; }
using ReportsWeb.Attributes;
using ReportsWeb.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace ReportsWeb.Models.DataTable
{
using Newtonsoft.Json;
using System.Collections.Generic;
namespace ReportsWeb.Models.DataTable
{
public sealed class DataParameters
{
[JsonProperty(PropertyName = "draw")]
public int Draw { get; set; }
using ReportsWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ReportsWeb.Repository
{
public sealed class PersonRepository
{
public IEnumerable<Person> GetPeopleReadOnly()