简体   繁体   English

使用带有 HTML 数据表的 Javascript 提交表格数据

[英]Submitting table's data with Javascript with HTML Datatables

I have a simple datatable that shows some JSON data received from an API.我有一个简单的数据表,显示了从 API 接收到的一些 JSON 数据。 I edited my table so that, for each row, there is a button that, when hit, will send an Ajax request to a URL containing the value for that specific row.我编辑了我的表格,以便对于每一行,都有一个按钮,当点击该按钮时,会将 Ajax 请求发送到包含该特定行的值的 URL。

Below is the function that will send the request, and the function that will render the table.下面是发送请求的函数和渲染表格的函数。 There are three columns on my table, when the X button is hit, the value of the column id for that row will be sent as an Ajax request to an external URL.我的表上有三列,当点击X按钮时,该行的列id值将作为 Ajax 请求发送到外部 URL。

This code works, but the only problem is that, along with the value of id , i would like to edit the table so that, when the button is hit, the value of the column item will be sent together with id , so that my ajax request can send item and id , not only id .这段代码有效,但唯一的问题是,除了id的值,我想编辑表格,以便在点击按钮时,列item的值将与id一起发送,以便我的ajax 请求可以发送itemid ,而不仅仅是id Can someone give me some advice on how to do that?有人可以就如何做到这一点给我一些建议吗? Thanks in advance!提前致谢!

$(document).ready(function() {
  $(document).on('click', '.btnClick', function() {
    var statusVal = $(this).data("status");
    console.log(statusVal)

    callAJAX("/request_handler", {
      "X-CSRFToken": getCookie("csrftoken")
    }, parameters = {
      'orderid': statusVal
    }, 'post', function(data) {
      console.log(data)
    }, null, null);

    return false;
  });

  let orderstable = $('#mytalbe').DataTable({
    "ajax": "/myview",
    "dataType": 'json',
    "dataSrc": '',
    "columns": [{
      "data": "item"
    }, {
      "data": "price"
    }, {
      "data": "id"
    },],
    "columnDefs": [{
      "targets": [2],
      "searchable": false,
      "orderable": false,
      "render": function(data, type, full) {
        return '<button type="button" class="btnClick sellbtn" data-status="replace">X</button>'.replace("replace", data);
      }
    }]
  });
});

Use html data attributes, then get them from the row that's being clicked/worked on, and send that along.使用 html 数据属性,然后从正在单击/处理的行中获取它们,然后将其发送。

<col data-item="$thisItem" id="a1" ><button class='a1' /></col>
$.click((e) => {
  let class = "a1"; // use the relevant references or use jQuery's parent(), etc
  let colId = $("col#"+class);
  let data = colId.data['data-item']; // this way you have your item info in the scope, and you can send it.

from: https://api.jquery.com/data/来自: https : //api.jquery.com/data/

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

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