web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / Filter Template - Powe...
Power Pages
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:
I have the same question (0)

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 47 Super User 2026 Season 1

#2
sannavajjala87 Profile Picture

sannavajjala87 16 Super User 2026 Season 2

#3
11manish Profile Picture

11manish 14 Super User 2026 Season 2

Last 30 days Overall leaderboard