How to make a Kendo Grid Combobox template or editor automatically set focus and select all text

I've been experimenting with using the combobox as either an editor or template for a column. The key difference is that one (the template) is always visible, while the other (the editor) is only displayed when the user edits a cell in the column. I've been tackling this issue for hours now, and I'm at a loss on what to try next.

What I aim to achieve is that when the user tabs into a cell containing either option, the combobox becomes active (this functionality works for the editor but not the template), and the current text is automatically selected (meaning if the user starts typing, the existing value gets cleared for the new input).

You can find the complete sample here

So, I have encountered 2 main challenges here: 1. How do I set focus on the combo box used as a template? I made attempts in 2 places - initially in the template itself, where I acknowledge the syntax is incorrect, but I'm unsure of the correct approach...

function createComboTemplate(data, field) {
  var tmpl = '<input style="width:100%" ' +
   'kendo-combo-box ' +
   'text-field="\'display\'" ' +
   'suggest = true ' +
   'filter="\'contains\'" ' +
   'value-field="\'rego\'" ' +
   'source="dataItem.carSource"' +
   'data-value="dataItem.car.rego" ' +
   'k-on="dataItem.onFocus(e)" ' +  // how to do this?
   'k-on-change="dataItem.handleDDLChange(kendoEvent, dataItem, \'' + field + '\', this)"/>';

 return tmpl;
 }

Regarding the line 'k-on="dataItem.onFocus(e)" ' above, it currently serves no function. I was attempting something like:

