从不谈论填充普通视图 [英] Never talking about populate a normal view

查看:57
本文介绍了从不谈论填充普通视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听到了很多有关填充桌子的信息.当我在google上查询时,有很多关于如何执行该操作的文章.但是,让我们谈谈填充视图!
是的,您没听错!我认为这是每个人第一次做的最简单,最正常的方式.

I am hearing so much about populating a table. When I look it up at google there is a whole universe of post how to do that. But let's talk about populating a view!
Yes, you heard right! I think it's such as the simplest and most normal way everybody would do the first time.

我要创建一个列表!在这个简单的清单中,我想显示句子.看起来像这样: https://www.dropbox.com/s /swm1n9ezled0ppd/img.jpg?dl=0
我的想法是,每个div都使用div,并带有标题,依此类推.但是我将如何在javascript的帮助下填充该div?最重要的是如何将div的标题设置为不同的值?

I want to create a list! In this simple list, I want to show sentences. Something looks like this: https://www.dropbox.com/s/swm1n9ezled0ppd/img.jpg?dl=0
And my idea was it to use for every single item a div, with a title in it and so on. But how would I populate that div with help of javascript? And the most important thing how to set the titles of the divs to a different value?

如何通过JavaScript做到这一点,或者还有我不知道的另一种更好的方法?

How to do that via JavaScript or is there another much better way i did not know?

db.collection("classes").doc(data.readGrades).get().then(function(doc) {
      if (doc.exists) {
        console.log("Document data:", doc.data());
        const data = doc.data();
        const members = data.members;
        var permission_table = document.getElementById("noten_tabelle_permission");
        var permission_table_container = document.getElementById("main_padding");

        members.forEach(el => {
          console.log(el)

          table_number++;

          clone = permission_table.cloneNode(true); // true means clone all childNodes and all event handlers
          clone.id = "layout_table" + table_number;
          permission_table_container.appendChild(clone);

          db.collection("users").doc(el).collection("grades").get().then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
                console.log(doc.id, " => ", doc.data());
                const data = doc.data();
                //addToTable("grade_table" + table_number, doc.id, data.mdl, data.klu);
                //window.alert("Table name: " + ["grade_table" + table_number])
                //____________________________________________________________________________

                const html = fillTemplate("grade_table" + table_number, "test", data.mdl, data.klu);

                // Join the array of html and add it as the `innerHTML` of `main`
                document.getElementById("main_padding").insertAdjacentHTML('beforeend', html);

                addToTable("grade_table" + table_number, doc.id, data.mdl, data.klu);

            });
          });
        })

      } else {
        console.log("No such document!");
      }
    }).catch(function(error) {
      console.log("Error getting document:", error);
    });

这是您编写的函数:

function fillTemplate({ table_name, id, subject, mdl, klu }) {
  return `
  <div class="noten_tabelle_permission" id="noten_tabelle_permission">
    <h1 id="member_name">${id}</h1>
    <table id="${table_name}" style="width:100%">
      <tr>
        <th>Fach</th>
        <th>mündlich</th>
        <th>Klausur</th>
      </tr>
      <!-- Make content with js code -->
    </table>
  </div>
  `;
}  

这里还有另一个功能:

function addToTable(table_name, subject, mdl, klu) {

  var subject_name = getSubjectByNumber(subject);
  var short_subject = getSubjectShortByNumber(subject);

      //Zeile erstellen

      var y = document.createElement([short_subject]);
      y.setAttribute("id", [short_subject]);
      document.getElementById([table_name]).appendChild(y);

      //Spalten in einer Zeile

      var y = document.createElement("TR");
      y.setAttribute("id", [short_subject]);

      //Spalten in einer Zeile

      var cE = document.createElement("TD");
      var tE = document.createTextNode([subject_name]);
      cE.appendChild(tE);
      y.appendChild(cE);

      var a = document.createElement("TD");
      var b = document.createTextNode([mdl]);
      a.appendChild(b);
      y.appendChild(a);

      var c = document.createElement("TD");
      var d = document.createTextNode([klu]);
      c.appendChild(d);
      y.appendChild(c);


      document.getElementById(table_name).appendChild(y);
}  

这里是HTML:

<div class='main_has_permission' id="main_has_permission" style="display: none;">
            <div class='main_padding' id="main_padding">

              <div class="noten_tabelle_permission" id="noten_tabelle_permission">
                <h1 id="member_name"></h1>
                <table id="grades_table" style="width:100%">
                  <tr>
                    <th>Fach</th>
                    <th>mündlich</th>
                    <th>Klausur</th>
                  </tr>
                  <!-- Make content with js code -->
                </table>
              </div>
            </div>
          </div>

先谢谢了. 〜卡尔

推荐答案

好,所以我有点无聊:)所以我准备了一个示例来帮助您.它包含:

OK, so I was a little bored :) so I cooked up an example to help you out. It contains:

1)包含您的数据的对象数组.每个对象都有标题,类型流派和年​​份.

1) An array of objects containing your data. Each object has a title, an array of genres, and a year.

2)它使用 map 遍历数据数组并为每个对象调用fillTemplate.

3)fillTemplate返回完整的模板文字使用每个数组对象的数据.注意:由于genre是流派的 array ,因此我们使用join将数组元素连接到一个列表中.

3) fillTemplate returns a completed template literal using each array object's data. Note: because genre is an array of genres we use join to join the array elements together into a list.

4)它使用 CSS flex模型设置HTML样式.

4) It uses the CSS flex model to style the HTML.

const data = [{ title: 'Mad Max: Fury Road', genre: ['Action & Adventure'], year: 2015 }, { title: 'Inside Out', genre: ['Animation', 'Kids & Family'], year: 2015 }, { title: 'Star Wars: Episode VII - The Force Awakens', genre: ['Action'], year: 2015 }];

function fillTemplate({ title, genre, year }) {
  return `
    <section class="panel">
      <div class="left">
        <div class="title">${title}</div>
        <ul class="genres">
          <li class="genre">${genre.join('</li><li class="genre">')}</li>
        </ul>
      </div>
      <div class="year">${year}</div>
    </section>
  `;
}

// For each array object call `fillTemplate` which returns a new
// array element of HTML 
const html = data.map(obj => fillTemplate(obj));

// Join the array of html and add it as the `innerHTML` of `main`
document.querySelector('main').innerHTML = html.join('');

main { display: flex; flex-direction: column; }
.panel { width: 60%; display: flex; padding: 0.5em; margin: 0.2em 0 0.2em 0; border: 1px solid #565656; background-color: #efefef; flex-direction: row; align-items: flex-start; }
.genres { margin: 0.3em 0 0 0; padding-left: 0; list-style-type: none; }
.panel div:first-child { width: 100%; }
.genre { display: inline-flex; margin-right: 0.4em; }
.title { font-weight: 600; font-size: 1.1em; }
.year { text-align: right; }

<main></main>

JSFiddle

这篇关于从不谈论填充普通视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