【问题标题】:Getting checklist to show all selected items (rather than whichever item is selected first)获取清单以显示所有选定的项目(而不是首先选择的项目)
【发布时间】:2019-01-28 15:24:44
【问题描述】:

我有一个数据表,其中包含对应于每一行的复选框。理想情况下,当您单击复选框时,它会将该行的内容附加到“收藏夹列表”中。但是发生的事情是,无论我单击哪个复选框,都是唯一显示在列表中的复选框。

我明白为什么只显示第一项 (favesArr[0]),但我想知道的是 how to get the List to show all of the items that are selected. 我在这个问题中遗漏了 HTML 文件,所以如果你想喜欢看的话就告诉我吧。

Here's a screencap 显示我的浏览器。

JS 示例:

import JSONfile from '../../../public/JSONfile.json';

loadTableData() {
    $.noConflict();
    let tableRes = JSONfile.d.results.filter(function(val) { 
      return (val.FileLeafRef.trim().length > 0);
    }).map(function(obj) {

      return {
        "Path": obj.EncodedAbsUrl,
        "Titles": obj.File.Name,
        "Categories": obj.ResourceType.results.map(function(val) {
          return val.Label;
        }).join(";"),
        // "Blank": ''
        }

      });

$('#km-table-id').DataTable( {
      columns: [
        { data: "Titles" }, // populates col-2 column with docs
        { data: "Categories" } // hidden col-3 categories
      ],
      columnDefs: [
        {
          data: "Path",
          render: function(data, type, row) {
            return $('<a>')
              .attr({target: "_blank", href: row.Path})
              .text(data)
              .wrap('<div></div>')
              .parent()
              .html();
          },
          targets: 0
        },
        { searchable: true, targets: [1], visible: false },
      ],
      data: tableRes,
      language: { searchPlaceholder: "Search All Documents" },
      responsive: true,
        scrollCollapse: true,
        scrollX: true,
        scrollY: 600,
      select: {
        selector: "td:first-child",
        style: "os"
      },
      stateSave: true
    });

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

    const $table = $("#km-table-id")

    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
        }
      }     

      // let arrOfTitles = favesArr;
      // let uniqueTitles = [];
      // $.each(arrOfTitles, function(e, el) {
      //   if ($.inArray(el, uniqueTitles) === -1) uniqueTitles.push(el);
      // });

      $(".populate-faves").append(JSON.stringify(favesArr[0].Titles) + '<br/>'); /////// This is the line that's bugging me


      console.log(favesArr); ///// does show whichever document (obj) is selected
    });


  } // ------------------ loadTableData

【问题讨论】:

    标签: javascript jquery arrays json datatables


    【解决方案1】:

    要为favesArr 中的每个条目获取一行,您可以先删除所有以前的条目 ($(".populate-faves").empty();),然后为每个收藏夹添加一个条目,如下所示:

    for (var i=0;i<favesArr.length;i++) {
        $(".populate-faves").append(JSON.stringify(favesArr[i].Titles) + '<br/>');
    }
    

    当然,有很多更优雅的方法可以解决这个问题,但我认为,如果你现在是,这将是最直观的方法。

    【讨论】:

    • 做到了!这比我想象的要简洁得多。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 2016-08-01
    相关资源
    最近更新 更多