Is it possible to include a link at the conclusion of an Angular Material autocomplete input field?

I'm currently working on adding a persistent link at the end of an Angular Material autocomplete. I want the link to always be visible, even after a search is performed. To achieve this, I came up with a workaround involving a directive that manipulates the "md-not-found" element. Here's a glimpse of what I have implemented:

Check out the example here

Initially, everything looks as intended - the "Create User" link appears below the autocomplete results. However, upon performing a search (e.g., typing "a"), the autocomplete results container shrinks to display only one item and fails to return to its original full height.

I'm seeking guidance on how to address this issue. Alternatively, if there's a different approach to achieving the desired outcome, I'd appreciate any suggestions.

Please note that the user creation aspect is purely illustrative in this scenario.

Your assistance is greatly appreciated.

Below is the directive utilized:

(function () {
    'use strict';

    angular.module('app').directive('notFoundAlwaysVisible', notFoundAlwaysVisible)

    notFoundAlwaysVisible.$inject = ['$rootScope'];

    function notFoundAlwaysVisible($rootScope) {
        return {
            restrict: 'A',
            require: '^mdAutocomplete',            
            replace: true,
            template: '',
            link: function (scope, element, attrs, parentCtrl) {
                parentCtrl.notFoundVisible = function () { return true; }
                return '';
            }
        }
    }
})();

Here's my autocomplete element integrated with the new directive:

<md-autocomplete md-no-cache="true" 
                    not-found-always-visible
                    md-selected-item="ctrl.item" 
                    md-items="item in ctrl.querySearch(ctrl.searchText)" 
                    md-search-text="ctrl.searchText" 
                    md-item-text="item.Name" 
                    md-min-length="0" 
                    md-floating-label="Username" 
                    md-select-on-match="true"
                    md-match-case-insensitive="true">
      <md-item-template>
        <div>
          <span md-highlight-text="ctrl.searchText">{{ item.Name }}</span>
        </div>
      </md-item-template>
      <md-not-found>
        <a href="#">Create User {{ ctrl.searchText }}</a>
      </md-not-found>
    </md-autocomplete>

Answer №1

Check out this for assistance. You have the option to insert your link at the end of the outcomes and apply a separate stylesheet specifically for that link.

<md-autocomplete ng-disabled="ctrl.isDisabled" md-no-cache="ctrl.noCache" md-selected-item="ctrl.selectedItem" md-search-text-change="ctrl.searchTextChange(ctrl.searchText)" md-search-text="ctrl.searchText" md-selected-item-change="ctrl.selectedItemChange(item)" md-items="item in (ctrl.querySearch(ctrl.searchText).concat(ctrl.footerOption))" 
       md-item-text="item.display" md-min-length="0" placeholder="What is your favorite US state?"  md-menu-class="autocomplete-custom-template">
        <md-item-template>
         <div ng-class="{fixed:item.isFooter}">
           <span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span>
          </div>
        </md-item-template>
        <md-not-found>
          No states matching "{{ctrl.searchText}}" were found.
          <a ng-click="ctrl.newState(ctrl.searchText)">Create a new one!</a>
        </md-not-found>
      </md-autocomplete>

To monitor the footer, use this method:

function selectedItemChange(item) {
      if (item) {
        if (item.isFooter)
           $log.info('footer selected, creating a new'  + JSON.stringify(item));           
        else           
          $log.info('Item changed to ' + JSON.stringify(item));           
      }
    }

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

Disappearing a menu list while zooming in on a page: The art of vanishing navigation

[QUESTION] Is there a way to make the Menu List disappear when zooming in on a webpage? For example: <-- Normal Page [No Zoom] --> [Logo] Profile Gallery Guestbook <-- Zoom In 120% --> [Logo] Profile Guestbook <-- Zoom In 150% --> ...

React-native - Dropdownpicker - Error: Unable to retrieve label for selected choice

I'm encountering an issue with DropDownPicker in my react-native project during page load Here is the code snippet where I am using DropDownPicker: <DropDownPicker items={[ { la ...

When it comes to retrieving values from JSON objects in JavaScript, one of two identical objects will return the expected values while the other may

I encountered a perplexing issue with two identical JSON objects. When I use JSON.stringify(obj1) == JSON.stringify(obj2), it returns true. Despite both objects being accessible and inspectable in the console, I'm facing difficulty accessing the valu ...

