【JavaScript】动态导入数据合并单元格的方法

先将数据库数据进行排序(order by,可以根据多个字段进行排序,具有优先级,第二个字段会在第一个字段相同的基础上完成排序),这样具有相同值得数据就会放在一起。
然后还用相同的list方法传入前台,传给前台成功以后,调用自己定义的合并单元格的方法。
如果要合并多列,需要定义多个方法,按照优先级,优先级低的排序时候应该校验优先级高的数据是否相同。
并且调用时候应该先排序优先级低的。

//合并相同数据的单元格
            function hebingRows1(col){
                var trs = $( "table tr" );
                var rows = 1;
                for ( var i=trs.length;i>0;i--){
                    var cur = $($(trs[i]).find( "td" )[col]).text();
                    var next = $($(trs[i-1]).find( "td" )[col]).text();
                    if (cur==next){
                        rows++;
                        $($(trs[i]).find( "td" )[col]).remove();
                    } else {
                        $($(trs[i]).find( "td" )[col]).attr( "rowspan" ,rows);
                        rows=1;
                    }
                }
            }
            function hebingRows2(col){
                var trs = $( "table tr" );
                var rows = 1;
                for ( var i=trs.length;i>0;i--){
                    var cur = $($(trs[i]).find( "td" )[col]).text();
                    var next = $($(trs[i-1]).find( "td" )[col]).text();
                    var company1 = $($(trs[i]).find( "td" )[1]).text();
                    var company2 = $($(trs[i-1]).find( "td" )[1]).text();
                    if (cur == next&&company1 == company2){
                        rows++;
                        $($(trs[i]).find( "td" )[col]).remove();
                    } else {
                        $($(trs[i]).find( "td" )[col]).attr( "rowspan" ,rows);
                        rows=1;
                    }
                }
            }
            function hebingRows3(col){
                var trs = $( "table tr" );
                var rows = 1;
                for ( var i=trs.length;i>0;i--){
                    var cur = $($(trs[i]).find( "td" )[col]).text();
                    var next = $($(trs[i-1]).find( "td" )[col]).text();
                    var company1 = $($(trs[i]).find( "td" )[1]).text();
                    var company2 = $($(trs[i-1]).find( "td" )[1]).text();
                    var mainproduct1 = $($(trs[i]).find( "td" )[2]).text();
                    var mainproduct2 = $($(trs[i-1]).find( "td" )[2]).text();
                    if (cur == next&&company1 == company2&&mainproduct1==mainproduct2){
                        rows++;
                        $($(trs[i]).find( "td" )[col]).remove();
                    } else {
                        $($(trs[i]).find( "td" )[col]).attr( "rowspan" ,rows);
                        rows=1;
                    }
                }
            }

            hebingRows3(3);
            hebingRows2(2);
            hebingRows1(1);

你可能感兴趣的:(JavaScript)