开发者

Display tabularized data easily?

开发者 https://www.devze.com 2023-03-15 05:52 出处:网络
I am trying to come up with a light weight template for generating a table to represent a list of model objects and display specified fields as columns. So far this is what I have come up with, with a

I am trying to come up with a light weight template for generating a table to represent a list of model objects and display specified fields as columns. So far this is what I have come up with, with a few annoying issues.

DataTable.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace Models
{
    public interface IDataTable
    {
        List<string> ColumnNames { get; }

        List<List<string>> TableRows { get; }
    }

    public class DataTable<T> : IDataTable
    {
        private Type TableType = typeof(T);

        public List<string> Columns { get; set; }

        public List<T> RowData { get; set; }

        public List<string> ColumnNames 
        {
            get
            {
                List<string> columnNames = new List<string>();
                foreach (string colPropName in Columns)
                    columnNames.Add(GetPropertyDisplayName(colPropName));
                return columnNames;
            }
        }

        public List<List<string>> TableRows
        {
            get
            {
                List<List<string>> tableRows = new List<List<string>>();
                foreach (T rowObj in RowData)
                {
                    List<string> tableRow = new List<string>();
                    foreach (string propName in Columns)
                    {
                        object value = TableType.GetProperty(propName).GetValue(rowObj, null);
                        if (value != null && value != String.Empty)
                            tableRow.Add(value.ToString());
                        else
                            tableRow.Add("N/A");
                    }
                    tableRows.Add(tableRow);
                }
                return tableRows;
            }
        }

        public DataTable(List<string> columns, List<T> rowData)
        {
            Columns = columns;
            RowData = rowData;
        }

        private string GetPropertyDisplayName(string propName)
        {
            DisplayNameAttribute attrib = TableType.GetProperty(propName).GetCustomAttributes(true).OfType<DisplayNameAttribute>().FirstOrDefault();
            if (attrib != null)
                return attrib.DisplayName;
            return propName;
        }
    }
}

Shared/DataTable.cshtml:

@model IDataTable

<table class="rounded-corner-table" summary="2007 Major IT Companies' Profit">
    <thead>
        <tr>
            @foreach (string colName in Model.ColumnNames)
            {
                <th scope="col">@colName</th>
            }
        </tr>
    </thead>
        <tfoot>
        <tr>
            <td colspan="@(Model.ColumnNames.Count - 1)" class="rounded-foot-left"><em>To be implemented: Instant search.</em></td>
            <td class="rounded-foot-right">&nbsp;</td>
        </tr>
    </tfoot>
    <tbody>
        @for (int i = 0; i < Model.TableRows.Count; ++i)
        {
            <tr>
                @foreach (string colValue in Model.TableRows[i])
                {
                    <td>@colValue</td>
                }
            </tr>
        }
    </tbody>
</table>

How I use Them:

public ActionResult List()
        {
            return PartialView(new DataTable<Administrator>(new List<string> { "EmailAddress", 
                "FirstName", 
                "LastName", 
                "Date" }, 
                Root.DataContext.Administrators.ToList()));
        }
//_______________________________________________________________________________
@model DataTable<Administrator>

@{Html.RenderPartial("DataTable");}

The two main problems I am having with this is I would prefer to be able to call Html.DisplayForModel() but do not know how to make it work and that it is not displaying the DisplayName attributes for the column names in the table. Can someone please offer me some advice on these issues?

Thanks, Alex.

UPDATE - Fixed my second issue:

private string GetPropertyDisplayName(string propName)
        {
            DisplayNameAttribute attrib = TableType.GetProperty(propName).GetCustomAttributes(true).OfType<DisplayNameAttribute>().FirstOrDefault();
            if (attrib == null)
            {
                MetadataTypeAttribute metaAttrib = TableType.GetCustomAttributes(true).OfType<MetadataTypeAttribute>().FirstOrDefault();
                if (metaAttrib 开发者_开发百科!= null)
                    attrib = metaAttrib.MetadataClassType.GetProperty(propName).GetCustomAttributes(true).OfType<DisplayNameAttribute>().FirstOrDefault();
            }

            if (attrib != null)
                return attrib.DisplayName;

            return propName;
        }


I reckon you are pretty much there. It looks pretty good.

What about setting up a tabular display template, as per Phil Haack's blog here?

Then you can call it like this Html.DisplayForModel("TableTemplateName")

0

精彩评论

暂无评论...
验证码 换一张
取 消