Searching in Datatables by the first letter of each column

I'm currently developing a web app with a datatable functionality. I have implemented both a global search bar and individual search bars in each column, similar to this example: https://datatables.net/examples/api/multi_filter.html. Now, my goal is to enable searching by the first letter only in each individual column.

$(document).ready(function() {
   var table=$('#example').DataTable( {
       responsive: true
   } );
    $('.dataTables_filter input').on( 'keyup', function (e) {
      var regExSearch = '\\b' + this.value;
    table.search(regExSearch, true, false).draw();
   });
} );

However, using the above approach does not yield the desired outcome for me. I have structured my individual searches as follows:

table.columns(':gt(1)').every( function () {
        var that = this;

        $( 'input', this.footer() ).on( 'keyup change clear', function () {
            if ( that.search() !== this.value ) {
                that
                    .search(this.value )
                    .draw();
            }
        } );
    } );

I attempted to adapt the global search logic by adding .search('\\b' +this.value ), but my searches return no results. It's possible that I am overlooking something, could someone provide guidance on how to achieve this?

Answer №1

Almost there, but I finally figured it out!

table.columns(':gt(1)').every( function () {
        var that = this;

        $( 'input', this.footer() ).on( 'keyup change clear', function () {
            if ( that.search() !== this.value ) {
                that
                    .search('\^'+this.value, true, false ) // regex=true, smart=false
                    .draw();
            }
        } );
    } );

Success!

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

Issue encountered: Django Rest Framework functionality fails to execute properly when accessed from varying URLs

I'm currently immersed in the tutorial found at: http://www.django-rest-framework.org/ as I endeavor to construct a restful API for my django application, which is being hosted on apache. My ultimate goal is to utilize ajax in order to call the API an ...

Creating an extensive amount of HTML content through the use of JSON

When I request data from my API, it returns JSON objects which then populate numerous divs with content. However, I find myself continuously using concatenation to insert object properties. Is there a more efficient method for achieving this? $.each(JSO ...

Versel TurboRepo is facing an issue during the installation of the expo router, causing a conflict

Utilizing TurboRepo for building a monorepo and experimenting with the react-native-web example to kickstart a full expo react-native-web implementation. I'm facing difficulties installing expo-router correctly within the native project. Despite thor ...

Conceal varying numbers of rows in a table using Jquery based on specific

I am attempting to hide certain table rows, but I am encountering an issue. Currently, when I click on a row with the class 'plevel1', it hides the following row with the class 'plevel2'. However, when I click on the top parent level ro ...

Repeating single records multiple times in AngularJS with ng-repeat

I've been working on a project using AngularJS and have encountered some strange behavior with ng-repeat. My controller is returning data to ng-repeat as follows: ..... //Other JS Functions ..... var app = angular.module('main', ['ngTa ...

Leveraging Vue.js's computed properties to access and manipulate elements within an

I have a simple template that displays text from a wysiwyg editor using two-way data binding, as shown below: <template> <div> <quill-editor v-model="debounceText" :options="editorOptionProTemplate" > </qu ...

How can you efficiently identify and resolve coding errors or typos in your code?

While practicing node.js and express.js, I encountered an issue with finding typos. One such instance was when I mistakenly typed: const decoded = jwt.veryfy(token, config.get('jwtSecret')); instead of jwt.verify. Even though I eventually disc ...

Is there a valid justification for jQuery altering the 'this' keyword in event handlers?

Consider this common scenario: console.log(this); // window or any parent object $('.selector').on('click', function(event) { console.log(this); // clicked DOM element }); var myFunc = function() { console.log(this); // windo ...

What is the recommended placement for the implicitlyWait method in Protractor?

When implementing implicitlyWait, what is the appropriate location to include browser.manage().timeouts().implicitlyWait(5000); within the test script? ...

Determine the dimensions of a div element in IE after adjusting its height to auto

I am currently working on a JavaScript function that modifies the size of certain content. In order to accomplish this, I need to obtain the height of a specific div element within my content structure. Below is an example of the HTML code I am dealing wit ...

Communication between child and parent components in Vue.js is an essential feature

Attempting to invoke functions from my component to Vue for the login process. This is the code snippet of my component : Vue.component('auths', { data: function() { return { ip: '', sessiontoken: '' } ...

What is the best way to set a button as the default option when it is pressed down?

<div class="input-group"> <input type="text" id="srcSchtext" class="form-control" runat="server" placeholder="Search here..." /> <span class="input-group-btn"> <asp:Button runat="server" ID="btnSearchEmployee" ...

Leveraging the MQTT.js (node package) in an electron application to dynamically update the Document

I am currently delving into electron and node.js in my project that involves working with mqtt to dynamically update the DOM. I am receiving messages via mqtt in my main.js through the onmessage function. Here is a snippet of my main.js: const { app, Brows ...

Despite encountering an error in my terminal, my web application is still functioning properly

My code for the page is showing an error, particularly on the home route where I attempted to link another compose page The error message reads as follows: Server started on port 3000 Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after t at new Node ...

Retrieve the accurate file name and line number from the stack: Error object in a JavaScript React Typescript application

My React application with TypeScript is currently running on localhost. I have implemented a try...catch block in my code to handle errors thrown by child components. I am trying to display the source of the error (such as file name, method, line number, ...

Understanding the correct Accept Content-Type selection in ExpressJS

I'm struggling to differentiate an AJAX call from other calls in ExpressJS. From what I gather, can I use request.accepts('json') to recognize a JSON request? The issue is - it seems like every call accepts everything! app.get( '*&ap ...

Flipping the order of .each() iterations in MongoDB

Working with MongoDB using Mongoose: I'm currently facing an issue where my code fetches the correct documents from the database, but they are sent to the client in the wrong order. I attempted to use another sort command after the .limit() function ...

The JavaScript date picker is malfunctioning in the HTML editor, but it functions properly in Fiddle

I have a working format available in a JS fiddle. Here is the code I have used on my demo site: I created a new folder named "js" and placed datepicker.js inside it, then linked it in my HTML like this: <script type="text/javascript" src="js/datepicke ...

What is the most effective method for refining options in a select input field?

After receiving a response from the API, the JSON data looks like this: [ { "id": 6, "nombre": "Pechuga de pollo", "categoria": "Pollo", "existencia": 100 }, { "id": 7, "nombre": "Pierna de pavo" ...

Error encountered: Circular reference issue was encountered when attempting to retrieve navigator.plugins with the use of Selenium and Python

I'm attempting to access the value of navigator.plugins from a Selenium-driven ChromeDriver initiated google-chrome Browsing Context. Using google-chrome-devtools, I am able to retrieve navigator.userAgent and navigator.plugins as shown below: https ...