jquery selector?

JennyL

Bekanntes Mitglied
Vielleicht könnt ihr mir weiterhelfen...
Ich möchte alle Zeilen einer Tabelle auswählen,
anschließend möchte ich alle td's jeder Zeile auswählen, die direkte Kinder der Zeile sind,
anschließend möchte ich den vorletzten und letzten Wert jeder Zeile addieren,
und alle Zeilen absteigend nach dieser Summe sortieren.
Das habe ich bis jetzt:
Javascript:
        let trs = $('.tableclazz > tbody:nth-child(1) > tr');
        let a = [];
        for (let i = 0; i < trs.length; i++) {
            const e = trs[i];
            const i1 = parseInt(e.find('> td')[9].textContent);
            const i2 = parseInt(e.find('> td')[10].textContent);
            a.push([e, i1 + i2]);
        }
        //...
e.find funktioniert aber nich
 
Könnte auch so funktionieren:
Javascript:
const nl = document.querySelectorAll('.tableclazz > tbody:nth-child(1) > tr')
const arr = Array.prototype.map.call(nl, tr => {
    const i1 = parseInt(tr.find('> td')[9].textContent);
    const i2 = parseInt(tr.find('> td')[10].textContent);
    return [tr, i1+i2]
});
 
@mihe7 hab vielen Dank, aber es funktioniert so auch ganz gut:
Javascript:
        let trs = $('.tableclazz > tbody:nth-child(1) > tr');
        let a = [];
        for (let i = 1; i < trs.length; i++) {
            const e = trs[i];
            const i1 = parseInt($(e).find('> td')[6].textContent);
            const i2 = parseInt($(e).find('> td')[7].textContent);
            const i3 = parseInt($(e).find('> td')[8].textContent);
            a.push([e, i1 + i2 + i3]);
        }
        a.sort(function (a, b) {
            return b[1] - a[1];
        });
        for (let i = 1; i < trs.length; i++) {
            const e = trs[i];
            e.outerHTML = a[i - 1][0].outerHTML;
        }
hier sind es die letzten 3 Werte. Deines ist so kompliziert, das verstehe ich gar nicht...
 
Es gibt sogar noch einen Hack, den ich aber nicht ausprobiert habe: const i1 = parseInt($(e + ' > td')[6].textContent);
Cool oder? 🙂
 

Zurück
Oben