简体   繁体   English

jQuery Datatable,使用html文本框编辑选定的行数据

[英]jQuery Datatable, editing selected rows data with html text boxes

I'm fairly new to coding. 我是编码新手。 I have a jQuery datatable, and when I select a row, the tds of that row fill out html textboxes above the table. 我有一个jQuery数据表,当我选择一行时,该行的tds填写了表格上方的html文本框。 I'm trying to make it so whatever is entered into those textboxes (and upon pressing the save button), is then saved into the row. 我正在尝试将其输入到这些文本框中(并按保存按钮),然后将其保存到该行中。

Currently I have it so it saves 1 field/td. 目前我有它,所以它节省了1个字段/ td。 If I press on column 0, fill out the Name textbox and press save, it saves. 如果按第0列,请填写“名称”文本框,然后按保存,它将保存。 But it works on any column. 但它适用于任何列。 It should only be editing the correct td. 它只能编辑正确的td。 Plus I want to edit the entire row, not just one td. 另外,我要编辑整个行,而不仅仅是一个td。 I'm not sure how to accomplish this. 我不确定如何做到这一点。 Thanks for any help! 谢谢你的帮助!

JSFiddle JSFiddle

Javascript: Javascript:

var table = $('#example').DataTable();

(function () {
      var table = document.querySelector('#example');
      var name = document.querySelector('#nameinput');
      var format = document.querySelector('#formatinput');
      var address = document.querySelector('#addressinput');
      var report = document.querySelector('#reportinput');
      var alarm = document.querySelector('#alarminput');



      table.addEventListener('click', onTableClick);

      function onTableClick (e) {
        var tr = e.target.parentElement;

        var data = [];
        for (var td of tr.children) {
          data.push(td.innerHTML);
        }

        name.value = data[0];
        address.value = data[1];
        format.value = data[2];
        report.value = data[3];
        alarm.value = data[4];
        console.log(alarm.value);


      }
        $("#saverow").click(function() {
            var table1 = $('#data-table').DataTable();
            var data = [];
            data[0] = name.value;
            data[4] = alarm.value;
            console.log(name.value);
            console.log(alarm.value);

            table1.draw(true);
        });


})();`

I've updated my code with what I've tried so far. 我已经用到目前为止尝试过的代码更新了代码。 Currently, what I type in the textboxes, correctly is displayed in the console (upon hitting the saverow button), now I cant figure out how to save that into the table. 当前,我在文本框中键入的内容正确显示在控制台中(按保存行按钮时),现在我不知道如何将其保存到表中。

i think it is mor responsive to edit data right in the table. 我认为这是mor响应表中的编辑数据。

HTML: HTML:

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Address</th>
            <th>Format</th>
            <th>Report Time</th>
            <th>Alarms</th>
            <th></th>

        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Tiger Nixon</td>
            <td>Tiger@gmail.com</td>
            <td>email</td>
            <td>1PM</td>
            <td>Master</td>
            <td class="td-button"></td>
        </tr>
        <tr>
            <td>Bill Gates</td>
            <td>111-111-1111</td>
            <td>sms</td>
            <td></td>
            <td>Master</td>
            <td class="td-button"></td>
        </tr>
    </tbody>
</table>

JS: JS:

var table = $('#example').DataTable();

$("#example tbody tr").click(function(){
 if (! $(this).find("button").length)
 {
  $(this).find("td").each(function(){
    if (!$(this).hasClass("td-button"))
    {
        var text = $(this).text();
        $(this).html ('<input type="text" value="' +  text + '">')
    } else
        $(this).html ('<button class="button-save">Save</button>')
   })
 }
})

$(document).on("click", ".button-save",function(){
    var tr = $(this).parent().parent();
  tr.find("td").each(function(){
    if (!$(this).hasClass("td-button"))
    {
        var text = $(this).find("input").val();
        $(this).text(text)
    } else
        $(this).html('');
  })
})

https://jsfiddle.net/91wvw619/ https://jsfiddle.net/91wvw619/

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

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