简体   繁体   English

如何将数据库表值添加到javascript表中?

[英]How to add database table values into a javascript table?

so that's my question. 所以这是我的问题。

I have a database called Storage with a table named products, that table has 20 rows of products with the following fields (name, description, price, quantity). 我有一个名为Storage的数据库,其中有一个名为products的表,该表包含20行产品,其中包含以下字段(名称,描述,价格,数量)。

in my javascript code, i'm using XMLHttpRequest to ask the server for this information, the server (java servlet) executes the query "SELECT * FROM products" to get all values and save them in a variable in server side 在我的JavaScript代码中,我正在使用XMLHttpRequest向服务器询问此信息,服务器(java servlet)执行查询“ SELECT * FROM products”以获取所有值并将它们保存在服务器端的变量中

my question goes, how can i get that data from the java servlet, send it to my javascript code (client side) and create a table with those values. 我的问题是,如何从Java Servlet获取数据,将其发送到我的JavaScript代码(客户端),并使用这些值创建表。

this is my attempt to generate the table in client side to add the values. 这是我尝试在客户端生成表以添加值。

`function generateTable()
{
    table.style.border = "1";
    table.style.borderColor = "red";
    table.style.alignment = "center";

    for (int i=0; i<10; i++)
    {
        table.insertRow();

        for(int j = 0; j<4; j++)
        {
            table.rows[i].insertCell();
        }
    }

    table.style.display = "block";
    document.body.appendChild(table);
}`

Notes: i have to use raw Javascript, Java, and if needed AngularJS. 注意:我必须使用原始Javascript,Java和AngularJS(如果需要)。

If you mean insert a row to the table, and you want to use AJAX way. 如果要在表中插入一行,则要使用AJAX方式。 AJAX is when you do not refresh the page, only JavaScript communicates in the background. AJAX是当您不刷新页面时,只有JavaScript在后台通信。

  1. post an AJAX message to a servlet 将AJAX消息发布到Servlet
  2. implement the doPost() method and return a Stirng value, it could be a JSON 实现doPost()方法并返回Stirng值,它可以是JSON
  3. implement the Javascript callback where the AJAX completed 在AJAX完成的地方实现Javascript回调

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM