Create a custom implementation of Bootstrap's Typeahead feature using Angular, CSS, and HTML without relying on any pre-existing libraries

Is there a way to achieve bootstrap's typeahead suggestive hint feature using only angular, html, and css? I want to dynamically display the first item of the search results in the search box!

<body>
    <div ng-app="demoApp" ng-controller='demoCtrl'>
        <br>
        <input type="text" ng-model="search_term" placeholder="search...">
        <br>
        <div ng-repeat="(index, item) in items | filter:search_term" ng-class="{'is-first' : index == 0}">{{item}}</div>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
    <script>
        var app = angular.module('demoApp', []);
        app.controller('demoCtrl', ['$scope', function($scope) {

            $scope.items = [
                'Apple',
                'Apricot',
                'Avocado',
                'Banana',
                'Bilberry',
                'Blackberry',
                'Blackcurrant',
                'Blueberry',
                'Boysenberry',
                'Cantaloupe',
                'Currant',
                'Cherry',
                'Cherimoya',
                'Cloudberry',
                'Coconut',
                'Cranberry',
                'Damson',
                'Date',
                'Dragonfruit'
            ];
        }]);
    </script>
    <style>
        /* put any CSS you need here */
        .is-first {
            color:red;
        }

    </style>
</body>

Any assistance is greatly appreciated!

Answer №1

After some consideration, I implemented a solution using ng-change to toggle the visibility of a duplicated input field that displayed the first result of the search filter:

<body>
<div ng-app="demoApp" ng-controller='demoCtrl'>
    <br>
    <input class="main" type="text" ng-model="search_term" placeholder="search..." ng-change="showSuggest()">
    <br>

    <div ng-show="suggestion" class="autocomplete" ng-repeat="(index, item) in items | filter:search_term | limitTo:1">
        <input  disabled type="text" placeholder=" {{ item }}">
    </div>
    <div ng-repeat="(index, item) in items | filter:search_term" ng-class="{'is-first' : index == 0}">{{item}}</div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script>
var app = angular.module('demoApp', []);
app.controller('demoCtrl', ['$scope', function($scope) {
    $scope.suggestion = false;
    $scope.items = [
        'Apple',
        'Apricot',
        'Avocado',
        'Banana',
        'Bilberry',
        'Blackberry',
        'Blackcurrant',
        'Blueberry',
        'Boysenberry',
        'Cantaloupe',
        'Currant',
        'Cherry',
        'Cherimoya',
        'Cloudberry',
        'Coconut',
        'Cranberry',
        'Damson',
        'Date',
        'Dragonfruit'
    ];

    $scope.showSuggest = function(){
     if ($scope.search_term.length < 1)
            {$scope.suggestion = false}
     else {$scope.suggestion = true}
 };

}]);
</script>
<style>
/* put any CSS you need here */
.is-first {
    color:red;
}

.main{
    position: absolute;
    top: 10;
    left: 10;
    z-index: 11;
    background: transparent;

}
.autocomplete{
    position: absolute;
    top: 10;
    left: 10;
    background: transparent;
    z-index: 10;
}

</style>

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

The dropdown menu items spill out beyond the confines of the container

I'm currently facing some challenges and struggling to find a solution. Unfortunately, I haven't been able to find any guidance on this issue. Important: To view the output, click on "Run snippet" and make it full screen by clicking on the hamb ...

What steps should be taken to resolve the Error encountered while testing Angular Js in netbeans?

