"Setting up a filter for the first row in an Excel-like table using a pin header

Does anyone know how to create a table filter in Excel where the header and first row stay pinned while scrolling down, even after applying filters?

Here is a JSFiddle demonstrating the issue:

http://jsfiddle.net/6ng3bz5c/1/

I have attempted the following code without success:

$('tbody').scroll(function(e) { 
     $('tbody tr:nth-child(2)').css("down", $("tbody").scrollDown()); 
  });

The goal is to make the first two rows stay fixed when scrolling down. Any suggestions would be greatly appreciated.

Answer №1

Consider utilizing the CSS property position: sticky (mdn) along with first-of-type to manage the first row (in my personal project, I typically use classes for styling, but here I'm sticking with element selectors to avoid altering your markup).

Based on your specific CSS setup, it might be beneficial to adjust the top offset on the first row for better responsiveness. For this demonstration, I've chosen a fixed height that seems to work well. Additionally, don't forget to include borders where necessary.

thead th{
  position:sticky;
  top:0;
  background:white;
}
tbody tr:first-of-type td{
  position:sticky;
  top:34px;
  background:white;
}

var myApp = angular.module('myApp', [])
  .controller('employeeController', function($scope) {
    // Angular controller code goes here
})
// Sample employee data for demonstration
var employees = [
  {"Name": "Manirul Monir", "City": "Sylhet", "Country": "Bangladesh"},
  // Other employee entries follow...
];
.filterdropdown{
  // CSS styles for filter dropdown menu
}
.filterdropdown.active{
  // Additional styling for active state of dropdown menu
}
// More CSS styles related to filtering and dropdown design
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div class="col-md-6" ng-app="myApp" ng-controller="employeeController">
  <table class="table table-bordered table-condensed" data="{{XLfiltrate()}}">
    // Table structure and headers for displaying employee data
  </table>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sending Django Variable With Javascript

As a newcomer to Javascript, I am grappling with retrieving the value of a variable from my HTML form. My current code seems to be somewhat functional - I added an alert to test the logic and found that the else statement is working fine. However, I'm ...

CSS problem causing issues with block display in basic gallery

I've created this code: <head> <style type="text/css"> div.img { margin: 2px; border: 1px solid #0000ff; height: auto; width: auto; float: left; text-align: center; } div.img img { display: inline; margin: 3px; border: 1 ...

Storing input field values in JavaScript using the onchange event handler.Would you like a different revision

I am looking to calculate the area by multiplying width and height entered into input fields, and then display the result. Any guidance would be greatly appreciated. Thank you! Here is my current code. const width = document.querySelector("#width"); con ...

Understanding the functionality of $exceptionHandlerIn order to fully

While exploring angularjs, I discovered a method to manage uncaught exceptions using the built-in $exceptionHandler service. As stated in the documentation, I am manually handling exceptions by incorporating try {...} - catch(e). Below is an example: try ...

Using jQuery's ajax function to send a POST request

While following the MvcMusicStore tutorial, I decided to exercise my jQuery skills by converting a $.post() call to a $.ajax() call in order to remove an item from the shopping cart. However, I encountered a runtime error and am unsure about what went wron ...

Endless cycle of invoking functions in Angular.js

Issue with the template : <div class="container"> <h1>Process Definitions</h1> <table class="table table-stripped" > <tr> <td><strong>Name</strong></td> <td><strong>Id</st ...

The issue of importing CSS files that themselves import other CSS files via URL is causing a problem

Why is this not working correctly? I have included [email protected] A.css (css file that imports from a URL) @import url('https://fonts.googleapis.com/css?family=Source Sans Pro:300,400,600,700,400italic,700italic&subset=latin'); Ap ...

Using jquery to target and manipulate elements with the div selector

I need assistance with adding a selector on my webpage where users can choose one option and retrieve the id of the selected part. This id will then be used in a link. I am new to using jQuery, so I am having trouble implementing this feature. Below is an ...

Enhancing Your Website with Interactive Highlighting Tags

Looking at the following html: Let's see if we can <highlight data-id="10" data-comment="1"> focus on this part only </highlight> and ignore the rest My goal is to emphasize only the highlight section. I know how to emphasize a span ...

Clicking the button does not properly redirect the page as intended by window.location.replace()

When I click the button, instead of redirecting the page, it just refreshes and stays on the same page. Any idea what may be causing this? I have attempted the following: window.location = "https://stackoverflow.com/"; Current jQuery code snippet: <sc ...

CSS not being applied to Next.js HTML pages

Currently, I am utilizing Nextjs and including the flaticon css in _app.js as follows: import '../public/assets/css/flaticon.css'; In the upcoming section, an error is being encountered: @font-face { font-family: 'Flaticon'; src: ...

What is the best way to retrieve the IDs of selected items in jQuery selectables?

I have a straightforward question that I've been struggling to find an answer to. When using jQuery selectables, I want to retrieve the ID of the selected list item when it's clicked. Here is an example of my list: <ol id="selectable"> ...

How can you retrieve the id of a div element using an if/else statement in React Js?

In my React Js project, I am attempting to create a responsive connecting line between 3 divs. While successfully adding a line between the elements, I encountered an issue when using Axios to fetch data and display it with an if/else statement. The error ...

Is there a proper method in AngularJS for factories to return objects?

I am facing an issue in retrieving POST data in an Angular.js project. This is the factory I am using: angular.module('mtApp').factory('getKey', function($http) { return { getData: function(data) { var key=&apos ...

Having trouble getting the jQuery function to update text properly

I'm curious to understand the behavior happening in this code snippet. It seems like updating the list item by clicking doesn't work as expected using the initial method. But when rearranging the JavaScript code, it displays the correct value wit ...

JavaScript Tutorial: Storing HTML Text in the HTML5 Local Storage

I am looking to save the text that is appended using html() every time I click on a button. The goal is to store this text in HTML 5 local storage. $('#add_new_criteria').on('click',function(){ $('#cyc_append').html(&apo ...

Trigger a popup using the javascript .link() method

I am currently using ag-grid to present JSON data. When the values are located within nested objects, I have to utilize a valueGetter from the grid API to map to the appropriate value. The value getter returns a value for each row and the grid matches it t ...

When implementing the Slick Carousel with autoplay within an Isotope grid, the height of the image slides may occasionally reduce to 1 pixel when applying filters

I've been struggling with this issue for more than a day now, and despite searching through similar posts and trying their solutions, I still haven't been able to resolve it. Here's an example of the problem: https://jsfiddle.net/Aorus/1c4x ...

I'm looking to create a Triangle shape with CSS, any tips on how to achieve

Similar Question: Understanding the mystery behind this CSS triangle shape Please provide me with your valuable suggestions. ...

Transfer the chosen item from one select box to another select box while ensuring there are no duplicates

Currently, I have two select boxes and my goal is to transfer options from one select box to another using a button. Here is the code snippet: PHP <table> <tr> <td> <select size="5" id="suppliedMovies" name="supp ...