How to disable searching in all columns EXCEPT the first two?

How to disable searching in all columns EXCEPT the first two?

sunny_ssunny_s Posts: 31Questions: 2Answers: 0
edited August 2018 in Free community support

My table has many columns, I want to know how to disable searching in all columns EXCEPT the first two? I found this documentation, the example shows how to disable filtering on the first column. But in my case I want to disable searching in all columns but the first two. How may I do so?

For example, I type "bagel", if the first two columns don't have any word "bagel", then there should be no matching records found. But right now since the word "bagel" appear in the 4th column, so it's considered as a match record.

let itemExpandedGroups = {};
  let departmentNameIndex = 2;
  let itemTable = $('#items-table').DataTable({
    'columnDefs': [
      {
        //to hide the department name uncommon the visible line
        'targets': [ departmentNameIndex ],
        // 'visible': false,
      }
    ],
    'paging':   false,
    'pageLength': 1,
    //use department name as the default order
    orderFixed: [[departmentNameIndex, 'asc']],
    rowGroup: {
      dataSrc: departmentNameIndex,
      startRender: function (rows, group) {
        let expanded = !!itemExpandedGroups[group];
        rows.nodes().each(function (r) {
          //collapsed by default
          // r.style.display = 'none';
          // if (expanded) {
          //   r.style.display = '';
          // }
          //expanded by default
        r.style.display = expanded ? 'none' : '';
        });
        // Add category name to the <tr>. NOTE: Hardcoded colspan
        return $('<tr/>')
          .append('<td colspan="10" class="to-be-edited">' + group + ' (' + rows.count() + ')<i class="fa fa-pencil-square-o fa-fw pencil-edit-name" aria-hidden="true"></i></td>')
          .attr('data-name', group)
          .css('cursor', 'pointer')
          .toggleClass('expanded', expanded);
      }
    },
    fixedHeader: {
      header: true
    }
  });

  $('#items-table tbody').on('click', 'tr.group-start', function () {
    let name = $(this).data('name');
    itemExpandedGroups[name] = !itemExpandedGroups[name];
    itemTable.draw(false);
  });

Replies

  • sunny_ssunny_s Posts: 31Questions: 2Answers: 0
    edited August 2018

    I added classname 'nosearch' to the all the td but the first two, and use

        'columnDefs': [
          {
            //to hide the department name uncommon the visible line
            'targets': [ departmentNameIndex ],
            // 'visible': false,
          },
          {
            'searchable': false,
            'targets': 'nosearch'
          }
        ],
    

    But doesn't work.
    I cannot just use index because I have no control of the number of columns. Is there anything like "targets": "_NOT 0, 1" ?

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946

    I added classname 'nosearch' to the all the td but the first two

    According to the columnDefs.targets docs you need to add the class to the TH. Sounds like you may have placed the nosearch class in the wrong spot.

    Kevin

  • sunny_ssunny_s Posts: 31Questions: 2Answers: 0

    Thank you for pointing out! It works.

This discussion has been closed.