Lines 587-593
function _dt_visibility(table_settings, table_dt) {
Link Here
|
587 |
options = {}, |
587 |
options = {}, |
588 |
table_settings = undefined, |
588 |
table_settings = undefined, |
589 |
add_filters, |
589 |
add_filters, |
590 |
default_filters |
590 |
default_filters, |
|
|
591 |
filters_options |
591 |
) { |
592 |
) { |
592 |
// Early return if the node does not exist |
593 |
// Early return if the node does not exist |
593 |
if (!this.length) return; |
594 |
if (!this.length) return; |
Lines 666-671
function _dt_visibility(table_settings, table_dt) {
Link Here
|
666 |
|
667 |
|
667 |
var table_dt = table.DataTable(); |
668 |
var table_dt = table.DataTable(); |
668 |
|
669 |
|
|
|
670 |
if (add_filters) { |
671 |
_dt_add_filters(this, table_dt, filters_options); |
672 |
} |
673 |
|
674 |
table_dt.on("column-visibility.dt", function () { |
675 |
if (add_filters) { |
676 |
_dt_add_filters(this, table_dt, filters_options); |
677 |
} |
678 |
}); |
679 |
|
669 |
table_dt.on("search.dt", function (e, settings) { |
680 |
table_dt.on("search.dt", function (e, settings) { |
670 |
// When the DataTables search function is triggered, |
681 |
// When the DataTables search function is triggered, |
671 |
// enable or disable the "Clear filter" button based on |
682 |
// enable or disable the "Clear filter" button based on |
Lines 688-691
function _dt_visibility(table_settings, table_dt) {
Link Here
|
688 |
|
699 |
|
689 |
return table; |
700 |
return table; |
690 |
}; |
701 |
}; |
|
|
702 |
|
703 |
function _dt_add_filters(table_node, table_dt, filters_options = {}) { |
704 |
if (!$(table_node).length) return; |
705 |
|
706 |
$(table_node).find("thead tr:eq(1)").remove(); // Remove if one exists already |
707 |
$(table_node) |
708 |
.find("thead tr") |
709 |
.clone() |
710 |
.appendTo($(table_node).find("thead")); |
711 |
|
712 |
let visibility = table_dt.columns().visible(); |
713 |
let columns = table_dt.settings()[0].aoColumns; |
714 |
table_dt.columns().every(function () { |
715 |
var column = this; |
716 |
let i = column.index(); |
717 |
var visible_i = table_dt.column.index("fromData", i); |
718 |
let th = $(table_node).find( |
719 |
"thead tr:eq(1) th:eq(%s)".format(visible_i) |
720 |
); |
721 |
var is_searchable = table_dt.settings()[0].aoColumns[i].bSearchable; |
722 |
$(this) |
723 |
.removeClass("sorting") |
724 |
.removeClass("sorting_asc") |
725 |
.removeClass("sorting_desc"); |
726 |
$(this).data("th-id", i); |
727 |
if (is_searchable || $(this).data("filter") || filters_options[i]) { |
728 |
let input_type = "input"; |
729 |
let existing_search = column.search(); |
730 |
if ($(th).data("filter") || filters_options.hasOwnProperty(i)) { |
731 |
input_type = "select"; |
732 |
let filter_type = $(th).data("filter"); |
733 |
let select = $( |
734 |
'<select class="dt-select-filter"><option value=""></option></select' |
735 |
); |
736 |
|
737 |
// FIXME eval here is bad and dangerous, how do we workaround that? |
738 |
if (!filters_options.hasOwnProperty(i)) { |
739 |
filters_options[i] = eval(filter_type); |
740 |
} else if (typeof filters_options[i] === "function") { |
741 |
filters_options[i] = filters_options[i](table_dt); |
742 |
} |
743 |
$(filters_options[i]).each(function () { |
744 |
let o = $( |
745 |
'<option value="^%s$">%s</option>'.format( |
746 |
this._id, |
747 |
this._str |
748 |
) |
749 |
); |
750 |
// Compare with lc, or selfreg won't match ^SELFREG$ for instance, see bug 32517 |
751 |
// This is only for category, we might want to apply it only in this case. |
752 |
existing_search = existing_search.toLowerCase(); |
753 |
if ( |
754 |
existing_search === this._id || |
755 |
(existing_search && |
756 |
this._id.toLowerCase().match(existing_search)) |
757 |
) { |
758 |
o.prop("selected", "selected"); |
759 |
} |
760 |
o.appendTo(select); |
761 |
}); |
762 |
$(th).html(select); |
763 |
} else { |
764 |
var title = $(th).text(); |
765 |
if (existing_search) { |
766 |
$(th).html( |
767 |
'<input type="text" value="%s" style="width: 100%" />'.format( |
768 |
existing_search |
769 |
) |
770 |
); |
771 |
} else { |
772 |
var search_title = __("%s search").format(title); |
773 |
$(th).html( |
774 |
'<input type="text" placeholder="%s" style="width: 100%" />'.format( |
775 |
search_title |
776 |
) |
777 |
); |
778 |
} |
779 |
} |
780 |
} else { |
781 |
$(th).html(""); |
782 |
} |
783 |
}); |
784 |
_dt_add_delay_filters(table_dt, table_node); |
785 |
table_dt.fixedHeader?.adjust(); |
786 |
} |
787 |
|
788 |
function _dt_add_delay_filters(table_dt, table_node) { |
789 |
let delay_ms = 500; |
790 |
|
791 |
let col_input_search = DataTable.util.debounce(function (i, val) { |
792 |
table_dt.column(i).search(val).draw(); |
793 |
}, delay_ms); |
794 |
let col_select_search = DataTable.util.debounce(function (i, val) { |
795 |
table_dt.column(i).search(val, true, false).draw(); |
796 |
}, delay_ms); |
797 |
|
798 |
$(table_node) |
799 |
.find("thead tr:eq(1) th") |
800 |
.each(function (visible_i) { |
801 |
var i = table_dt.column.index("fromVisible", visible_i); |
802 |
$(this) |
803 |
.find("input") |
804 |
.unbind() |
805 |
.bind("keyup change", function (e) { |
806 |
if (e.keyCode === undefined) return; |
807 |
col_input_search(i, this.value); |
808 |
}); |
809 |
|
810 |
$(this) |
811 |
.find("select") |
812 |
.unbind() |
813 |
.bind("keyup change", function () { |
814 |
let value = this.value.length |
815 |
? "^" + this.value + "$" |
816 |
: ""; |
817 |
col_select_search(i, this.value); |
818 |
}); |
819 |
}); |
820 |
} |
691 |
})(jQuery); |
821 |
})(jQuery); |
692 |
- |
|
|