Sort Datatables Component by Date

Asked

Viewed 468 times

2

I’m using the jam Datatables where the birthday of the month returns. I am ordering in my controller per day, and it works correctly in a normal table. There is a way to change the datatable to sort as it receives controller data?

Follow my controller:

 public ActionResult Aniversariantes()
    {

        var usuarios = usuarioRepository.Lista.Where(u => u.DtNascimento.Month == DateTime.Now.Month);
        var usuariosOrdenados = usuarios.OrderBy(u => u.DtNascimento.Day).ToList();
        return View(usuariosOrdenados);

    }

My View:

@model IEnumerable<PortalRH.DomainModel.Entities.Usuario>


@{
ViewBag.Title = "Aniversariantes do Mes";
}

<div class="row">
<div class="col-md-2">

</div>
<div class="col-md-8">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h5><strong>Aniversariantes do Mes</strong></h5>
        </div>
        <table id="myTable" class="table">
            <thead>
                <tr>
                    <th>Nome do Funcionário</th>
                    <th>Secretaria</th>
                    <th>Data do Aniversário</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @item.NmFuncionario
                        </td>
                        <td>
                            @item.Descricao
                        </td>
                        <td>
                            @item.DtNascimento.Day.ToString("00")/ @item.DtNascimento.Month.ToString("00")
                        </td>
                    </tr>
                }
            </tbody>
        </table>

    </div>
</div>

<script>
var $j = jQuery.noConflict();
$j(document).ready(function () {
    $j("#myTable").dataTable();
});
</script>

1 answer

2


Got it, just add the field and the ordering order in my script.

<script>
var $j = jQuery.noConflict();
$j(document).ready(function () {
    $j("#myTable").dataTable({
        "aaSorting": [[2, "asc"]]
    });
});

  • You can accept the answer to make it clear that you have found the solution to your problem!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.