添加另一列会导致复选框停止工作

时间:2019-02-05 20:25:25

标签: javascript jquery datatables onclick append

至少,它是 之前的渲染。我创建了一个DataTable,其中包含多行文档(由JSON数据填充)和对应于每行的复选框。单击复选框,将其添加到一个单独的div(“收藏夹列表”)中,并且直到最近都可以使用。

我在表中合并了其他几列,由于某种原因,它似乎与复选框收藏夹冲突。我相信我必须在代码中添加/更新某些内容才能与新列一起使用,但是我不确定该如何处理。有什么想法吗?

正在加载表格数据:

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: "Blank" },
        { data: "Titles" }, // populates col-2 column with docs
        { data: "Categories" }, // hidden col-3 categories
        { data: "Blank" }
      ],
      columnDefs: [
        {
          data: "Path",
          ordering:  true, targets: [1],
          render: function(data, type, row) {
            return $('<a>')
              .attr({target: "_blank", href: row.Path})
              .text(data)
              .wrap('<div></div>')
              .parent()
              .html();
          },
          targets: [1]
        },
        { searchable: true, targets: [2], visible: false }, 
      ],
      data: tableRes,
      language: { searchPlaceholder: "Search All Documents" },
      lengthMenu: [ 10, 25, 50, 100, 250, 500 ],
      order: [],
      pageLength: 500,
    //   paging: true,
      pagingType: "full_numbers",
      responsive: true,
        scrollCollapse: true,
        scrollXInner: true,
        scrollY: 550,
      sDom: '<"top">rt<"bottom"flp><"left">' // puts dropdown on bottom
    });

收藏夹功能:

function faveFunc(evt) {
    var anchor = $($(evt.target).prev().find("a")[0]).clone();
    switch($(".populate-faves").find("a:contains(" + $(anchor).text() + ")").length)
    {
      case 0:
        $(".populate-faves").append(anchor);
        break;
      default:
        $(".populate-faves > a:contains(" + $(anchor).text() + ")").remove();
        break;
    }

    console.log(faveFunc);

  }; // ------------ faveFunc


function newList() {
    let data = $(evt.target).prev().find("a").eq(0).html();
     let outputList = $(".populate-faves");

     $(".populate-faves").html("");

    $("#km-table-id tbody tr").each(function(i, el) {
      let fave = $(".checkbox-class", el);
      let itemText = $(data, el);

      if(fave.prop("checked")) {
        outputList.append("<li>" + itemText.html() + "<li>");
      }
    });
  };


  $(".checkbox-class").on("click", faveFunc);
  $("#add-id").on("click", function(){
    console.log($(this));
  });

HTML片段:

<table id="km-table-id" class="cell-border display stripe-hover">
          <thead>
            <tr>
              <th></th>
              <th>Title</th>
              <th>Categories</th>
              <th>My Favorites</th>
            </tr>
          </thead>

          <!-- <tbody>

          </tbody>  -->

        </table>

1 个答案:

答案 0 :(得分:0)

更新:我找到了一个(有效的)解决方案。

基本上,“标题”的DataTable列必须位于最后一个位置,以与每个复选框正确对应。我确定有一种解决方案,可以在其后添加列,并且可以添加复选框,但是现在还可以。

$('#km-table-id').DataTable( {
      columns: [
        { data: "Blank" },
        { data: "Categories" }, // hidden
        { data: "Blank" },
        { data: "Titles" } // MUST be in last position to respond w/ checkboxes
      ],
      columnDefs: [
        {
          data: "Path", 
          ordering:  true, targets: [3],
          render: function(data, type, row) {
            return $('<a>')
              .attr({target: "_blank", href: row.Path})
              .text(data)
              .wrap('<div></div>')
              .parent()
              .html();
          },
          targets: [3]
        },
        { searchable: true, targets: [1], visible: false },
      ],

...

我试图实现一个更清洁的版本,其中columnDefs嵌套在{ data: "Titles"...下,但是没有用。目前,我坚持使用上面的代码。

相关问题