【问题标题】:How do I create a node with childnodes using SQlite & Javascript?如何使用 SQlite 和 Javascript 创建带有子节点的节点?
【发布时间】:2019-05-10 12:57:27
【问题描述】:

我正在尝试使用 Electron 和一个名为 SQlite 的数据库构建一个应用程序。我想从多个表中创建一个标题和字幕列表。它需要看起来像这样:

**Titlename 1**
- Subname 1
- Subname 2
- Subname 3

**Titlename 2**
- Subname 4
- Subname 5
- Subname 6

这是我的代码:

db.serialize(() => {

var rows = document.getElementById("database");

db.each("SELECT DISTINCT titlename, Subname FROM sqlitedatabase ORDER BY Titleorder", function(err,row) {

// The results from the database (the titlename) are put into <p id="chaptertitle">
var item = document.createElement("p");
item.setAttribute("id", "chaptertitle");
item.textContent = row.chapname;

// The results of the subchapter column go into a second variable:  
var item2 = document.createElement("p");
item2.setAttribute("id", "subchaptertitle");
item2.textContent = row.Subchapter;

// Then both <p>-elements are put into the <div id='database'>
rows.appendChild(item);
rows.appendChild(item2);

});
});

// and we close the database
db.close();

结果和我想的不太一样:

        <div id="database">
    <p id="chaptertitle">Titlename 1</p>
    <p id="subchaptertitle">Subname 1</p>
    <p id="chaptertitle">Titlename 1</p>
    <p id="subchaptertitle">Subname 2</p>
    </div>

我该如何解决这个问题?我想我需要使用某种循环,但我不知道该怎么做。类似'foreach chaptertitle 选择相应的字幕'。你能帮帮我吗?

【问题讨论】:

  • 有人吗?有什么我应该看的文档或方向吗?
  • 不清楚你是问如何在查询中加入多个表,还是在js中chaptertitle只有在变化时才处理。

标签: node.js sqlite electron


【解决方案1】:

只是为了回答我自己的问题:

我将子节点添加到了错误的父节点。这有效:

// The results of the subchapter column go into a second variable  
var item2 = document.createElement("p");
item2.setAttribute("id", "subchaptertitle");
item2.textContent = row.Subchapter;

// add the subchapter as a childnode to the parentnode (var item)
rows.appendChild(item);
item.appendChild(item2);

将 item 附加到 &lt;div id="database"&gt;(变量行)和 item2 到 &lt;p id="chaptertitle"&gt;(变量项)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多