简体   繁体   English

表在特定列(MVC)中未正确排序

[英]Table not sorting properly in specific column (MVC)

Quick problem: 快速问题:

I've used some javascript i found to sort all my tables by clicking their header, and this works absolutely fine in most cases. 我使用了一些JavaScript,通过单击它们的表头对所有表进行排序,在大多数情况下,这绝对可以正常工作。 Strings, ints, etc all get sorted properly all across the page when tagged appropriately. 正确标记了字符串,整型等所有内容后,整页都可以正确排序。

            function sortTable(n) {

        var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0; //declares necessary variables

        table = document.getElementById("toSortTable"); 

        switching = true; 

        dir = "asc"; 

        while (switching) { 

            switching = false; 
            rows = table.getElementsByTagName("tr"); 

            for (i = 1; i < (rows.length - 1) ; i++) { 

                shouldSwitch = false; 

                x = rows[i].getElementsByTagName("td")[n]; 
                y = rows[i + 1].getElementsByTagName("td")[n]; 

                if (dir == "asc") { 

                    if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) { //compares the 2 values
                        shouldSwitch = true; /
                        break;
                    }

                } else if (dir == "desc") {
                    if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) { 
                        shouldSwitch = true; 
                        break;
                    }
                }
            }
            if (shouldSwitch) { 
                rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
                switching = true;
                switchcount++; 
            } else {
                if (switchcount == 0 && dir == "asc") {
                    dir = "desc";
                    switching = true;
                }
            }
        }
    }

As said, this JS seems to work on most pages i assign it, but in one table it gives me some issues. 如前所述,这个JS似乎可以在我为其分配的大多数页面上运行,但是在一张表中却给了我一些问题。 On one of my tables the sorting simply won't provide the right order. 在我的一张桌子上,排序根本无法提供正确的顺序。

https://imgur.com/a/7EZ5y (don't know how to embed this) https://imgur.com/a/7EZ5y (不知道如何嵌入)

The table has been clicked (ie sorted) in both images by total billing cost. 已按总帐单费用在两个图片中点击(即排序)了表格。 I read that botched sorting can occur when one of the values contains a whitespace, so the field gets recognized as a string instead of dec/int, but this was not the case in the DB, as well as the weird entry showing up in the middle and not before / after the rest of the ints/decs. 我读到,当其中一个值包含空格时,可能会发生拙劣排序,因此该字段将被识别为字符串而不是dec / int,但这在DB中不是这种情况,并且在中间,而不是在其余int / decs之前/之后。

The only thing i can imagine being off is my controller that populates my VM: 我唯一能想到的就是关闭填充我的VM的控制器:

            public ActionResult Index()
    {
        List<InvoiceVM> listOfInvoices = new List<InvoiceVM>();
        var iNVOICES = db.INVOICES.Include(i => i.INVOICES_TO_CREDIT);



        foreach (var entry in iNVOICES)
        {
            bool check = false;
            foreach (var checkDupe in listOfInvoices)
            {
                if (checkDupe.invoiceNo == entry.INV_NO)
                {
                    check = true;
                }
            }
            if (check == true)
            {
                check = false;
            }
            else
            {
                InvoiceVM toAdd = new InvoiceVM();
                toAdd.account = entry.ACCOUNT;
                toAdd.subDate = entry.SUB_DATE;
                toAdd.cc = entry.CC;
                toAdd.reference = entry.SOLUTION_OWNER;
                toAdd.invoiceNo = entry.INV_NO;
                toAdd.project = entry.PROJECT_ID;
                toAdd.ikeaProject = entry.SLTN_NAME;
                toAdd.invoiceNo = entry.INV_NO;
                toAdd.autoNo = Convert.ToInt32(entry.AUTO_ID);

                foreach (var invoice in iNVOICES)
                {
                    if (invoice.INV_NO == toAdd.invoiceNo)
                    {
                        toAdd.totalCost += Convert.ToInt32(invoice.EMP_TOTAL_COST);
                    }
                }

                listOfInvoices.Add(toAdd);

            }

        }
        return View(listOfInvoices);

    }

