【问题标题】:On the table last entry is coming at the last.I want to show the latest entries on the top of the table. How to do it?在表上最后一个条目出现在最后。我想在表格顶部显示最新条目。怎么做?
【发布时间】:2020-07-12 05:58:00
【问题描述】:

我有一个表单,如果用户发布某些内容,它将显示在表格上。我为此目的使用了 firebase 实时数据库。目前最新的条目显示在表格的最后。我想在表格的顶部显示最新的条目。最新的条目应该在第一个,旧的条目应该在最后。

// Initialize Firebase
var config = {
  //mycofig
};
firebase.initializeApp(config);

var database = firebase.database();
var ref = database.ref("Registrations");

ref.on("value", function(snapshot) {
  snapshot.forEach(function(childSnapshot) {
    var data = childSnapshot.val();
    var childKey = childSnapshot.key;
    var childData = childSnapshot.val();
  });
  var database = firebase.database();
  database.ref("Registrations").once("value", function(snapshot) {
    if (snapshot.exists()) {
      var content = "";
      snapshot.forEach(function(data) {
        var val = data.val();
        content += "<tr>";
        content += "<td>" + val.name + "</td>";
        content += "<td>" + val.business + "</td>";
        content += "<td>" + val.phone + "</td>";
        content += "<td>" + val.area + "</td>";
        content += "<td>" + val.category + "</td>";
        content += "</tr>";
      });
      $("#ex-table").append(content);
    }
  });
});
table {
  width: 100%;
  border-collapse: collapse;
}


/* Zebra striping */

tr:nth-of-type(odd) {
  background: #eee;
}

th {
  background: #333;
  color: white;
  font-weight: bold;
}

td,
th {
  padding: 6px;
  border: 1px solid #ccc;
  text-align: left;
}

@media only screen and (max-width: 760px),
(min-device-width: 768px) and (max-device-width: 1024px) {
  /* Force table to not be like tables anymore */
  table,
  thead,
  tbody,
  th,
  td,
  tr {
    display: block;
  }
  /* Hide table headers (but not display: none;, for accessibility) */
  thead tr {
    position: absolute;
    top: -9999px;
    left: -9999px;
  }
  tr {
    border: 1px solid #ccc;
  }
  td {
    /* Behave  like a "row" */
    border: none;
    border-bottom: 1px solid #eee;
    position: relative;
    padding-left: 50%;
  }
  td:before {
    /* Now like a table header */
    position: absolute;
    /* Top/left values mimic padding */
    top: 6px;
    left: 6px;
    width: 45%;
    padding-right: 10px;
    white-space: nowrap;
  }
  /*
Label the data
*/
  td:nth-of-type(1):before {
    content: "Name";
  }
  td:nth-of-type(2):before {
    content: "Location";
  }
  td:nth-of-type(3):before {
    content: "Rent p/m";
  }
  td:nth-of-type(4):before {
    content: "BHK/Rooms";
  }
  td:nth-of-type(5):before {
    content: "Total Persons";
  }
  td:nth-of-type(6):before {
    content: "Phone no";
  }
  td:nth-of-type(7):before {
    content: "Other Details";
  }
  td:nth-of-type(8):before {
    content: "Posting Date";
  }
}

</style>
<table id="ex-table">
  <thead>
    <tr>
      <th>Name</th>
      <th>Business name</th>
      <th>Phone no</th>
      <th>Area</th>
      <th>Category</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

【问题讨论】:

  • 你试过data.reverse()吗?您是否在查询中正确排序数据?你试过什么?
  • 我试过 data.reverse() ,但它显示数据未定义

标签: javascript html css firebase firebase-realtime-database


【解决方案1】:

您的问题是您在构建 html 字符串后将项目放置在表格的末尾 - 如下所示

 $("#ex-table").append(content);

如果您将其交换为“前置” - 它会按照以下方式在表格的开头插入新项目

 $("#ex-table").prepend(content);

因此,添加到表格的最后一行是表格的第一行。这是文档的链接 - https://api.jquery.com/prepend/

也就是说 - 我会操纵正在读取的数据并将其反转,以使数据与预期表的顺序相匹配。

【讨论】:

  • 'face-plant' - 哦 - 当然 - 你正在从所有 tr 中制作字符串,然后附加 - 我的解决方案完全行不通 - 抱歉。 - 您是否尝试删除 data.reverse(); - 如果这是颠倒数据的顺序,那么删除它可能会起作用。
【解决方案2】:

flex-direction: column-reverse; 会将表格中最新的项目放在首位。将此 css 添加到您希望最后一项放在首位的表中:

#ex-table{
  display: flex;
  flex-direction: column-reverse;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    相关资源
    最近更新 更多