以嵌套方式显示评论,如Gmail评论 [英] Display Comment in Nested Way like Gmail Comment

查看:122
本文介绍了以嵌套方式显示评论,如Gmail评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表注释,看起来像这样,添加了一些模型内容:

I have a table comments, thats look like this, added some mockup content as well:

+------------+---------+----------+-------------------+------------------------------------+---------------------------+
| comment_id | user_id | post_id  | comment_parent_id |          comment_content           | comment_creation_datetime |
+------------+---------+----------+-------------------+------------------------------------+---------------------------+
|         26 |       1 |    16329 |                 0 | Första                             | 2016-01-24 10:42:49       |
|         27 |       1 |    16329 |                26 | Svar till första                   | 2016-01-24 10:42:55       |
|         28 |       1 |    16329 |                26 | Andra svar till förta              | 2016-01-24 10:43:06       |
|         29 |       1 |    16329 |                28 | Svar till "andra svar till första" | 2016-01-24 10:43:23       |
+------------+---------+----------+-------------------+------------------------------------+---------------------------+

我想显示评论Reddit样式,如下图所示:

Im trying to display the comments Reddit style, like this image:

即使我不明白如何开始从哪里开始...
i尝试googling找到与我的要求相关的文章,但我没有找到它。
任何帮助...
我发布图像在下面的评论,因为我没有声誉指向后图像谢谢

Even i don't understand how to start where to start... i tried googling to find article which related to my requirement but i didn't find it. Any Help... i'm posting Image in below comments because i don't have reputation point to post image thank you

推荐答案

假设你有一个类注释

public class Comment
{
    public int comment_id { get; set; }
    public int user_id { get; set; }
    public int post_id { get; set; }
    public int comment_parent_id { get; set; }
    public string comment_content { get; set; }
    public DateTime comment_creation_datetime { get; set; }

    public Comment(int comment_id, int user_id, int post_id, int comment_parent_id, string comment_content, DateTime comment_creation_datetime)
    {
        this.comment_id = comment_id;
        this.user_id = user_id;
        this.post_id = post_id;
        this.comment_parent_id = comment_parent_id;
        this.comment_content = comment_content;
        this.comment_creation_datetime = comment_creation_datetime;
    }

    public override string ToString()
    {
        return string.Format("{0} {1} {2}", this.comment_id, this.comment_content, this.comment_creation_datetime);
    }
}

填充以下示例值

List<Comment> cList = new List<Comment>();
cList.Add(new Comment(26, 1, 16329, 0, "Första  ", new DateTime(2016, 01, 24, 10, 42, 49)));
cList.Add(new Comment(27, 1, 16329, 26, "Svar till första", new DateTime(2016, 01, 24, 10, 42, 55)));
cList.Add(new Comment(28, 1, 16329, 26, "Andra svar till förta", new DateTime(2016, 01, 24, 10, 43, 06)));
cList.Add(new Comment(29, 1, 16329, 28, "Svar till andra svar till första", new DateTime(2016, 01, 24, 10, 43, 23)));

您需要一个方法来对项目进行排序

you need a method so sort the items

public static IEnumerable<Comment> Sort(List<Comment> SortItemsList, int parentID = 0)
{
    foreach (var item in SortItemsList.Where(x => x.comment_parent_id == parentID).OrderBy(x => x.comment_id).ToList())
    {
        yield return item;
        foreach (var child in Sort(SortItemsList, item.comment_id))
        {
            yield return child;
        }
    }
}

每个项目的深度

public static int GetDepth(List<Comment> cList, Comment cItem)
{
    if (cItem.comment_parent_id == 0)
    {
        return 0;
    }
    else
    {
        return GetDepth(cList, cList.Where(x => x.comment_id == cItem.comment_parent_id).Single()) + 1;
    }
}

示例:

foreach (Comment cItem in Sort( cList))
{
    Console.WriteLine(GetDepth(cList, cItem) + " | " + cItem.ToString());
}






嵌套处理此处描述的注释。

这篇关于以嵌套方式显示评论,如Gmail评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