Home > Back-end >  How to set the total records and records filtered in MVC?
How to set the total records and records filtered in MVC?

Time:10-11

I am having trouble on figuring how to set the total records and records filtered from the data. As shown below, the records total and records filtered were still in fixed value so when the datatable is shown, it shows the exact value from the controller.

This is the controller:

[CustomSecurityAttributes]
        public ActionResult GetAllNSLogDetails(int UserID)
        {
            int maxRows = 10;
            int currentPage = 1;
            JsonResult _json = new JsonResult();
            NonconformingServiceManager _NSManager = new NonconformingServiceManager();
            NSLog NSLog = new NSLog();
            NSLog.Customers = _NSManager.GetAllNSIssue(UserID).Skip((currentPage - 1) * maxRows).Take(maxRows).ToList(); ;
            NSLog.recordsTotal = 4218;
            NSLog.recordsFiltered = 4218;
            NSLog.draw = 1;
            NSLog.PageCount = Convert.ToInt32(Math.Ceiling((double)NSLog.Customers.Count() / maxRows));
            _json.Data = NSLog; //  _NSManager.GetAllNSIssue(UserID); 
            _json.MaxJsonLength = int.MaxValue;
            return _json;
        }

This is the JS:

function LoadNonconformingServiceLog() {

    NSDetailsTable = $("#NonconformingServiceLogGrid").DataTable({
        "autoWidth": true,
        //paging: false,
        "scrollX": true,
        pagingType: 'full_numbers',
        processing: true,
        serverSide: true,
        
        /*dom: "<'row be-datatable-header'<'col-sm-6'l><'col-sm-6 text-right'B>>"  
            "<'row be-datatable-body'<'col-sm-12'tr>>"  
            "<'row be-datatable-footer'<'col-sm-4'i><'col-sm-8'p>>",
        buttons: [
            {
                extend: 'csvHtml5', filename: 'NSLogs', text: 'Excel',
                exportOptions: {
                    columns: ':visible'
                }
            },
        ],*/
        dom: 'Bfrtip',
        buttons: [
            'csv', 'pdf'
        ],
        ajax: {
            url: "/NonconformingService/GetAllNSLogDetails?UserID="   userID,
            type: 'POST',
            datatype: "json",
            
            dataSrc: "Customers",

            dataFilter: function (data) {
                var json = jQuery.parseJSON(data);
                
                json.recordsFiltered = json.recordsFiltered;
                json.data = json.Customers;
                return JSON.stringify(json); // return JSON string
            },
        },

        // Scroll Bar 
        scrollY: 650,
        deferRender: true,
        scroller: true,
        
        loadingIndicator: true,

        "columns": [
            {
                "data": "DateIssue", render: function (data, _type, _row) {
                    if (data == "" || data == null) {
                        return "";
                    }
                    return moment(data).format('YYYY-MM-DD');
                }
            },
            {
                "data": "NSCode",
                render: function (data, _type, row) {
                    if (row.Company == 1) {
                        return `<a href="javascript:RedirectActionView('${row.NSCode}')" style="color: cornflowerblue; cursor: pointer;"> ${data} </a>`;
                    }
                    else if (row.Company == 2) {
                        return `<a href="javascript:RedirectActionToALIView('${row.NSCode}')" style="color: cornflowerblue; cursor: pointer;"> ${data} </a>`;
                    }
                }
            },
            {
                "data": "SIRCode", render: function (data, _type, row) {
                    if (data == "" || data == null) {
                        return "<p>&#8212;</p>";
                    }
                    else if (userType == 4) {
                        return data;
                    }
                    else {
                        return `<a href="javascript:RedirectActionViewToSIR('${row.ID}')" style="color: cornflowerblue; cursor: pointer;"> ${data} </a>`;
                    }
                }
            },
            {
                "data": "SIRDateIssue", render: function (data, _type, row) {
                    if (data == "" || data == null) {
                        return "<p>&#8212;</p>";
                    }
                    return moment(data).format('YYYY-MM-DD');
                }
            },
            {
                "data": "SIRDueDate", render: function (data, _type, row) {
                    dDate = moment(data).format('ll');
                    if (row.FormatIssueDate == "late") {
                        return `<p style="color: #b57272"> ${moment(dDate, 'll').fromNow()} </p>`;
                    }
                    else if (row.FormatIssueDate == "not") {
                        return `<p style="color: #7fb581"> ${moment(dDate, 'll').fromNow()} </p>`;
                    }
                    else if (row.FormatIssueDate == "submitted") {
                        return dDate;
                    }
                    else {
                        return "<p>&#8212;</p>";
                    }
                }
            },
            {
                "data": "SubmissionDate", render: function (data, _type, row) {
                    if (data == "" || data == null) {
                        return "<p>&#8212;</p>";
                    } else {
                        if (row.SIRDueDate > data) {
                            return `<p style="color: #7fb581"> ${moment(data).format('ll')} </p>`;
                        }
                        else {
                            return `<p style="color: #b57272"> ${moment(data).format('ll')} </p>`;
                        }
                    }
                }
            },
            { "data": "TypeName" },
            { "data": "CategoryName" },
            { "data": "ConcernedDeptName" },
            /*{ "data": "Remarks" },*/
            {
                "data": "Status", render: function (data, _type, row) {
                    if (data == 1) {
                        return "<p style='color: blue'>In Progress</p>";
                    }
                    else if (data == 2) {
                        return "<p style='color: green'>Submitted</p>";
                    }
                    else if (data == 3) {
                        return "<p style='color: #6b0334'>Delayed</p>";
                    }
                    else if (data == 4) {
                        return "<p style='color: blue'>For CE</p>";
                    }
                    else if (data == 5) {
                        return "<p style='color: #058017'>Effective - Closed</p>";
                    }
                    else if (data == 6) {
                        return "<p style='color: #9e7306'>Ineffective</p>";
                    }
                    else if (data == 7) {
                        return "<p style='color: #780404'>Invalid / Cancelled</p>";
                    }
                    else
                        return "<p>&#8212;</p>";
                }
            },
            {
                "data": "CEDueDate", render: function (data, _type, row) {
                    if (data == "" || data == null) {
                        return "<p>&#8212;</p>";
                    }
                    return moment(data).format('ll');
                }
            }
        ]

    })
    
}

