Нужна критика (( учусь (
window.onload = init;
Конструктор
var Team = function (name, game, wins, draws, defeat, goals_scored, goals_against, difference, glasses, id) {
this.name = name;
this.game = game;
this.wins = wins;
this.draws = draws;
this.defeat = defeat;
this.goals_scored = goals_scored;
this.goals_against = goals_against;
this.difference = difference;
this.glasses = glasses;
this.id = id;
};
Object.prototype.add = function () {
var tr = document.getElementById(this.id),
id = this,
meth = [];
for (var i = 0; i < tr.children.length; i++){
тут должен быть калькулятор расчета матчей
tr.children[i].onclick = function (x) {
alert(x);
alert(i);
}
}
}
запуска сохраньоных команд
function init() {
var teamArray, team, team_JSON;
teamArray = localStorage['team'];
if(teamArray) {
team = localStorage.getItem('team');
team_parse = JSON.parse(team);
for(var i = 0; i < team_parse.length; i++){
createDOM(team_parse[i]);
}
} else {
teamArray = [],
team_JSON = JSON.stringify(teamArray);
localStorage.setItem('team', team_JSON);
}
}
Создаэт команду.
function createObj() {
var localSto, team, team_JSON, obj, team_parse,
id = new Date().getTime(),
obj = new Team(prompt(), 0, 0, 0, 0, 0, 0, 0, 0, id);
if(obj.name != null && obj.name != ''){
localSto = localStorage.getItem('team');
team_parse = JSON.parse(localSto);
team_parse.push(obj);
team_JSON = JSON.stringify(team_parse);
localStorage.setItem('team', team_JSON);
createDOM(obj);
}
}
ДОМ
function createDOM(team) {
var table = document.getElementById('table'),
tr = document.createElement('tr'),
td = document.createElement('td'),
li = document.createElement('li');
tr.setAttribute('id', team.id);
tr.setAttribute('onclick', 'count('+team.id+');');
li.setAttribute('onclick', 'delet('+team.id+');');
table.appendChild(tr);
ul.appendChild(li);
li.innerHTML = team.name+' удалить';
for (var i in team) {
if(team.hasOwnProperty(i)) {
tr.appendChild(td.cloneNode(true));
tr.children[fn_1()].innerHTML = team[i]
}
}
function fn_1 (value) {
if(!fn_1.anwers) fn_1.anwers = {};
if(fn_1.anwers[value] != null && fn_1.anwers[value] < 10){
fn_1.anwers[value]+=1;
return fn_1.anwers[value];
}
return fn_1.anwers[value] = 0;
}
}
Удаления
function delet (remov) {
var local = localStorage.getItem('team'),
j = JSON.parse(local),
mas = [],
str = String(remov),
tr = document.getElementById(str),
yes = confirm('Удалить?')
if(yes) {
tr.parentNode.removeChild(tr);
for (var i = 0; i < j.length; i++){
if(j[i].id != remov){
mas.push(j[i]);
}
}
var team_JSON = JSON.stringify(mas);
localStorage.setItem('team', team_JSON);
}
}
Запись в таблицу.
function count(tt) {
var local = localStorage.getItem('team'),
team = JSON.parse(local),
team_length = team.length
for (var i = 0; i < team_length; i++){
if(team[i].id == tt) {
team[i].add();
}
}
}