简体   繁体   English

将值输出到HTML表

[英]Outputting values to an HTML table

I know there has to be a cleaner, more concise way to do this... but I'm not getting anywhere trying to Google for it. 我知道必须有一种更简洁,更简洁的方法来进行此操作...但是我没有办法尝试使用Google。 Basically, I have a set of data that I need to write to an HTML table. 基本上,我有一组需要写入HTML表的数据。

The table comes out looking something like this (but much longer): 桌子出来看起来像这样(但更长):

DOB            SDLW                  SDLY                     WTD             LWTD
Start Date: 03/06/2013  02/27/2013            03/07/2012               03/04/2013      02/25/2013
End Date:                                                              03/06/2013      02/27/2013
Net Sales     5,661.47   4,876.82   16.1%       5,765.33    -1.8%       13,941.10       13,310.09     4.7%
Gross Sales   6,499.14   5,549.94   17.1%       6,602.71    -1.6%       16,046.50       15,306.56     4.8%
Guest Count     369.00     317.00   16.4%         392.00    -5.9%          920.00          872.00     5.5%
Total Comps     187.29     102.46   82.8%         163.19     4.8%          499.74          455.82     9.6%
Total Voids      56.25      51.05   10.2%         131.65   -57.3%          161.05          227.83   -29.3%

The by-line issue: 副刊问题:

Originally I had written this using an HtmlTextWriter , but having to call AddAttribute() for each <td> made an incredible amount of (noisy) code. 最初,我是使用HtmlTextWriter编写此代码的,但是必须为每个<td>调用AddAttribute()导致大量(嘈杂的)代码。 I then moved to wrappers, so I can least reduce a cell to a single line of code: 然后,我转到了包装器,因此我至少可以将单元格简化为一行代码:

    public string wrapAmntCell(Object input)
    {
        return "<td align=\"right\">" + string.Format("{0:n}", input) + "&nbsp;" + "</td>";
    }

Which I don't particularly like still, but I've at least significantly improved where I started. 我仍然不特别喜欢 ,但是我至少从开始的地方有了明显的进步。 However... 然而...

The bigger picture issue: 更大的问题:

Is that I'm building these rows by concatenating strings of values that are built with += statements (cringe), something like this: 我是通过串联用+ =语句(临界)构建的值字符串来构建这些行的,类似这样:

string[] finalNet = new string[validSites.Count];
string[] finalGross = new string[validSites.Count];
string[] finalGC = new string[validSites.Count];
string[] finalComp = new string[validSites.Count];
string[] finalVoid = new string[validSites.Count];

foreach (string num in validSites)
{
    //SDLY
    foreach (DataStore sdlyTmp in sdly)
    {
        if (sdlyTmp.siteNumber.Equals(num))
        {
            finalNet[countTD] += wrapAmntCell(sdlyTmp.netSales);
            finalGross[countTD] += wrapAmntCell(sdlyTmp.grossSales);
            finalGC[countTD] += wrapAmntCell(sdlyTmp.guestCount);
            finalComp[countTD] += wrapAmntCell(sdlyTmp.compTotal);
            finalVoid[countTD] += wrapAmntCell(sdlyTmp.voidTotal);

            tmpCol03[0] = sdlyTmp.netSales;
            tmpCol03[1] = sdlyTmp.grossSales;
            tmpCol03[2] = sdlyTmp.guestCount;
            tmpCol03[3] = sdlyTmp.compTotal;
            tmpCol03[4] = sdlyTmp.voidTotal;

            //percentage diff
            finalNet[countTD] += wrapPctCell(getPctDiff(tmpCol01[0], tmpCol03[0]));
            finalGross[countTD] += wrapPctCell(getPctDiff(tmpCol01[1], tmpCol03[1]));
            finalGC[countTD] += wrapPctCell(getPctDiff(tmpCol01[2], tmpCol03[2]));
            finalComp[countTD] += wrapPctCell(getPctDiff(tmpCol01[3], tmpCol03[3]));
            finalVoid[countTD] += wrapPctCell(getPctDiff(tmpCol01[4], tmpCol03[4]));
            //end diffs

            totalsCol03[0] += sdlyTmp.netSales;
            totalsCol03[1] += sdlyTmp.grossSales;
            totalsCol03[2] += sdlyTmp.guestCount;
            totalsCol03[3] += sdlyTmp.compTotal;
            totalsCol03[4] += sdlyTmp.voidTotal;
        }
    }
}

(in writing that, I realize I could probably combine my inner foreach and the if statement nested in it using some .NET 3.5 magic; I'm still learning, and would appreciate guidance on that as well) (在编写该代码时,我意识到我可以使用一些.NET 3.5魔术将我的内部foreach和嵌套在其中的if语句结合起来;我仍在学习,并且也希望能对此进行指导)

Where those Col arrays are used for percentage diffs. 这些Col数组用于百分比差异的地方。

At the end of all of my loops I end up with five arrays that contain full rows, from <tr>...</tr> (19 cells of data per line), which I then loop back over foreach site again (as the information is stored positionally, Store1's data is located at finalNet[0], finalGross[0], etc) and write them to an HtmlTextWriter . 在所有循环的结尾,我最终得到五个数组,这些数组包含来自<tr>...</tr> (每行19个数据单元)的完整行,然后我再次循环遍历foreach站点(如信息存储在位置,Store1的数据位于finalNet [0],finalGross [0]等),并将它们HtmlTextWriter

Part of my issue is that my actual data is stored vertically (using an object that contains a full column for one date - these values are pulled from SQL queries based on the date in the column header) - whereas it has to be output horizontally in the table. 我的问题的部分原因是我的实际数据是垂直存储的(使用的对象包含一个日期的完整列-这些值是根据列标题中的日期从SQL查询中提取的)-而必须水平输出桌子。

However, I'm having issues figuring out how to clean up this code and keep the format of my table intact. 但是,我在弄清楚如何清理此代码并保持表格式完整时遇到了问题。 Any thoughts would be greatly appreciated. 任何想法将不胜感激。

I would suggest you use a view engine such as Razor . 我建议您使用诸如Razor之类的视图引擎。 The project is open source and works outside of the ASP.Net runtime. 该项目是开源的,并且可以在ASP.Net运行时之外运行。 This way you could separate your presentation (the generation of the HTML table) from your data generation. 这样,您可以将演示文稿(HTML表的生成)与数据生成分开。 In this way you could write code along the lines of the following. 这样,您可以按照以下内容编写代码。 Assuming that your data from your SQL queries is column orientated (as you suggested) and is a collection named data 假设您来自SQL查询的数据是面向列的(如您建议的那样),并且是一个名为data的集合

C# code in your executable: 可执行文件中的C#代码:

var data = ...;

var template = File.ReadAllText("path/to/razor/template.cshtml");
var html = Razor.Parse(template,data); //Bind the SQL data to the HTML

Razor template example: 剃刀模板示例:

<table>
    @foreach(var row in Model) {
        <tr>
            @foreach(var col in row) {
                <td>@col</td>
            }
        </tr>
    }
</table>

Without knowing more of your code, it's a little difficult to give you a better example, but I think that this should lead you in the right direction 在不了解更多代码的情况下,为您提供一个更好的示例会有些困难,但是我认为这应该引导您朝着正确的方向前进

I am totally agree with "Reddog". 我完全同意“ Reddog”。 You should use gridview control and Pass your Data object as datasource to gridview. 您应该使用gridview控件,并将Data对象作为数据源传递到gridview。 Also you can use JQGrid for client side scripting. 您也可以使用JQGrid进行客户端脚本编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM