使用DataTable时如何显示DateTime?

发布于 2025-02-12 05:03:52 字数 1383 浏览 0 评论 0原文

我正在使用Razor页面在ASP.NET Core中构建Web应用程序。但是,我还想尝试使用一些JavaScript显示数据表。我想显示属性的启动时间,末日和添加的属性,这些属性是类型的DateTime。但是如果没有进行很多背景转换,我就无法使它起作用。

我的问题是,显示存储在DateTime属性中的值的最简单方法是什么?

这就是我的JS文件的样子,它从自定义类移动的对象中显示信息:

$(document).ready(function () {

    
    $('#DT_load').DataTable({
        "ajax": {
            "url": "/api/Shift",
            "type": "GET",
            "datatype":"json"
        },
        "columns": [
            { "data": "user.name", "width": "25%" },
            { "data": "location.name", "width": "25%" },
            {**<!-- Here i want to display DateTime properties values -->**
                "data": "id",
                "render": function (data) {
                    return `<div class="w-75 btn-group" >
                            <a href="/Admin/Shifts/upsert?id=${data}" class="btn btn-success text-white mx-2">
                            <i class="bi bi-pencil-square"></i> </a>
                            <a href="/Admin/Shifts/upsert?id=${data}"  class="btn btn-danger text-white mx-2">
                            <i class="bi bi-trash-fill"></i> </a>
                            </div>`

                },
                "width":"15%"
            },
        ],
        "width":"100%"
    });
});

我正在使用一个控制器,该控制器获取具有特定属性的所有属性,但包括属性也不起作用。

I am building a web app in ASP.NET Core using Razor Pages. However, i also wanted to try using some javascript to display a datatable. I want to display the properties StartTime, EndTime, and Added, which are of the type DateTime. But i cannot get it to work without doing a lot of background conversions.

My question is, what would be the simplest way to display the values stored in the DateTime properties?

This is what my js-file looks like, it displays information from an object of the custom class Shift:

$(document).ready(function () {

    
    $('#DT_load').DataTable({
        "ajax": {
            "url": "/api/Shift",
            "type": "GET",
            "datatype":"json"
        },
        "columns": [
            { "data": "user.name", "width": "25%" },
            { "data": "location.name", "width": "25%" },
            {**<!-- Here i want to display DateTime properties values -->**
                "data": "id",
                "render": function (data) {
                    return `<div class="w-75 btn-group" >
                            <a href="/Admin/Shifts/upsert?id=${data}" class="btn btn-success text-white mx-2">
                            <i class="bi bi-pencil-square"></i> </a>
                            <a href="/Admin/Shifts/upsert?id=${data}"  class="btn btn-danger text-white mx-2">
                            <i class="bi bi-trash-fill"></i> </a>
                            </div>`

                },
                "width":"15%"
            },
        ],
        "width":"100%"
    });
});

I am using a controller that gets all properties with specific properties, but including the properties doesn't work either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淑女气质 2025-02-19 05:03:52

我的问题是,显示存储在DateTime属性中的值的最简单方法是什么?

如果要在DataTable中显示Datatime类型数据,则只需要直接显示。例如:

模型:

public class CustomerModel{
    public DateTime StartTime { get; set; }
}

操作:

public IActionResult Shift()
        {
            List<CustomerModel> l = new List<CustomerModel> { new CustomerModel { StartTime = new DateTime(2022, 7, 1) }, new CustomerModel {  StartTime = new DateTime(2022, 7, 1) } };

            return new JsonResult(l);
        }

JS:

$(document).ready(function () {

    
    $('#DT_load').DataTable({
        "ajax": {
            "url": "/api/Shift",
            "type": "GET",
            "datatype":"json"
        },
        "columns": [
            { "data": "startTime", "width": "25%" },
        ],
        "width":"100%"
    });
});

My question is, what would be the simplest way to display the values stored in the DateTime properties?

If you want to show datatime type data in datatable,you only need to show it directly.For example:

model:

public class CustomerModel{
    public DateTime StartTime { get; set; }
}

action:

public IActionResult Shift()
        {
            List<CustomerModel> l = new List<CustomerModel> { new CustomerModel { StartTime = new DateTime(2022, 7, 1) }, new CustomerModel {  StartTime = new DateTime(2022, 7, 1) } };

            return new JsonResult(l);
        }

js:

$(document).ready(function () {

    
    $('#DT_load').DataTable({
        "ajax": {
            "url": "/api/Shift",
            "type": "GET",
            "datatype":"json"
        },
        "columns": [
            { "data": "startTime", "width": "25%" },
        ],
        "width":"100%"
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文