如何在每天的列标题中显示日期和总数?

huangapple go评论49阅读模式
英文:

how can I show the date in a columns header every day with total?

问题

To display the date in a column and show each day's data horizontally in your MVC view, you can pivot the data in your controller and modify the view accordingly. Here's how you can achieve this:

Controller:

public ActionResult SamplesSttat()
{
    int HospitalId = Convert.ToInt32(Session["HospitalID"]);
    var sTATISTICS_DAILY = db.STATISTICS_DAILY.Include(s => s.stat_Departments)
        .Include(s => s.Hospital).Where(x => x.hospital_no == HospitalId)
        .Include(s => s.sys_users1).OrderByDescending(stat => stat.DATE).ToList();
    
    // Pivot the data
    var pivotData = sTATISTICS_DAILY.GroupBy(stat => stat.stat_Departments.Dept_name)
        .Select(group => new
        {
            Department = group.Key,
            Data = group.Select(stat => new
            {
                Date = stat.DATE.ToString("dd/MM/yyyy"),
                MorningSamples = stat.SAMPLES_MORNING,
                EveningSamples = stat.SAMPLES_EVENING
            })
        }).ToList();

    ViewBag.PivotData = pivotData;
    return View();
}

View:

@model IEnumerable<warehouse.Models.STATISTICS_DAILY>

@{
    ViewBag.Title = "SamplesSttat";
    Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
}

<h2>Statistic</h2>

<table class="table">
    <tr>
        <th>
            Department
        </th>
        @foreach (var item in ViewBag.PivotData.First().Data)
        {
            <th>
                @item.Date
            </th>
        }
    </tr>

    @foreach (var pivotItem in ViewBag.PivotData)
    {
        <tr>
            <td>
                @pivotItem.Department
            </td>
            @foreach (var dataItem in pivotItem.Data)
            {
                <td>
                    @dataItem.MorningSamples || @dataItem.EveningSamples
                </td>
            }
        </tr>
    }
</table>

This code will pivot the data so that each date becomes a column, and the view will display the data as you described, with the date in a column and each day's data shown horizontally.

英文:

I need to make the statistic page in MVC how to make the controller and view to be like this output and show the date in the column with every day for example :

department     day1||(SAMPLES_MORNING) ,  day1 || (SAMPLES_EVENING) , day2 || (SAMPLES_MORNING) , day2 || (SAMPLES_EVENING) , .......

如何在每天的列标题中显示日期和总数?

this is the model class :

    public partial class STATISTICS_DAILY
    {
        public int ID { get; set; }
        public Nullable&lt;int&gt; DEPT_ID { get; set; }
        public Nullable&lt;int&gt; SAMPLES_MORNING { get; set; }
        public Nullable&lt;int&gt; SAMPLES_EVENING { get; set; }
        public Nullable&lt;System.DateTime&gt; DATE { get; set; }
        public Nullable&lt;int&gt; hospital_no { get; set; }
        public Nullable&lt;int&gt; user_id { get; set; }
    
        public virtual stat_Departments stat_Departments { get; set; }
        public virtual sys_users sys_users { get; set; }
        public virtual Hospital Hospital { get; set; }
        public virtual sys_users sys_users1 { get; set; }
    }
}

in the statistic 1M means first date morning total number of samples and 1E means first date evening total number of samples
2M means second date morning total number of samples and 2E means second date evening total number of samples and so on till last day

I tried this is the controller :

 public ActionResult SamplesSttat()
        {
            
            int HospitalId = Convert.ToInt32(Session[&quot;HospitalID&quot;]);
            var sTATISTICS_DAILY = db.STATISTICS_DAILY.Include(s =&gt; s.stat_Departments)
                .Include(s =&gt; s.Hospital).Where(x =&gt; x.hospital_no == HospitalId)
                .Include(s =&gt; s.sys_users1);
            return View(sTATISTICS_DAILY.OrderByDescending(stat =&gt; stat.DATE).ToList());
        } 

and this is the view :

@model IEnumerable&lt;warehouse.Models.STATISTICS_DAILY&gt;

@{
    ViewBag.Title = &quot;SamplesSttat&quot;;
    Layout = &quot;~/Views/Shared/_LayoutDashboard.cshtml&quot;;
}

&lt;h2&gt;Statistic&lt;/h2&gt;


&lt;table class=&quot;table&quot;&gt;
    &lt;tr&gt;
        &lt;th&gt;
            @Html.DisplayNameFor(model =&gt; model.stat_Departments.Dept_name)
        &lt;/th&gt;
        &lt;th&gt;
            @Html.DisplayNameFor(model =&gt; model.SAMPLES_MORNING)
        &lt;/th&gt;
        &lt;th&gt;
            @Html.DisplayNameFor(model =&gt; model.SAMPLES_EVENING)
        &lt;/th&gt;
       
        &lt;th&gt;
            @Html.DisplayNameFor(model =&gt; model.DATE)
        &lt;/th&gt;
       
       
        &lt;th&gt;&lt;/th&gt;
    &lt;/tr&gt;

@foreach (var item in Model) {
    &lt;tr&gt;
        &lt;td&gt;
            @Html.DisplayFor(modelItem =&gt; item.stat_Departments.Dept_name)
        &lt;/td&gt;
        &lt;td&gt;
            @Html.DisplayFor(modelItem =&gt; item.SAMPLES_MORNING)
        &lt;/td&gt;
        &lt;td&gt;
            @Html.DisplayFor(modelItem =&gt; item.SAMPLES_EVENING)
        &lt;/td&gt;
    
        &lt;td&gt;
            @Html.DisplayFor(modelItem =&gt; item.DATE)
        &lt;/td&gt;
       
       
       
       
    &lt;/tr&gt;
}

&lt;/table&gt;

This is the current output view :

如何在每天的列标题中显示日期和总数?

How can I put the date column up and view each day in a column not in the rows like the statistic

答案1

得分: 0

我用不同的方法解决了它

我做了一个水晶报表的交叉表报告,这种报告可以进行统计

此外,如果我找到方法,我将尝试在MVC控制器和视图中完成它,我会发布它

谢谢

英文:

I solved it in different way

I did a crystal report cross-tab report this kind of report will do such statistics

Also I will try to do it in MVC controller and view if I find the way I will post it

Thanks

huangapple
  • 本文由 发表于 2023年5月18日 05:17:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定