0

Current code is working fine. However column headers are missing. In current example NRO, NRO1, SNAME are column headers.

I understand that <th> should be added to existing method but have hard times figuring out how?

  <tr>
    <th>NRO</th>
    <th>NRO1</th>
    <th>SNAME</th>
  </tr>

Here is List to HTML table method (found on this website):

    public static string GetMyTable<T>(IEnumerable<T> list, params Func<T, object>[] fxns)
    {

        StringBuilder sb = new StringBuilder();
        sb.Append("<table>\n");
        foreach (var item in list)
        {
            sb.Append("<tr>\n");
            foreach (var fxn in fxns)
            {
                sb.Append("<td>");
                sb.Append(fxn(item));
                sb.Append("</td>");
            }
            sb.Append("</tr>\n");
        }
        sb.Append("</table>");

        return sb.ToString();
    }

I use it like this:

    var HTML = GetMyTable(duplicates, x => x.NRO, x => x.NRO1, x => x.SNAME);

I have tried to do it like this:

    public static string GetMyTable<T>(IEnumerable<T> list, params Func<T, object>[] fxns)
    {

        StringBuilder sb = new StringBuilder();
        sb.Append("<table>\n");
        sb.Append("<tr>\n");
        sb.Append("<th>NRO</th>\n");
        sb.Append("<th>NRO1</th>\n");
        sb.Append("<th>SNAME</th>\n");
        sb.Append("</tr>\n");
        foreach (var item in list)
        {
            sb.Append("<tr>\n");
            foreach (var fxn in fxns)
            {
                sb.Append("<td>");
                sb.Append(fxn(item));
                sb.Append("</td>");
            }
            sb.Append("</tr>\n");
        }
        sb.Append("</table>");

        return sb.ToString();
    }

Is it possible to make a loop for headers so there will be no need to assign them for each table separately?

2 Answers 2

1

How about the following code, if I understand you correctly.

public static string GetMyTable<T>(IEnumerable<T> list,
    params Func<T, (object, string)>[] fxns)
{
    var sb = new StringBuilder();
    sb.Append("<table>\n");
    sb.Append("<tr>\n");

    foreach (var fxn in fxns)
        sb.Append("<th>").Append(fxn(default).Item2).AppendLine("</th>");

    sb.Append("</tr>\n");

    foreach (var item in list)
    {
        sb.Append("<tr>\n");
        foreach (var fxn in fxns)
        {
            sb.Append("<td>");
            sb.Append(fxn(item).Item1);
            sb.Append("</td>");
        }
        sb.Append("\n</tr>\n");
    }
    sb.Append("</table>");

    return sb.ToString();
}

Use it like this:

var html = GetMyTable(duplicates,
    x => (x?.NRO, nameof(x.NRO)),
    x => (x?.NRO1, nameof(x.NRO1)),
    x => (x?.SNAME, nameof(x.SNAME))
);
Sign up to request clarification or add additional context in comments.

Comments

1

you are adding the headers inside the loop, just create them once, when you create your table tag.

    public static string GetMyTable<T>(IEnumerable<T> list, params Func<T, object>[] fxns)
    {

        StringBuilder sb = new StringBuilder();
        sb.Append("<table>\n");
        // this needs to be outside the loop
        sb.Append("<tr>\n");
        sb.Append("<th>NRO</th>\n");
        sb.Append("<th>NRO1</th>\n");
        sb.Append("<th>SNAME</th>\n");
        sb.Append("</tr>\n");
        foreach (var item in list)
        {

            sb.Append("<tr>\n");
            foreach (var fxn in fxns)
            {
                sb.Append("<td>");
                sb.Append(fxn(item));
                sb.Append("</td>");
            }
            sb.Append("</tr>\n");
        }
        sb.Append("</table>");

        return sb.ToString();
    }

1 Comment

Thank you for this! I have figured it out already. However this means that this method will not work for any table as headers are specified in method itself. Is it possible to create a loop for headers as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.