After trying to implement the steps outlined in the link below I keep encountering the following error: Exception in thread "main" java.lang.NoClassDefFoundError: com/google/jstestdriver/hooks/TestListener at java.lang.Class.getDeclaredMethods0(Nativ ...

PHP and JavaScript Form: Include the page URL in the email form when submitting

After submitting a basic form, I am struggling to include the URL of the page in the email notification to track where the user completed the form. I have attempted various solutions from this forum, but unfortunately none of them seem to work. How can I ...

Div with inline styling featuring nowrap and hidden overflow effect

My top bar contains multiple div elements. Everything looks good in Chrome, but in Firefox, the .third div wraps to a second row. How can I ensure that the .third div behaves like it does in Chrome with nowrap in Firefox? http://jsfiddle.net/qhoc/C6f4c/ ...

Incorporate CSS styling within a PHP document

Many individuals are aware that HTML can be embedded within a PHP file. However, can CSS also be included in a PHP file and accessed using <link rel="stylesheet" type="text/css" href="mystyle.php" /> to make it act as a CSS file? While it is possib ...

Which one relies on the other: angular-data or angular-cache?

I'm struggling to get angular-cache set up properly. According to the documentation: Angular-cache is a dependency of angular-data and must be loaded before angular-data if you are using angular-data. That's all well and good, but what if I only ...

What methods can we employ to obtain a list of the most popular search results in Django Rest Framework by utilizing Elastic Search

My technology stack includes Django 2.1, Elastic Search 6.1, and Django-Elasticsearch-DSL-DRF 0.17.6. Here is the code from my view.py file: class SchoolViewSet(DocumentViewSet): document = SchoolDocument serializer_class = SchoolDocumentSerializ ...

Disregard the Margin of Ancestor Elements

I'm attempting to make an image span the full width of a parent element, but the parent element has a margin of 10px. This means that when I apply #parent img { position: absolute; top:0; left:0; height: auto; width: 100% } It on ...

Struggling to vertically center input within Bootstrap 4 column

I'm attempting to design some form elements and align the inputs vertically within the columns. I've experimented with vertical-align:middle; and margin:auto 0;, however, neither of these solutions seem to be effective. input { vertical-alig ...

Is there a way for me to edit the HTML file in Shopify?

I'm currently navigating my way through Shopify and finding it to be a bit perplexing. Although I understand what changes need to be made and how to make them, I am struggling to locate the HTML file that requires editing. Despite clicking on Online S ...

Differences between Tags and Elements in HTML5

Could someone provide a detailed explanation of the distinctions between tags and elements in HTML5? I am curious about this topic because I have noticed a lot of confusion regarding the terminology online, with the terms being used interchangeably even o ...

What could be causing the issue with my HTML drop down menu links not functioning properly

I'm a novice in the world of web design. I recently crafted two drop-down menus for my website, one for projects and the other for labs. While the drop-down functionality itself is operational and the links are clickable, they fail to redirect users t ...

Button to access overlay menu on my map with OpenLayers 3

I am trying to add a menu button on top of my map that, when clicked, will display a window. I have created the map div and the overlayMenu button div, but unfortunately, the button is not showing up where I want it to. I would like the button to be locate ...

I am having trouble aligning the unordered list in the center

My struggle lies in centering the ul element which serves as a menu. Despite trying various CSS properties such as margin: 0 auto;, text-align: center;, and adjusting the margin value, I am unable to achieve the desired result. This is the CSS code I have ...

Angular is facing difficulties in loading JSON data from Rails

Within my Rails backend, I have a method designed to retrieve data from the database and convert it into JSON: def index @airports = Airport.select('id,city,country') render status:200, json: { airports: @airports } end In addition, ...

Refining the options in security question dropdown menus

Firstly: The title should mention filtering security options in dropdown lists, but it seems I'm restricted from using the term questions or question in the title. I came across this code example, but it appears to be outdated. Does anyone know why a ...

Having issues getting Toggle Switches to function properly in Asp.Net Webforms

I am in need of a toggle switch that can be toggled on or off depending on the user's preference. Here is a CSS code snippet that I have come across in various examples: .switch { position: relative; display: inline-block; width: 60px; hei ...

When clicked, an error is triggered in Firefox but not in Chrome

Can you help me with this code snippet? <button onclick="return confirm('Are you sure?');"> <a href="home.php">Go Home</a> </button> I am trying to display a confirmation message when the user clicks on the b ...

Unusual problem encountered on mobile devices with custom font-face and Font Awesome font combination

Experiencing difficulties with font-face implementation on mobile devices. The custom font-face definition for the Rex font is as follows: @font-face { font-family: 'RexBold'; src: url('RexBold.eot?#iefix') format ...

Is there a way to remove any incomplete information from the output (window.alert) in JavaScript when a user does not fill in all the prompts?

I am seeking input for 8 pieces of information to be displayed in an alert box, only if the user enters something in each field. All the gathered data will be shown in a single alert box once the user confirms their desire to review it. If the user choos ...