Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Pages - General Discussions
Unanswered

Filter Template - Power Pages

(0) ShareShare
ReportReport
Posted on by 2

Hello. 

To explain the objective: I have a project with multiple tables that need to be filtered like in Excel (sort, checkbox, type search). When testing the script, I created a page with a table and, together with HTML, implemented the filters, and they work.

The issue is that I can't seem to apply the script to other tables because they are structured differently. My test table is written as a normal HTML table, while the tables where I want to apply the script look different.

cattavares1_0-1715852547380.png
(Tables where I want to apply the script look like:)
cattavares1_1-1715852582042.png

 

Here is the filter script (I have AJAX and DataTable sources):

<script>

        $(document).ready(function() {
            var table = $('table').DataTable({
                searching: true,
                paging: true,
                ordering: true
            });

            //popup filter
            $('th').each(function(index) {
                var columnIndex = index;
                var columnData = table.column(columnIndex).data().unique().toArray();
                var filter = $('<div class="popup-filter"></div>');
                filter.append('<input type="text" placeholder="Search">');
                filter.append('<button class="sort">Sort</button>');
                filter.append('<br>');
                columnData.forEach(function(option) {
                    var textContent = $(document.createElement('div')).html(option).text();
                    filter.append('<label><input type="checkbox" class="filter-checkbox" data-filter-value="' + textContent + '">' + textContent + '</label><br>');
                });


                $(this).append(filter);
            });

            //close popup
            $('th').click(function(event) {
                $('.popup-filter').hide();
                $(this).find('.popup-filter').toggle();
                event.stopPropagation(); // prevent the click event from bubbling up
            });

            //close popup when clicking outside of it
            $(document.body).on('click', function(event) {
                if (!$(event.target).closest('.popup-filter').length) {
                    $('.popup-filter').hide();
                }
            });

            //close popup when clicking enter in search
            $('.popup-filter input[type="text"]').on('keyup', function(event) {
                if (event.keyCode === 13) {
                    table.column($(this).closest('th').index()).search(this.value).draw();
                    $('.popup-filter').hide();
                }
            });

            // search
            $('.popup-filter input[type="text"]').on('keyup', function() {
                table.column($(this).closest('th').index()).search(this.value).draw();
            });

            // sorting
            $('.popup-filter').on('click', '.sort', function() {
                var columnIndex = $(this).closest('th').index();
                var order = table.column(columnIndex).order()[0];
                if (order === 'asc') {
                    table.column(columnIndex).order('desc').draw();
                } else {
                    table.column(columnIndex).order('asc').draw();
                }
            });

            //checkbox
            $('.popup-filter').on('change', '.filter-checkbox', function() {
                var columnIndex = $(this).closest('th').index();
                if ($(this).prop('checked')) {
                    //var filterValue = $(this).val();
                    var filterValue = $(this).data('filter-value');
                    console.log(filterValue);
                    //var filterValue = table.column(columnIndex).data(this.value).val();
                    //alert(filterValue);
                    table.column(columnIndex).search(filterValue).draw();
                } else {
                    table.column(columnIndex).search('').draw();
                }
            });
        });
        </script>
 
 
I suspect the issue might be with "var table = $('table').DataTable({" but I'm not sure how to proceed with creating a web template first with the script and then applying it to all tables.
Categories:

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 69 Super User 2025 Season 1

#2
oliver.rodrigues Profile Picture

oliver.rodrigues 49 Most Valuable Professional

#3
Jon Unzueta Profile Picture

Jon Unzueta 43