"var combo = input.data("kendoComboBox");
  combo.input.on("focus", function () {"...

which is somewhat similar to what I tried below in the second issue related to the editor (as seen further down)

Next, I attempted focusing within the following section..

beforeEdit: function (e) {
        if (e.field === "car.display") {
            e.preventDefault();
            var grid = $("#gridID").data("kendoGrid");
            var current = grid.current();
            var input = current.find("k-input")
            var combo = input.data("kendoComboBox");
            console.log("combo is " + combo);
            // combo is null
            if (combo != null) {
                combo.focus();
            }

It seems that I am facing trouble obtaining the combo instance here (it's consistently showing up as null) Where did I go wrong in this scenario?

2. The second issue pertains to the editor...

    function createCombo(container, options, data) {

     var input = $('<input/>')
     input.appendTo(container)
     input.kendoComboBox({
     autoBind: true,
      ...

     var combo = input.data("kendoComboBox");
     combo.input.on("focus", function () {
      var element = this;
     setTimeout(function () {
      // select all text?
      combo.select();
  });
 });
 }

In the snippet above, the focus handler functions correctly, yet the combo.select(); does not end up selecting the text

To sum it up, there are various components to address, yet my goal remains consistent across both scenarios - ensuring that the combo is selected upon Tab usage and that all text is highlighted.

Any assistance provided here would be immensely appreciated! Thank you in advance

Answer №1

This solution worked perfectly for me. It offers a more streamlined and elegant approach that can be applied to any ComboBox on the page, regardless of when it appears (since Kendo automatically adds the role="combobox" attribute):

$(document).on('focus', 'input[role="combobox"]', function () {
    $(this).select();
});

Answer №2

Doesn't quite have that angular feel, but it seems to do the trick:

$("#gridID").on("focus", ".k-input", function() {
    var parent = $(this).closest(".k-combobox");
    if (parent.length >0) {
        var input = $(parent).find("input:not(:visible)");
        var combo = $(input).getKendoComboBox();
        if (combo) {
            combo.input.select();
        }
    }
})

and

beforeEdit: function (e) {
    if (e.field === "car.display") {
        e.preventDefault();
        var current = this.current();
        $(current).find(".k-input").first().focus();
    }
},

(demo)

You could also achieve this in beforeEdit (specifically for the car column since the editor may not be available yet in other cases):

beforeEdit: function (e) {
    if (e.field === "car.display") {
        e.preventDefault();
        var current = this.current();
        var input = current.find("input:not(:visible)")
        var combo = input.data("kendoComboBox");
        if (combo) {
            combo.input.select();
        }
    }
},

(demo)

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

Fetching information from the database and presenting it in a div using AJAX, jQuery, and CodeIgniter

I am trying to implement a functionality where data from the database is displayed in one div upon clicking a button. Here is my controller : function search_course() { $this->load->view('pages/doctor-search'); ...

"Troubleshooting: Inability to send emails through PHP mail script while using jQuery's

I'm at a loss with this issue. I'm attempting to utilize the jQuery ajax function to send POST data to an email script. Below is the jQuery code snippet. $('#bas-submit-button').click(function () { var baslogo = $('input#bas- ...

Sending a Secure Identifier through jQuery Function

When working with MVC, I have utilized jquery templates. The templates are currently returning an ID, which is then passed through a link I have set up. However, I am looking to enhance security by encrypting the ID using a server-side function. {#templ ...

Switching the border of a div that holds a radio button upon being selected

I have a piece of code that I use to select a radio button when clicking anywhere within a div, which represents a product photo. To make it clear for the customer, I want to add a border around the selected product. Here is the initial code: <script t ...

Chrome and Safari browsers are both experiencing a glitch where the Full Calendar event time is automatically adding an

Currently, I am utilizing Jquery fullcalendar 3.3.1 and moment.js 2.15.1 to display an event calendar. When clicking on an event, a modal popup showing the event details appears. The event details are stored in an SQL database, and ajax is used to populate ...

Basic image slider featuring adjustable heights

I'm currently working on a basic content slider, but I've encountered an issue where the container doesn't expand as new content slides in. It seems to be related to the absolute positioning of the content items. If I don't use absolute ...

Transferring information from AJAX to PHP script with the click of a button

Simply put, I am in the process of adding a pop-up update panel to my to-do website using an HTML button. The website already has a login-register system and uses MySQL queries to display different tables for each user. The update buttons on the website c ...

Issue with unrecognized expression in JQuery when processing Ajax response

Recently, I implemented a JQuery Ajax Form on my website. $('#modal-body-sign-in').on('submit', '#sign-in', function(e) { e.preventDefault(); var data = $(this).serialize(); var url = $(this).attr(&apo ...

Mastering form reset functionality using jquery

When attempting to register, an error is generated if any field is left blank in the form (from PHP). The input fields that have been filled out are retained using Smarty: {if isset($smarty.post.registratie.naam)} value="{$smarty.post.registratie.naam}"{el ...

Obtain the printed value of a button via PHP and manipulate it using JavaScript

I have a dynamic table that displays results from a database. <table id="table" class="table datatable-responsive"> <thead> <tr class="bg-teal"> <th>#</th> <th>Date & Time</th& ...

jQuerry's on method fails to respond to newly added elements through the clone() function

I always thought that jquery's on() function would handle events for dynamically added elements in the DOM, such as those added via ajax or cloning. However, I'm finding that it only works for elements already attached to the dom at page load. Cl ...

How to Effortlessly Populate Cascading Dropdowns in ASP.Net MVC 5 using a Reusable

I am currently working on an ASP.Net MVC 5 application that has multiple edit pages. Each edit page consists of various input elements such as textboxes, checkboxes, and dropdowns. I want to implement a functionality where the values of one dropdown are ch ...

JQuery functions for click and hover are not functioning as expected on a div element

I am having trouble with a div that I have declared. The click event is not working as expected, and I also need to use the :hover event in the css, but it isn't functioning either. What could be causing this issue? <div id="info-button" class="in ...

Exploring a JavaScript object to verify if a property contains nested data elements

I am currently working on traversing through the object above in order to determine if a contact is a member of a specific list. For instance, if the user is a member of the list with an ID of 2022, I want to display their first name (which is also includ ...

Mastering the Art of Live Search in Drop Down Multi Select

I need to develop a search functionality similar to the one found on . This search should allow users to select options from a dropdown menu or type their own search query, and provide live search results. I attempted to use the jQuery plugin https://www.j ...

Exploring the power of jQuery closures and handling events like mouseover and mouseout

I'm currently grappling with the concept of utilizing closures in conjunction with jQuery event functions. The challenge I am facing involves creating rounded shapes on the screen that stop and fade when hovered over, then resume fading when the mous ...

Using jQuery and AJAX to prevent the default behavior of a link, initiate an AJAX request, and then navigate to the link afterwards

Attempting a seemingly 'simple' task has turned into quite the challenge for me. My goal is to have a user click a link, prevent the default action, execute an AJAX function, and then proceed to follow that link. So far, my attempts have only re ...

Switching from using jQuery's $.ajax method to Angular's $http service

I have been working on a jQuery code snippet that works seamlessly across different origins: jQuery.ajax({ url: "http://example.appspot.com/rest/app", type: "POST", data: JSON.stringify({"foo":"bar"}), dataType: "json", contentType: "a ...

The current layout of the div is hindering the ability to switch from vertical scrolling to horizontal scrolling

I'm currently utilizing this scroll converter tool to transform vertical scrolling into horizontal scrolling. However, despite correct script inclusion and initialization, the conversion is not working as expected. It seems like there might be an issu ...

The link in the drop down select is not functioning in IE8/9. When trying to open the drop down select link, an

Could use some help with IE8 and 9 compatibility, can't seem to find a solution. This code works smoothly on Chrome, FF, and Safari. There are two dropdown menus, each with two links. Every dropdown menu has its own "Buy Now" button. Upon selecting ...