c# - 具有五个数字级别的分层大纲 - 如何插入兄弟或子行并调整现有记录?

标签 c# oop document hierarchy outlining

我有一个带有“点”(或大纲)层次结构的表: 字段是 L1 L2 L3 L4 L5 (L = level)

例如:

1.0.0.0.0
1.1.0.0.0
1.1.1.0.0
1.1.2.0.0
1.2.0.0.0

如果我想在 1.1.1.0.0 插入一个同级,我应该得到一个新行 1.1.2.0.0 - 并且已经存在的 1.1.2.0.0 应该调整到 1.1.3.0.0,等等.

如果我想插入一个 child 1.1.1.0.0,我应该得到一个新行 1.1.1.1.0,不需要调整,因为在该级别不存在 sibling 。

我已经为此创建了程序代码 - 但它正在变成意大利面条 - 我想要一个 OOP 解决方案,其中包含一个处理这些插入和调整的类。

任何人都可以推荐伪代码来处理这两种类型的插入和对现有“行”的必要调整吗?

如有任何帮助或建议,我们将不胜感激!

最佳答案

我认为给您评论的人并没有真正理解这个问题。您已经有了一个表,因此使用 LinkedList 只会做一个表。您确实需要向该方法传递要插入的行和要插入的字段。仅添加值为 1.1.1.0.0 的新行并不能提供足够的信息来重新编号。

下面的代码我使用了一个数据表,每列都有一个字段。为了简化代码,我假设索引是整数。代码不是很复杂

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Outlining outling = new Outlining();
            outling.Add(2,2);
            outling.Add(0, 2);
            outling.Add(5, 2);
        }
    }
    public class Outlining
    {
        public DataTable dt = null;

        public Outlining()
        {
            dt = new DataTable();
            dt.Columns.Add("L1", typeof(int));
            dt.Columns.Add("L2", typeof(int));
            dt.Columns.Add("L3", typeof(int));
            dt.Columns.Add("L4", typeof(int));
            dt.Columns.Add("L5", typeof(int));

            dt.Rows.Add(new object[] { 1, 0, 0, 0, 0 });
            dt.Rows.Add(new object[] { 1, 1, 0, 0, 0 });
            dt.Rows.Add(new object[] { 1, 1, 1, 0, 0 });
            dt.Rows.Add(new object[] { 1, 2, 0, 0, 0 });
        }
        public void Add(int at, int level)
        {
            DataRow newRow = dt.Rows.Add();
            if (at < dt.Rows.Count - 1)
            {
                //move row if not last row
                dt.Rows.Remove(newRow);
                dt.Rows.InsertAt(newRow, at);
            }
            newRow.BeginEdit();
            newRow.ItemArray = dt.Rows[at + 1].ItemArray.Select(x => (object)x).ToArray();
            newRow.EndEdit();

            Renumber(at, level);
        }
        public void Renumber(int rowInsertIndex, int level)
        {
            for (int row = rowInsertIndex; row < dt.Rows.Count - 1; row++)
            {
                Boolean match = true;
                //check if columns to left still match, if no we are done
                for (int i = 0; i < level - 1; i++)
                {
                    if (dt.Rows[i][level] != dt.Rows[i + 1][level])
                    {
                        match = false;
                        break;
                    }
                }
                if (!match) break;
                dt.Rows[row + 1][level] = ((int)(dt.Rows[row + 1][level])) + 1;
            }
        }

    }
}

关于c# - 具有五个数字级别的分层大纲 - 如何插入兄弟或子行并调整现有记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54842617/

相关文章:

python - 三重继承导致元类冲突......有时

javascript - 客户端 JavaScript,创建多个元素并尽快显示每个元素

rotation - jsPDF文档/页面旋转

c# - 将数组传递给可变长度参数列表

c# - 实现存储库模式的正确方法是什么?以及如何使用它?

c# - 扩展(而不是更改)新行为的搜索类?

java - 覆盖具有不同变量名称的方法

java - 使用 XPath 处理 XML 文件生成的路径时出错

c# - .NET Core 中 Microsoft Fakes 的任何替代方案?

c# - 有哪些调试超时的好方法? (C#)