(I know this could probably be solved via some super simple GroupBy + Sum making this code look super trash, happy to take that suggestion if anyone wants to pitch it in). (我知道可以通过一些超级简单的GroupBy + Sum使此代码看起来超级垃圾来解决,如果有人愿意加入此建议,我们很乐意接受)。

Anyway, this populates properly, as the values displayed in the pictures above are the proper sums of values in the actual Invoice table. 无论如何,由于上面图片中显示的值是实际“发票”表中值的正确总和,因此这会正确填充。 Also, the individual values on the "Total Cost" are higher for the 42k, than the 11k (in case the sorting used an original value instead of the summed up value). 同样,“总成本”上的单个值对于42k高于11k(在排序使用原始值而不是总和的情况下)。 Both of these values have been combined in the controller by other values, but this doesn't feel relevant once the values have been passed to a VM. 这两个值已在控制器中与其他值组合在一起,但是一旦将这些值传递给VM,就感觉不相关。

I'd look for more answers to this, and I'm sure the mistake is somewhere in my controller, but I just have no idea what to look for. 我会寻找更多答案,而且我确定错误是在控制器中的某个地方,但我根本不知道要寻找什么。 The sorting works perfectly in all other tables, and even works perfectly fine in that table on the same page, for all columns but the Total Cost one. 除了“总成本”之外,对于所有列,该排序在所有其他表中均能很好地工作,甚至在同一页的该表中也能很好地工作。 Any help would be appreciated. 任何帮助,将不胜感激。

Thanks a lot! 非常感谢!

Are you absolutely certain this script sorts the numeric values correctly in other tables? 您是否绝对可以确定此脚本在其他表中正确地对数值进行了排序? You did mention that as long as everything is "tagged" appropriately, but what does this mean? 您确实提到只要适当地“标记”了所有内容,但这意味着什么? Are you somehow able to specify that specific table data cells are numbers only? 您是否能够以某种方式指定特定的表格数据单元仅是数字?

As for something being wrong in controller, I doubt that is the case here. 至于控制器中有什么问题,我怀疑情况就是这样。 The controller does feed the view with the data but the sorting is entirely down to the javascript. 控制器确实向视图提供数据,但排序完全取决于javascript。

It is of course possible to do the sorting in controller, you can view an example here . 当然可以在控制器中进行排序,您可以在此处查看示例。

Down to business now though, here your script retrieves the values from specified table cells: 现在开始讨论,这里您的脚本从指定的表格单元中检索值:

x = rows[i].getElementsByTagName("td")[n]; 
y = rows[i + 1].getElementsByTagName("td")[n]; 

and here is one of the comparisons: 这是比较之一:

x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()

I don't think retrieving the innerHTML and changing it to lower case is able to tell a difference between a number and a string. 我不认为检索innerHTML并将其更改为小写字母能够分辨出数字和字符串之间的区别。 I could be wrong, but I did do some simple testing and it coped well with alphabetical sorting. 我可能是错的,但我确实做了一些简单的测试,因此可以很好地应对字母排序。 However with numbers it considered 1002 to be smaller than 14, for example. 但是,例如使用数字,它认为1002小于14。

You could try adding some more conditional checking if the x and y variables are numbers or strings then save them to new numeric variables and then do the sorting. 您可以尝试添加更多条件检查,以检查x和y变量是数字还是字符串,然后将它们保存到新的数字变量中,然后进行排序。

Or you can try alternative sorting solution: DataTables . 或者,您可以尝试使用其他排序解决方案: DataTables It is quite nifty a package, although jQuery based. 尽管是基于jQuery的,但它是一个非常漂亮的软件包。 (not in any way affiliated with them, I just it myself). (不以任何方式隶属于他们,我就是我自己)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM