处理动态表数据

时间:2019-06-17 12:10:45

标签: javascript python flask sqlalchemy flask-sqlalchemy

我正在使用Flask-Sqlalchemy。

我在从动态创建的HTML表中收集变量时遇到问题。

我已经创建了表格的第一行,表单的形式是将数据发送到后端,如下所示:

<form action='/upload_enclosure_script' method='POST'>
    <table id='sct_components_table'>
        <thead>
            <tr>
              <td > ID</td>
              <td > Qty</td>
            </tr>
          </thead>
          <tr name='row_01' id = 'row_01'>
            <td> 1 </td>
            <td> <input name='quantity1' id='quantity1'/>  </td>
          </tr>
      </table>
      <button type='submit' value='Upload' ><b>Continue</b></button>
  </form>
  <button onclick="Add_row()"> Add Row</button>

然后使用JavaScript创建了一个脚本,该脚本将在单击“添加行”按钮时继续添加行。

function Add_row() {
    var tbl = document.getElementById('sct_components_table');
    var iteration = tbl.tBodies[0].rows.length;
    var row_number = iteration + 1;
    newRow = tbl.tBodies[0].insertRow(-1);
    newRow.setAttribute('name','row_02');
    newRow.setAttribute('id','row_02');

    var newCell = newRow.insertCell(0);
    newCell.innerHTML = row_number;

    var newCell1 = newRow.insertCell(1);
    el.type = 'text';
    el.name = 'quantity'+row_number;
    el.id = 'quantity'+row_number;
    el.value = '';
    newCell1.appendChild(el);
}

现在,我想使用Python脚本来收集所有变量,以便我可以处理它们。但是,我无法获取要处理的动态创建的行(Row_02,Row_03等)中的变量。我的想法是,使用JavaScript在HTML中添加一行不会将其添加到表单中,因此信息永远不会发送到数据库的后端。

@upload_enclosure_blueprint.route('/upload_enclosure_script', methods=['POST' , 'GET'])

@login_required
def upload_enclosure_script():
    quantity1 = request.form['quantity1']
    print(quantity1)
    quantity2 = request.form['quantity2']
    print(quantity2)

    return render_template('product_lookup.html', page_name=page_name)

我尝试将所有tr / td的HTML'name'标签更改为具有相同的值,例如

<td> <input name='quantity' id='quantity'/></td>

然后尝试像这样收集它们作为列表:

@upload_enclosure_blueprint.route('/upload_enclosure_script', methods=['POST' , 'GET'])
@login_required
def upload_enclosure_script():

    temp_multi_quantity = request.form.getlist('quantity')
    print(temp_multi_quantity[0])
    print(temp_multi_quantity[1])
    return render_template('product_lookup.html', page_name=page_name)

我几乎没有成功。有谁知道是否有可能实现我想要达到的目标?

0 个答案:

没有答案
相关问题