JSFiddle - React, Tailwind, and code Playground

by riaanc

HTML

-1,-2147483647,AssignedTo,-2147483640,-2147483647,-2147483647,0,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
-2,-2147483646,Channel,-2147483640,-2147483646,-2147483646,0,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
-3,-2147483645,CreatedBy,-2147483640,-2147483645,-2147483645,0,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
-4,-2147483644,Usershometerritory,-2147483640,-2147483644,-2147483644,0,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
-5,-2147483643,Usersparentterritory,-2147483640,-2147483643,-2147483643,0,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
-6,-2147483642,Userssiblingterritories,-2147483640,-2147483642,-2147483642,0,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
-7,-2147483641,Children,-2147483640,-2147483641,-2147483641,0,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
1,-2147483640,Worldwide,0,2147483647,2141356163,1,0,NULL,NULL,65,2013-08-20 15:52:32.000,2013-08-20 15:52:32.000,NULL,408498,-2147483640
24,-2147483639,South Africa,-2147483640,2141356162,1074047746,3,1,1,2008-01-03 15:00:37.000,28,2016-12-06 11:28:25.000,2016-12-06 11:28:25.000,NULL,82100647,-2147483639
25,-2147483638,Dr Morton and Partners,-2147483639,909846449,535338849,26,2,1,2008-01-03 15:02:41.000,65,2016-03-29 18:04:51.000,2016-03-29 18:04:51.000,NULL,124835867,-2147483638
28,-2147483637,Cape Town Medi-clinic,-2147483638,-2049453012,-2147483636,0,3,1,2008-01-03 15:04:11.000,1,2008-01-03 15:04:11.000,2008-01-03 15:04:11.000,NULL,6126914,-2147483637
29,-2049453011,Wynberg Broad Road Medical Centre,-2147483638,-1951422386,-2049453010,0,3,1,2008-01-03 15:04:39.000,1,2008-01-03 15:04:39.000,2008-01-03 15:04:39.000,NULL,6126914,-2047163251
30,-1951422385,Gatesville Medical Centre,-2147483638,-1853391760,-1951422384,0,3,1,2008-01-03 15:05:00.000,1,2008-01-03 15:05:00.000,2008-01-03 15:05:00.000,NULL,6126914,-1946842865
31,-1853391759,Vincent Pallotti Hospital,-2147483638,-1755361134,-1853391758,0,3,1,2008-01-03 15:05:22.000,1,2008-01-03 15:05:22.000,2008-01-03...

CSS

public partial class Form1 : Form
    {
        public IEnumerable<Territory> GetCsv()
        {
            var lines = System.IO.File.ReadAllLines(@"c:\temp\delete\territories.csv");
            var items = lines.Select(l => {
                var columns = l.Split(',');
                return new Territory
                {
                    PrimaryKey = int.Parse(columns[0]),
                    TerritoryID = int.Parse(columns[1]),
                    DisplayName = columns[2],
                    ParentTerritoryID = int.Parse(columns[3]),
                    RangeEnd = int.Parse(columns[4]),
                    RangeStart = int.Parse(columns[5]),
                    ChildCount = int.Parse(columns[6]),
                    Depth = int.Parse(columns[7]),
                    Display = columns[14] != "NULL"
                };
                }).OrderBy(r => r.RangeEnd).ToList();

            return items.Where(i => i.Display == true).ToList();

        }

        public void AddTerritory(Territory aParent, IEnumerable<Territory> aRecords)
        {
            var childRecords = aRecords.Where(r => r.ParentTerritoryID == aParent.TerritoryID);
            foreach (var cr in childRecords)
            {
                aParent.Children.Add(cr);
                AddTerritory(cr, aRecords);
            }
                    
        }

        public string MakeString(Territory aParent, int aDepth)
        {
            var str = "";
            str += new String(' ', aDepth * 4) + aParent.DisplayName+ "\r\n";
            foreach (var child in aParent.Children)
                str += MakeString(child, aDepth + 1);

            return str;
        }


        public Form1()
        {
            InitializeComponent();
            var records = GetCsv();

            var parents = records.Where(i => i.ParentTerritoryID == 0);
            
            foreach (var parent in parents)
            {
                AddTerritory(parent, records);
                textBox1.Text...

JavaScript

[
  {
    "terr_territoryid": -1069146751,
    "terr_caption": "Bellville",
    "terr_parentid": -2147483638,
    "terr_rangeend": -971116126
  },
  {
    "terr_territoryid": -2147483637,
    "terr_caption": "Cape Town Medi-clinic",
    "terr_parentid": -2147483638,
    "terr_rangeend": -2049453012
  },
  {
    "terr_territoryid": 991947098,
    "terr_caption": "Cape Town MRI",
    "terr_parentid": -2147483639,
    "terr_rangeend": 1074047745
  },
  {
    "terr_territoryid": 160831245,
    "terr_caption": "Century City",
    "terr_parentid": -2147483638,
    "terr_rangeend": 285667112
  },
  {
    "terr_territoryid": -1657330507,
    "terr_caption": "Chris Barnard Memorial Hospital",
    "terr_parentid": -2147483638,
    "terr_rangeend": -1559299882
  },
  {
    "terr_territoryid": -873085499,
    "terr_caption": "Claremont Hospital",
    "terr_parentid": -2147483638,
    "terr_rangeend": -775054874
  },
  {
    "terr_territoryid": -2147483638,
    "terr_caption": "Dr Morton and Partners",
    "terr_parentid": -2147483639,
    "terr_rangeend": 909846449
  },
  {
    "terr_territoryid": -1461269255,
    "terr_caption": "Fountain Medical Centre",
    "terr_parentid": -2147483638,
    "terr_rangeend": -1363238630
  },
  {
    "terr_territoryid": -1951422385,
    "terr_caption": "Gatesville Medical Centre",
    "terr_parentid": -2147483638,
    "terr_rangeend": -1853391760
  },
  {
    "terr_territoryid": -1559299881,
    "terr_caption": "Goodwood",
    "terr_parentid": -2147483638,
    "terr_rangeend": -1461269256
  },
  {
    "terr_territoryid": -1363238629,
    "terr_caption": "Kenilworth Medicross",
    "terr_parentid": -2147483638,
    "terr_rangeend": -1265208004
  },
  {
    "terr_territoryid": -186871117,
    "terr_caption": "Khayelitsha Medicross",
    "terr_parentid": -2147483638,
    "terr_rangeend": -88840492
  },
  {
    "terr_territoryid": 909846450,
    "terr_caption": "KINGSBURY RADIOLOGY TUFT JV",
    "terr_parentid": -2147483639,
   ...