Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Добавление сообщения при удалении последнего объекта со страницы html (https://javascript.ru/forum/misc/81586-dobavlenie-soobshheniya-pri-udalenii-poslednego-obekta-so-stranicy-html.html)

mateorich 19.12.2020 18:38

Добавление сообщения при удалении последнего объекта со страницы html
 
Всем привет. Мне нужно добавить вывод сообщения "Список студентов пуст" после удаления последнего студента на странице html. Собственно я понимаю, что нужно добавить проверку по типу следующей:
if (!this.students.lenght){
****
},else{};

Но как по итогу проверки добавить в пустую таблицу нужное сообщение? Ниже мой код а который нужно добавить проверку.
function Student(){
 this.students = [
  { name: "Ivan", estimate: 4, course: 1, active: true },
  { name: "Petr", estimate: 3, course: 1, active: false },
  { name: "Alex", estimate: 2, course: 4, active: false },
  { name: "Max", estimate: 5, course: 2, active: true },
  { name: "Anton", estimate: 2, course: 3, active: true },
  { name: "Roman", estimate: 3, course: 2, active: false },
  { name: "Vladimir", estimate: 2, course: 4, active: true },
  { name: "Oleg", estimate: 5, course: 3, active: true },
  { name: "Evgeniy", estimate: 5, course: 5, active: true },
  { name: "Pavel", estimate: 3, course: 5, active: true },
  { name: "Danil", estimate: 2, course: 3, active: true },
  { name: "Denis", estimate: 4, course: 4, active: false },
];
}
Student.prototype.render = function(){
let tbody = document.querySelector(".students tbody");
tbody.innerHTML = "";

let self = this;

for (i = 0; i < this.students.length; i++){

  let tr = document.createElement("TR");

  if(this.students[i].estimate < 4){
      tr.className = "bad";
  }else if(this.students[i].estimate === 4 ){
      tr.className = "ts";
  }else if(this.students[i].estimate > 4 ){
    tr.className = "good";
}
  let td = document.createElement("TD");
   td.innerHTML = this.students[i].name;
   tr.appendChild(td);

   td = document.createElement("TD");
   td.innerHTML = this.students[i].estimate;
   tr.appendChild(td);

   td = document.createElement("TD");
   td.innerHTML = this.students[i].course;
   tr.appendChild(td);

   td = document.createElement("TD");
   td.innerHTML = this.students[i].active;
   tr.appendChild(td);

   td = document.createElement("TD");
     td.innerHTML = "Удалить";
     td.addEventListener("click", (function(index) {
         return function(event){
          self.remove(index); 
          self.render();
        };
    })(i));
    tr.appendChild(td);
   tbody.appendChild(tr)
}
};

Student.prototype.remove = function(index){
  let self = this;
  this.students.splice(index, 1);
  self.render();
};
let s = new Student();
s.render();
s.remove();

рони 19.12.2020 18:50

Цитата:

Сообщение от mateorich
if (this.students.lenght){
// строка 23
}else{tbody.innerHTML = "<tr><td>...>"};

:-?

mateorich 19.12.2020 18:55

Т.е. выходит вот так?
if (this.students.lenght){
for (i = 0; i < this.students.length; i++)
},else{tbody.innerHTML = "<tr><td>Список студентов пуст>"};

рони 19.12.2020 18:59

Цитата:

Сообщение от mateorich
Т.е. выходит вот так?

почти, запятая только лишняя, а можно просто после цикла добавить
в строку 61
this.students.length || (tbody.innerHTML = "<tr><td colspan=5>Список студентов пуст</td></tr>");

mateorich 19.12.2020 19:02

Спасибо большое


Часовой пояс GMT +3, время: 03:19.