Using a computed property setter in Vue.js/Javascript while focusing on a datepicker can lead to crashing the browser

Can anyone explain why my javascript / vuejs code is crashing on my JSFiddle when I focus on the start date datepicker (causing the browser to hang)? If you uncomment the endDate computed property and comment out the current one, it works fine but the fun ...

Error message: validator is not defined when integrating jquery.validate with jquery.uploadfile package

Currently, I am facing an issue while attempting to incorporate both jquery.validate and jquery.uploadfile on the same page. The error message I'm receiving is: TypeError: validator is undefined if ( validator.settings.rules ) { Interestingly, if ...

What causes axios to return a z_buf_error?

I've encountered an issue with axios returning an error cause: Error: unexpected end of file at BrotliDecoder.zlibOnError [as onerror] (node:zlib:189:17) { errno: -5, code: 'Z_BUF_ERROR' I'm puzzled as to why this functio ...

The Express/Mongoose route consistently modifies the identical item

I'm encountering an issue where any attempt to update or create a new item results in only creating one item and then updating that same item regardless of the input. Is there something incorrect with this route? // @route POST api/item // @desc ...

Instructions for activating column resizing in MUI DataGrid

Is there a way to enable column resizing for users in MUI DataGrid? It's enabled by default on XGrid, but I would like to enable it on Datagrid as well. Any assistance is appreciated. <DataGrid className={classes.table} ...

What is the best way to determine the maximum length of an input field?

Is it possible to validate the maximum length in Angular? For instance, if I have a input field like this: <input type="text" class="form-control font28 centerText" ng-model="ticketPin" maxlength="10" only-digits mask="9999999999" restrict="reject" c ...

Incorporating <span> elements into a comma-separated list using Jquery on every other item

When I receive a comma-separated list of items from a database and insert them into a table cell, I want to apply alternating styles to make it easier for users to distinguish between them. For example: foo, bar, mon, key, base, ball I'm looking to ...

Dynamic css property implementation in Vue list rendering

I am currently working on creating a carousel-style list of items similar to the iOS native date/time pickers, where the list can be shifted both upwards and downwards. The positioning of each item is determined by the `top` property from a model, which i ...

How can I add a new property to an object type within an Interface in TypeScript?

I'm currently exploring how to merge declare an interface, with the twist of adding a property to the object literal type instead of directly to the interface itself. Within a library, I have a type that looks like this: interface DefaultSession { ...

Access exclusive content by subscribing now!

How can I return a reference to a subject from a service without allowing the receiver to call .next() on the subject? Let's say there is a service with a subject that triggers new events. class ExampleService { private exampleSubject = new Subjec ...

Display irregularly spaced time samples from a MySQL database on a Google Line Chart using a PHP query

Currently, I am utilizing a Line Chart from Google to display data fetched from a MySQL database involving various variables at different time intervals. Although the sample time is set at 1 minute, there are occasions where some data points are lost (for ...

What is the process for converting a string into a date while disregarding the time zone

Due to the way dates are stored, it is important for me to retrieve them exactly as they are stored, but time zones are causing issues. moment("2020-10-28T08:41:00.000Z").format("YYYY-MM-DD HH:mm") // Result: 2020-10-28 09:41 However, ...

discord.js: Imported array not displaying expected values

I've been facing an issue with accessing elements from an imported array. Even though the array is successfully imported, attempting to access its elements using [0] results in undefined. Here's how I exported the array in standList.js: exports. ...

What is the best way to verify the type of an object received from request.body in Typescript

Is it possible to check the object type from the request body and then execute the appropriate function based on this type? I have attempted to do so in the following manner: export interface SomeBodyType { id: string, name: string, [etc....] } ...

Show a specific form field based on the chosen option in a dropdown menu using Angular and TypeScript

I am looking to dynamically display a form field based on the selected value from a dropdown list. For instance, if 'first' is chosen in the dropdown list, I want the form to remain unchanged. However, if 'two' is selected in the drop ...

Controlling the Number of Typeahead Suggestions in AngularJS Bootstrap

Looking for a way to restrict user input in a text field using typeahead with results from an array? The goal is to limit users to selecting only items present in the array. For example, with an array like this: ['alison','bentley',&a ...

"Use jQuery to toggle the slide effect for the first element of a

Below is the HTML code snippet: <div class="row header collapse"> Content 1 <i class="fas fa-chevron-circle-up" ></i> </div> <div class="instructions-container"> <div></di ...