c# How to generate a tournament bracket HTML table

前端 未结 2 1494
谎友^
谎友^ 2021-02-03 14:19

so i have been stuck with this issue for 3 weeks now and i couldn\'t for the life of me figure it out. what im trying to do is to get this kind of output/presentation using tabl

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-03 14:21

    We can implement this using System.Web.UI.WebControls.Table by dynamically adding table rows and table cells.

    Class File

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// 
    /// Summary description for Matches
    /// 
    
        public class Rounds
        {
            public int RoundNumber{get;set;}
            public List Matches{get;set;}
    
            public Rounds(int number, List matches)
            {
                this.RoundNumber = number;
                this.Matches = matches;
            }
        }
    
        public class Match
        {
            public int MatchId{get;set;}
            public Team Team1{get;set;}
            public Team Team2 {get; set;}
            public Team WinningTeam { get; set; }
    
            public Match(int id, Team t1, Team t2) :this (id, t1, t2, null)
            {
    
            }
    
            public Match(int id, Team t1, Team t2, Team t3)
            {
                this.MatchId = id;
                this.Team1 = t1;
                this.Team2 = t2;
                this.WinningTeam = t3;
            }
        }
    
        public class Team
        {
            public int TeamId {get;set;}
            public string TeamName {get;set;}
    
            public Team(int id, string name)
            {
                this.TeamId = id;
                this.TeamName = name;
            }
        }
    

    .aspx.cs file - We need to add a div control in .aspx file.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Test : System.Web.UI.Page
    {
        List rounds = new List();
        protected void Page_Load(object sender, EventArgs e)
        {
            ShowRoundMatchesUsingTable();
        }
    
        private List GetRoundMatchesDetails()
        {
            List teamList = new List();
            teamList.Add(new Team(1, "Arcenal"));
            teamList.Add(new Team(2, "Barsa"));
            teamList.Add(new Team(3, "Manchester"));
            teamList.Add(new Team(4, "Black Burn"));
            teamList.Add(new Team(5, "Ferrari"));
            teamList.Add(new Team(6, "Adidas"));
            teamList.Add(new Team(7, "Reebock"));
            teamList.Add(new Team(8, "Nike"));
    
            List matchList1 = new List();
            matchList1.Add(new Match(1, teamList.Find(lst => lst.TeamName == "Arcenal"), teamList.Find(lst => lst.TeamName == "Barsa")));
            matchList1.Add(new Match(1, teamList.Find(lst => lst.TeamName == "Manchester"), teamList.Find(lst => lst.TeamName == "Black Burn")));
            matchList1.Add(new Match(1, teamList.Find(lst => lst.TeamName == "Ferrari"), teamList.Find(lst => lst.TeamName == "Adidas")));
            matchList1.Add(new Match(1, teamList.Find(lst => lst.TeamName == "Reebock"), teamList.Find(lst => lst.TeamName == "Nike")));
    
            List matchList2 = new List();
            matchList2.Add(new Match(2, teamList.Find(lst => lst.TeamName == "Arcenal"), teamList.Find(lst => lst.TeamName == "Manchester")));
            matchList2.Add(new Match(2, teamList.Find(lst => lst.TeamName == "Adidas"), teamList.Find(lst => lst.TeamName == "Nike")));
    
            List rounds = new List();
    
            rounds.Add(new Rounds(1, matchList1));
            rounds.Add(new Rounds(2, matchList2));
    
            return rounds;
        }
    
        private void ShowRoundMatchesUsingTable()
        {
            IEnumerable roundsList = GetRoundMatchesDetails();
    
            if (roundsList == null || roundsList.Count() == 0) return;
    
            Table tbl = new Table();
    
            TableRow trHeaderRow = new TableRow();
            TableRow trDetailRow = new TableRow();
            TableCell tcDetails = new TableCell();
    
            foreach (Rounds round in roundsList)
            {
                TableHeaderCell th = new TableHeaderCell();
                th.Text = "Round : " + round.RoundNumber ;
                trHeaderRow.Cells.Add(th);
    
                if (round.Matches != null && round.Matches.Count > 0)
                {
                    tcDetails = new TableCell();
                    trDetailRow.Cells.Add(tcDetails);
                }
    
                foreach (Match m in round.Matches)
                {
                    Table dtlTable = new Table();
                    tcDetails.Controls.Add(dtlTable);
    
                    TableRow tr1 = new TableRow();
                    TableCell tc = new TableCell();
                    tc.Text = m.Team1.TeamName;
                    tr1.Cells.Add(tc);
                    dtlTable.Rows.Add(tr1);
    
                    tr1 = new TableRow();
                    tc = new TableCell();
                    tc.Text = "Vs";
                    tr1.Cells.Add(tc);
                    dtlTable.Rows.Add(tr1);
    
                    tr1 = new TableRow();
                    tc = new TableCell();
                    tc.Text = m.Team2.TeamName;
                    tr1.Cells.Add(tc);
                    dtlTable.Rows.Add(tr1);
                }
            }
    
            tbl.Rows.Add(trHeaderRow);
            tbl.Rows.Add(trDetailRow);
            div.Controls.Add(tbl);
        }
    }
    

提交回复
热议问题