hmm: Erster Versuch den Code Aufzuräumen

Beitrag lesen

Hallo Leute,

CSS auslagern würde heißen, dass ich das setAttribute aligne aus dem JS Code rausnehme und in die CSS Datei schreibe?

Hier mein überarbeiteter Tabellencode:

            function createTableFromJson(data) {
                data_global = data;
                
                var skillHeader = document.createElement("tr");
                var attributeHeader = document.createElement("tr");
                
                setPlatzhalterInRow(4, skillHeader, "td");
                setPlatzhalterInRow(4, attributeHeader, "td");
                
                for(var i = 0; i < data[0].skills.length; i++) {
                    setPlatzhalterInRow(1, skillHeader, "td");
                    
                    if(i > 0) {
                        setPlatzhalterInRow(1, skillHeader, "td");
                    }
                    
                    setElementInRow(skillHeader, "th", data[0].skills[i].skill);
                    setElementInRow(attributeHeader, "td", "Wissen");
                    setElementInRow(attributeHeader, "td", "Erfahrung");
                    setElementInRow(attributeHeader, "td", "Interesse");
                }
                document.getElementsByTagName("table")[0].appendChild(skillHeader);
                document.getElementsByTagName("table")[0].appendChild(attributeHeader);
                document.getElementsByTagName("table")[0].setAttribute("align", "center");
                
                for(var i = 0; i < data.length; i++) {
                    var line = document.createElement("tr");
                    
                    setElementWithAttributeInRow(line, "input", "Checkbox", "", "Checkbox", ["name"], [data[i].personenId]);
                    setElementWithAttributeInRow(line, "td", "", "", data[i].personenId, ["contenteditable"], [false]);
                    setElementWithAttributeInRow(line, "td", "", "", data[i].vorname, ["contenteditable", "name"], [true, i + "," + "vorname"]);
                    setElementWithAttributeInRow(line, "td", "", "", data[i].nachname, ["contenteditable", "name"], [true, i + "," + "nachname"]);
                    
                    for(var k = 0; k < data[i].skills.length; k++) {
                        setElementWithAttributeInRow(line, "td", "", "", data[i].nachname, ["contenteditable", "name"], [true, i + "," + "skills," + k + ",wissen"]);
                        setElementWithAttributeInRow(line, "td", "", "", data[i].nachname, ["contenteditable", "name"], [true, i + "," + "skills," + k + ",erfahrung"]);
                        setElementWithAttributeInRow(line, "td", "", "", data[i].nachname, ["contenteditable", "name"], [true, i + "," + "skills," + k + ",interesse"]);
                    }
                    
                    document.getElementsByTagName("table")[0].appendChild(line);
                }
            };
            
            function setElementWithAttributeInRow(line, elementType, type, innerHtml, className, attributeNameArray, attributeValueArray) {
            	var element = document.createElement(elementType);
            	element.type = type;
            	element.className = className;
            	element.innerHTML = innerHtml;
            	
            	for(var i = 0; i < attributeNameArray.length; i++) {
            		element.setAttribute(attributeNameArray[i], attributeValueArray[i]);
            	}
            	
            	line.appendChild(element);
            	return line;
            };
            
            function setElementInRow(line, elementType, innerHtml) {
            	var element = document.createElement(elementType);
            	element.innerHTML = innerHtml;
            	line.appendChild(element);
            	return line;
            };
            
            function setPlatzhalterInRow(anzahlPlatzhalter, line, platzhalterType) {
            	for(var i = 0; i < anzahlPlatzhalter; i++) {
            		line.appendChild(document.createElement(platzhalterType));
            	}	
            	return line;
            };

was wären die nächsten Schritte zur Verbesserung des Codes?