【问题标题】:Insert Table data and Total into database将表数据和总计插入数据库
【发布时间】:2021-05-22 12:24:10
【问题描述】:

我想在我的应用程序中添加费用和投资管理
这是示例图片

这是我的html代码

function day(){

var table = document.getElementById('customers');
var sumVal=0;
var sumexp=0;
for (var i = 1; i < table.rows.length; i++) {
   Description = table.rows[i].cells[0].firstChild.value,
   Investment = table.rows[i].cells[1].firstChild.value,
   Expenses = table.rows[i].cells[2].firstChild.value,
   sumVal = sumVal +parseInt(table.rows[i].cells[1].firstChild.value);
   sumexp = sumexp +parseInt(table.rows[i].cells[2].firstChild.value);
  
console.log(Description);
console.log(Investment);
console.log(Expenses);
console.log(sumVal);
console.log(sumexp);

}

  
}
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
input {
  border: none;
}
div{
width: 50%;
}
<div>                       
    <table id="customers">
        <tbody id="mytbody">
            <tr>                                  
                <th>Descritption</th> 
                <th>Investment</th>
                <th>Expenses</th>                                                                
            </tr>
            <tr>
                <td><input type="text" name="yourname" /> </td> 
                <td><input type="number" name="yourname" /></td>
                <td><input type="number" name="yourname" /></td>                                   
            </tr>
            <tr>
                <td><input type="text" name="yourname" /> </td> 
                <td><input type="number" name="yourname" /></td>
                <td><input type="number" name="yourname" /></td>                                   
            </tr>
              
        </tbody>
    </table> 
  <input type="button" id="save" value ="save" name="save" onclick="day()">
  
</div>

我的问题是

  1. 我得到了js中的所有表值现在如何将值插入数据库

  2. 如果任何单元格中没有数字,则在计算总计时返回总计是NAN 即使某些单元格为空,如何处理这个以计算所有值

我在 Electron js 和 PouchDB 上做这个应用程序

任何人都可以帮助我node jsJavascript中的代码

【问题讨论】:

  • 你试过什么,什么没用?您的代码中没有一行 Javascript。只是 HTML 和一些 CSS(所问的问题不需要)。 PouchDB 和 Node.js 并不是那么简单,所以我想你已经有更多的代码了。
  • 添加了js你能帮我插入数据库吗

标签: javascript node.js pouchdb


【解决方案1】:

很抱歉,您所做的只是从表中检索值。仍然没有足够的代码来提供实际答案。 Stackoverflow 并不是为您编写代码 - 它旨在帮助您解决已定义的问题。那么你应该怎么做:

  1. 考虑一下您希望如何将数据存储在基于文档的 NoSQL-DB 中。因为这就是 PouchDB。您必须首先考虑数据结构。也许考虑一下基于键值对的简单本地存储是否更适合您的需求。

    可能是这样的

     "_id": "a5dffd2f-5c4f-46a4-abdc-2f1f3569d2cf",
     "_rev": "3-8a1a6dc9ac78b1e388c4b2051e298691",
     "user": "user_1",
     "Date": "1621654503"
     "Data": [{
         "Descritption": "his description",
         "Investment": "20",
         "Expenses": "13"
         },
         "Descritption": "his description 2",
         "Investment": "30",
         "Expenses": "14"
         }]
    

    假设每人多于一条线

  2. 将您的 Data 放入对象数组中,其余放入变量中。

  3. 将其写入您的 PouchDB

     const PouchDB = require('pouchdb-browser');
     let db = new PouchDB('myDB', {adapter: 'idb'});
    
     db.post({
         Descritption: yourDescritption,
         user: yourUser,
         date: yourDate, 
         Data: yourArrayofObjects,
     }).then(function (response) {
         console.log(response);
     }).catch(function (err) {
         console.log(err);
     });
    
  4. 您仍然需要考虑检索或更新您的数据。

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    相关资源
    最近更新 更多