简体   繁体   中英

Rearranging TR position using the TD ID (Work only in the first element)

I'm rearranging a table using only javascript, each TD has a ID that I compare with the rowIndex of the Table.

The problem is when I run the code, only the first Element work, you can see in the images below, what's happening ?

let trs =  document.querySelectorAll(".tr-row");
let tds = document.querySelectorAll(".td-descr");
let tbodyAtivo = document.querySelector("#tbody-ativo");

tds.forEach((td) =>{
    trs.forEach((tr) => {
              let trPai = td.closest("tr") //tr of each element   
              if (tr.rowIndex == td.id) //rowIndex
                tbodyAtivo.insertBefore(trPai,tbodyAtivo[tr.rowIndex]); //insert de new element

    })
})

在此处输入图片说明

在此处输入图片说明

I followed the recommendation of @adelriosantiago, i founded the answer using order by case.

 SELECT codigo,classificacao,descricao,
ABS(SUM(CASE WHEN MONTH(periodo) IN (1) THEN IFNULL(saldo_anterior,NULL) ELSE  NULL END)) AS saldo_anterior,
ABS(SUM(CASE WHEN MONTH(periodo) IN (1) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m1,
ABS(SUM(CASE WHEN MONTH(periodo) IN (2) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m2,
ABS(SUM(CASE WHEN MONTH(periodo) IN (3) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m3,
ABS(SUM(CASE WHEN MONTH(periodo) IN (1,2,3) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS t1,
ABS(SUM(CASE WHEN MONTH(periodo) IN (4) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m4,
ABS(SUM(CASE WHEN MONTH(periodo) IN (5) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m5,
ABS(SUM(CASE WHEN MONTH(periodo) IN (6) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m6,
ABS(SUM(CASE WHEN MONTH(periodo) IN (4,5,6) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS t2,
ABS(SUM(CASE WHEN MONTH(periodo) IN (7) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m7,
ABS(SUM(CASE WHEN MONTH(periodo) IN (8) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m8,
ABS(SUM(CASE WHEN MONTH(periodo) IN (9) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m9,
ABS(SUM(CASE WHEN MONTH(periodo) IN (7,8,9) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS t3,
ABS(SUM(CASE WHEN MONTH(periodo) IN (10) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m10,
ABS(SUM(CASE WHEN MONTH(periodo) IN (11) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m11,
ABS(SUM(CASE WHEN MONTH(periodo) IN (12) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS m12,
ABS(SUM(CASE WHEN MONTH(periodo) IN (10,11,12) THEN IFNULL(credito-debito,NULL) ELSE  NULL END)) AS t4,
ABS(SUM(IFNULL(debito-credito,NULL))) AS acumulado
FROM tbl_balancete
WHERE cod_empresa = '557' -- COD IGUAL AO COD INFORMADO NO INPUT
AND YEAR(periodo) = 2019 -- ANO IGUAL AO ANO INFORMADO NO INPUT
AND 
(
classificacao IN ('1.1.01.02','1.1.02.01','1.1.07.01','1.1.10.01','1.2.02.01','1.2.03.01')
OR 
codigo IN ('35','126','273','2002','189','1890','2345','2849','2030','1890','189','4427','3031','2695','4428','6185')
)
GROUP BY codigo, classificacao, descricao
ORDER BY CASE `codigo`
 WHEN '35'   THEN 1
 WHEN '42'   THEN 2
 WHEN '2625' THEN 3
 WHEN '126'  THEN 4
 WHEN '273'  THEN 5
 WHEN '2002' THEN 6
 WHEN '189'  THEN 7
 WHEN '1890' THEN 8
 WHEN '2345' THEN 9
 WHEN '2849' THEN 10
 WHEN '210'  THEN 11
 ELSE 12
 END ASC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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