And this is the View Model:

namespace SIMS.Model.Models
{
    public class NSLog
    {
        public List<NonconformingServiceModel> Customers { get; set; }
        public int CurrentPageIndex { get; set; }
        public int PageCount { get; set; }
        public int recordsTotal { get; set; }
        public int recordsFiltered { get; set; }
        public int draw { get; set; }
    }
    public class NonconformingServiceModel
    {

        public int ID { get; set; }
        public byte Type { get; set; }
        public string TypeName { get; set; }
        public string OtherType { get; set; }
        public byte Category { get; set; }
        public string CategoryName { get; set; }
        public string OtherCategory { get; set; }
        public string Reason { get; set; }
        public string Remarks { get; set; }
        public string ModeThru { get; set; }
        public DateTime? DateDelivered { get; set; }
        public DateTime? LoadingDate { get; set; }
        public DateTime DateIssue { get; set; }
        public string ConcernedDeptString { get; set; }
        public List<int> ConcernedDeptArray { get; set; }
        public List<string> ConcernedDeptName { get; set; }
        public string Origin { get; set; }
        public string Destination { get; set; }
        public string CartonNumber { get; set; }
        public string ShipmentNumber { get; set; }
        public string ShipperName { get; set; }
        public string ConsigneeName { get; set; }
        public string Address { get; set; }
        public string NSGroup { get; set; }
        public int CreatedByID { get; set; }
        public string CreatedByName { get; set; }
        public int CreatedByDept { get; set; }
        public int UserType { get; set; }
        public string NSCode { get; set; }
        public byte? Status { get; set; }
        public int? SIRID { get; set; }
        public string SIRCode { get; set; }
        public DateTime? SIRDateIssue { get; set; }
        public DateTime? SIRDueDate { get; set; }
        public DateTime? SubmissionDate { get; set; }
        public DateTime? CEDueDate { get; set; }
        public DateTime? DetectionDate { get; set; }
        public string FormatIssueDate { get; set; }
        public int UserID { get; set; }
        public int? Company { get; set; }
    }

}

What is the possible solution to this problem? How to set the total records and records filtered from the data instead of the fixed value? All answers will be appreciated.

CodePudding user response:

I figured it out on how to fix my own problem.

This is the problem:

NSLog.Customers = _NSManager.GetAllNSIssue(UserID).Skip((currentPage - 1) * maxRows).Take(maxRows).ToList(); ;
NSLog.recordsTotal = 4218;
NSLog.recordsFiltered = 4218;

My solution is I used the Count() method:

NSLog.Customers = _NSManager.GetAllNSIssue(UserID).Skip((currentPage - 1) * maxRows).Take(maxRows).ToList(); ;
NSLog.recordsTotal = NSLog.Customers.Count();
NSLog.recordsFiltered = NSLog.Customers.Count();
  • Related