Нет описания

datatablesUtils.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* remove user provided filter */
  2. function removeFilter(clickedObject) {
  3. console.log("removeFilter")
  4. let inputObject = $(clickedObject)
  5. .parents("table")
  6. .find("input")[$(clickedObject).parents("th").index()]
  7. inputObject.value=""; // Clear input
  8. $(inputObject).trigger("change"); // trigger change event
  9. }
  10. /* add a text input to each column of a given datatable to allow filtering */
  11. function addFilterFields(tableId){
  12. $('#' + tableId + ' thead tr')
  13. .clone(true)
  14. .addClass('filters')
  15. .appendTo('#' + tableId + ' thead');
  16. }
  17. /* callback function to activate filtering on a datatable */
  18. function tableFiltering(api, table_anchor, exclude_columns=[]) {
  19. // For each column
  20. api
  21. .columns()
  22. .eq(0)
  23. .each(function (colIdx) {
  24. // Set the header cell to contain the input element
  25. var cell = $('#'+table_anchor+' .filters th').eq(
  26. $(api.column(colIdx).header()).index()
  27. );
  28. if (exclude_columns.includes(colIdx)) {
  29. $(cell).html('<div class="form-group has-feedback" style="display: none;"><input type="text" class="form-control" placeholder="Filter"><i class="fas fa-times-circle form-control-feedback" onclick="removeFilter(this);"></i></div>');
  30. return;
  31. }
  32. $(cell).html('<div class="form-group has-feedback"><input type="text" class="form-control" placeholder="Filter"><i class="fas fa-times-circle form-control-feedback" onclick="removeFilter(this);"></i></div>');
  33. // On every keypress in this input
  34. $(
  35. 'input',
  36. $('#'+table_anchor+' .filters th').eq($(api.column(colIdx).header()).index())
  37. )
  38. .off('keyup change')
  39. .on('keyup change', function (e) {
  40. e.stopPropagation();
  41. // Get the search value
  42. $(this).attr('title', $(this).val());
  43. var regexr = '({search})';
  44. var cursorPosition = this.selectionStart;
  45. // Search the column for that value
  46. api
  47. .column(colIdx)
  48. .search(
  49. this.value != ''
  50. ? regexr.replace('{search}', '(((' + this.value + ')))')
  51. : '',
  52. this.value != '',
  53. this.value == ''
  54. )
  55. .draw();
  56. $(this)
  57. .focus()[0]
  58. .setSelectionRange(cursorPosition, cursorPosition);
  59. });
  60. });
  61. }