排序表功能仅工作一次

时间:2018-11-02 00:02:41

标签: javascript html

我有一个用HTML编写的足球联赛表格,每当我使用下拉列表和输入系统模拟比赛时,该表格都会更新。该系统运行良好,并更新了联赛表中相应球队的价值。
问题在于,更新表中的这些值时,将调用对表进行排序的功能,但它根本不起作用。该功能如下:

function SortTable()
        {
            var table, rows, switching, i, x, y, shouldSwitch;
            table = document.getElementById("liga123");
            switching = true;

            /* Make a loop that will continue until no switching has been done */
            while (switching)
            {
                /* Start with no switching */
                switching = false;
                rows = table.rows;

                /* Loop through all table rows except the first one, which is the heading */
                for (i = 1; i < (rows.length - 1); i++)
                {
                    /* Start saying there should be not switching */
                    shouldSwitch = false;

                    /* Get 2 elements to compare, one from current row and another from the next one */
                    x = rows[i].getElementsByTagName("TD")[9];
                    y = rows[i + 1].getElementsByTagName("TD")[9];

                    /* Check if those 2 rows should switch */
                    if (x.innerHTML() < y.innerHTML())
                    {
                        /* Switch, restore the hierarchy by points and break */
                        shouldSwitch = true;
                        x = rows[i].getElementsByTagName("TD")[9];
                        y = rows[i + 1].getElementsByTagName("TD")[9];
                        break;
                    }
                    if (x.innerHTML() == y.innerHTML())
                    {
                        /* Change the hierarchy to Goals Difference */
                        x = rows[i].getElementsByTagName("TD")[8];
                        y = rows[i + 1].getElementsByTagName("TD")[8];

                        if (x.innerHTML() < y.innerHTML())
                        {
                            /* Switch, restore the hierarchy by points and break */
                            shouldSwitch = true;
                            x = rows[i].getElementsByTagName("TD")[9];
                            y = rows[i + 1].getElementsByTagName("TD")[9];
                            break;
                        }
                        if (x.innerHTML() == y.innerHTML())
                        {
                            /* Change the hierarchy to Goals in Favour */
                            x = rows[i].getElementsByTagName("TD")[6];
                            y = rows[i + 1].getElementsByTagName("TD")[6];

                            if (x.innerHTML() < y.innerHTML())
                            {
                                /* Switch, restore the hierarchy by points and break */
                                shouldSwitch = true;
                                x = rows[i].getElementsByTagName("TD")[9];
                                y = rows[i + 1].getElementsByTagName("TD")[9];
                                break;
                            }
                            if (x.innerHTML() == y.innerHTML())
                            {
                                /* Change the hierarchy to Victories */
                                x = rows[i].getElementsByTagName("TD")[3];
                                y = rows[i + 1].getElementsByTagName("TD")[3];

                                if (x.innerHTML() < y.innerHTML())
                                {
                                    /* Switch, restore the hierarchy by points and break */
                                    shouldSwitch = true;
                                    x = rows[i].getElementsByTagName("TD")[9];
                                    y = rows[i + 1].getElementsByTagName("TD")[9];
                                    break;
                                }
                                if (x.innerHTML() == y.innerHTML())
                                {
                                    /* Change the hierarchy to Loses */
                                    x = rows[i].getElementsByTagName("TD")[5];
                                    y = rows[i + 1].getElementsByTagName("TD")[5];

                                    if (x.innerHTML() > y.innerHTML())
                                    {
                                        /* Switch, restore the hierarchy by points and break */
                                        shouldSwitch = true;
                                        x = rows[i].getElementsByTagName("TD")[9];
                                        y = rows[i + 1].getElementsByTagName("TD")[9];
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                if (shouldSwitch)
                {
                /* If a change must be done, do it and mark it */
                rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
                switching = true;
                }
            }
        }

然后,当调用该函数时,它应该遍历表中的所有行,并首先按更多点进行排序。
该“层级”系统的工作方式如下:如果2个团队的得分相同,则按目标差异排序。如果两支球队的进球差相同,则按有利的进球排序;如果还不够,则按胜利排序,最后按失败排序。
我的问题是该函数在每次匹配后都可以工作,但是没有排序。我在做什么不好?我没有发布排名表,因为它们大约有300行,但是如果需要的话,我会这样做。
顺便说一句,我的代码具有西班牙语的所有ID和注释,所以也许我忘了编辑其中一些

0 个答案:

没有答案
相关问题