C# MVC Core 5, Datatables. Summary Property

Viewed 69

I am writing an app in MVC Core 5. I have a domain entity whereby I am doing a calculation. I am also using ABP.IO framework. I know how to retrieve data from a database, but I am trying to retrieve information from the summary property and don't know how to get the datatable to display it.

My domain entity:

using CRM.Enums;
using CRM.Interfaces.Common;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace CRM.Entities.Common
{
    public abstract class MileageLogBase : FullAuditedAggregateRoot<int>, IMileageLogBase, IMultiTenant, IValidatableObject
    {
        [HiddenInput(DisplayValue = false)]
        [ScaffoldColumn(false)]
        public Guid? TenantId { get; set; }

        [Column(TypeName = "date")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, ConvertEmptyStringToNull = false, DataFormatString = "{0:yyyy-MM-dd}")]
        [Required]
        public DateTime DateOccurred { get; set; }

        [Range(000000.0, 999999.9)]
        [Required]
        public int StartOdometer { get; set; }

        [Range(000000.0, 999999.9)]
        [Required]
        public int EndOdometer { get; set; }

        public bool IsCompanyVehicle { get; set; }

        public bool IsReimbursedByCompany { get; set; }

        [EnumDataType(typeof(ContractorType))]
        [HiddenInput(DisplayValue = true)]
        [Required]
        [ScaffoldColumn(false)]
        public ContractorType CompanyType { get; set; }

        public int CalculateTotalMilesDriven
        {
            get
            {
                return EndOdometer - StartOdometer;
            }
        }

        public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            var TodaysDate = DateTime.Today;

            if (DateOccurred > TodaysDate)
            {
                yield return new ValidationResult(
                    "The date cannot be in the future. Please specify a valid date.",
                    new[] { nameof(DateOccurred) }
                );
            }
        }
    }
}

The JS file.

$(function () {
    var l = abp.localization.getResource('CRM');

    var dataTable = $('#AmazonFreshTable').DataTable(
        abp.libs.datatables.normalizeConfiguration({
            serverSide: true,
            paging: true,
            order: [[1, "asc"]],
            searching: false,
            scrollX: true,
            ajax: abp.libs.datatables.createAjax(cRM.services.contractor.mileageLog.amazonFreshMileageLog.getList),
            columnDefs: [
                {
                    title: l('Datatable:ColumnHeader:DateOccurred'),
                    data: "dateOccurred",
                    render: function (data) {
                        return luxon
                            .DateTime
                            .fromISO(data, {
                                locale: abp.localization.currentCulture.name
                            }).toLocaleString();
                    }
                },
                {
                    title: l('Datatable:ColumnHeader:StartOdometer'),
                    data: "startOdometer"
                },
                {
                    title: l('Datatable:ColumnHeader:EndOdometer'),
                    data: "endOdometer"
                },
                {
                    title: l('Datatable:ColumnHeader:CalculateTotalMilesDriven'),
                    data: "calculateTotalMilesDriven"
                },
                {
                    title: l('Datatable:ColumnHeader:IsCompanyVehicle'),
                    data: "isCompanyVehicle"
                },
                {
                    title: l('Datatable:ColumnHeader:IsReimbursedByCompany'),
                    data: "isReimbursedByCompany"
                },
                //{
                //    title: l('Datatable:ColumnHeader:ContractorType'),
                //    data: "companyType",
                //    render: function (data) {
                //        return l('Enum:ContractorType:' + data);
                //    }
                //},
                {
                    title: l('Datatable:ColumnHeader:CreationTime'), data: "creationTime",
                    render: function (data) {
                        return luxon
                            .DateTime
                            .fromISO(data, {
                                locale: abp.localization.currentCulture.name
                            }).toLocaleString(luxon.DateTime.DATETIME_SHORT);
                    }
                }
            ]
        })
    );
});

I am trying to display the CalculateTotalMilesDriven in the datatable. Please advise if anyone knows how to do this. I have looked online and not able to find a resolution to this issue.

Thanks, Em.

0 Answers
Related