Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multi filter datatables in asp.net MVC

I'm trying to implement the multiple filters in the datatables in asp.net, but the time I search a value, my table is not updated.

I followed the official example of the site, but it did not work. Here is the source code I'm using.

JS on VIEW

$('#students tfoot th').each( function () {
                var title = $(this).text();
                if (title !== "") {
                    $(this).html('<input type="text" class="form-control form-control-sm" style="width: 100%" placeholder="' + title + '" />');
                } else {
                    $(this).html('<div class="text-center">-</div>');
                }
            } );

            tabela.columns().every( function () {
                var that = this;    
                $( 'input', this.header() ).on( 'keydown', function (ev) {
                    if (ev.keyCode == 13) { //only on enter keypress (code 13)
                        that
                            .search( this.value )
                            .draw();
                    }
                } );
            } );

ACTION on CONTROLLER

[HttpPost]
        public JsonResult Listar2()
        {
            var search = Request.Form.GetValues("search[value]")?[0];

            var list = db.Students;

            if (!string.IsNullOrEmpty(search))
            {
                list = list.Where(m => m.name.ToLower().Contains(search.ToLower()) || m.class.ToLower().Contains(search.ToLower()));
            }

            var draw = Request.Form.GetValues("draw")?[0];
            var start = Request.Form.GetValues("start")?[0];
            var length = Request.Form.GetValues("length")?[0];

            var width = length != null ? Convert.ToInt32(length) : 0;
            var skip = start != null ? Convert.ToInt32(start) : 0;

            var totalRecords = list.Count();
            var resultFinal = list.Skip(skip).Take(width).ToList();

            return Json(new
            {
                data = resultFinal,
                draw,
                recordsFiltered = totalRecords,
                recordsTotal = totalRecords
            });
        }
like image 278
Leomar de Souza Avatar asked Nov 30 '25 10:11

Leomar de Souza


1 Answers

I don't know what you want to accomplish. The official example uses JavaScript to sort the datatable which is inserted into HTML already. You should load all the entries first, pass them to the view and then this script should filter those entries

like image 87
Martin Avatar answered Dec 02 '25 01:12

Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!