【问题标题】:Data isn't getting pushed to array数据没有被推送到数组
【发布时间】:2019-01-25 19:32:22
【问题描述】:

我已经能够将JSON数据(本地文件)显示到我的表中的每一行中,以及检查数据的相应复选框时,我希望将这些所选值推入数组。最终目标是在 div 中显示数组,但直到我能够看到数组正在被填充。

JS sn-p:

($("#km-table-id tbody tr")).append($("<input />", {"type": "checkbox"}).addClass("checkbox-class"));

let table = $("#km-table-id").DataTable();
    let favesArr = [];

    $(".checkbox-class").on("click", "tr", function() {
      let data = table.row(this).data();
      for (var i = 0; i < favesArr.length; i++) {
        favesArr.push($(data).text());
        $("#myFave.hs-gc-header").append(favesArr[i]);
      }      
      console.log(data); // this was showing the selected data a short time ago, but not anymore  

    });

    console.log(favesArr); // only showing empty

【问题讨论】:

    标签: javascript jquery arrays datatables


    【解决方案1】:

    首先,您的最后一行将始终打印一个空数组,因为您只是在事件处理程序中填充它。

    其次,您使用i &lt; favesArr.length 作为循环条件。 favesArr这里还空着,如果没有填写其他部分的代码。因此,循环体永远不会被执行。你可能想在这里data.length

    最后但同样重要的是,您可能只想将 data[i] 而不是整个数组推送到您的 favesArray

    【讨论】:

    • 我将favesArr.push( 行移到for loop. 上方,没有任何改变,但我的方向是否正确?
    • @Bodrov 您的问题是您只是在单击事件上推送内容,但在设置事件侦听器后立即触发单击事件下的内容。如果您希望它在变量更改时执行事件处理程序,请在单击处理程序中向上移动您的 console.log 语句。
    【解决方案2】:

    我建议您捕获复选框是否被选中。您可以通过抓取数据索引来检查该项目是否已经在数组中。

    不确定您的 HTML 是什么样子...

    (function($) {
      $.fn.appendText = function(text) { return this.text(this.text() + text); };
      $.fn.appendHtml = function(html) { return this.html(this.html() + html); };
    })(jQuery);
    
    const $table = $('#km-table-id');
    
    $table.find('tbody tr').append($("<input>", { type: 'checkbox', class : 'checkbox-class'}));
    
    let table = $table.DataTable();
    let favesArr = [];
    
    $table.on('click', '.checkbox-class', function(e) {
      let data = table.row(this.parentNode).data(),
          checked = $(this).is(':checked'),
          dataIndex = favesArr.indexOf(data);
      if (checked) {
        if (dataIndex === -1) {
          favesArr.push(data); // Add item
        }
      } else {
        if (dataIndex > -1) {
          favesArr.splice(dataIndex, 1); // Remove item
        }
      }
      $('#myFave.hs-gc-header').appendHtml('>> ' + favesArr.map(x => x.join(', ')).join('; ') + '<br/>');
    });
    body {
      background: #666;
    }
    .table-wrapper {
      background: #fff;
      width: 80%;
      margin: 0 auto;
      margin-top: 1em;
      padding: 0.25em;
    }
    
    #myFave.hs-gc-header {
      background: #fff;
      width: 81%;
      margin: 0 auto;
      margin-top: 0.5em;
      height: 5em;
      overflow: scroll;
    }
    <link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    
    <div class="table-wrapper">
      <table id="km-table-id">
        <thead>
          <tr>
            <th>A</th><th>B</th><th>C</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>1</td><td>2</td><td>3</td>
          </tr>
          <tr>
            <td>4</td><td>5</td><td>6</td>
          </tr>
          <tr>
            <td>7</td><td>8</td><td>9</td>
          </tr>
        </tbody>
      </table>
    </div>
    
    <div id="myFave" class="hs-gc-header"></div>

    【讨论】:

    • 我更新了我的代码,点击一个框后我得到 x.join 不是控制台中的函数。据我所知,我的代码与您的代码相同,所以也许是别的东西?感谢您在代码 sn-p 中创建表格,顺便说一句。
    • @Bodrov x 代表每行中的单个单元格值。
    • 快速更新:我将console.log(favesArr) 移动到了不同的位置,可以看到数组正在被填充,但数组没有被附加到 div。我将添加/删除/重新排列代码以查看发生了什么。
    猜你喜欢
    • 1970-01-01
    • 2022-08-18
    • 2020-02-03
    • 2020-12-31
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 2022-11-10
    相关资源
    最近更新 更多