Dynamic search for selection

Asked

Viewed 211 times

0

I need to in a text field by clicking the F2 key to open a query window and selecting the employee to take code from it to the text field.

Would you have some library in jquery or other solution?

  • You can start with the function keypress

  • @Andersoncarloswoss Until then I know how to call a query window, but I don’t know how to return the value of the query to the text field.

  • Then edit your question and be clearer on your needs. Post the codes you’ve already done and detail exactly what the code should do.

2 answers

0

Function keys (F1, F2, F3, F4...) are usually already used for certain functions of the browser, so it is not possible to monitor them. If you want to capture any other key you can simply use the code below:

  function teclaPressionada(){
     alert("tecla" + event.keyCode);
  }
document.onkeypress = tecla;

So you will discover the key code when pressing any key with the page html opened. After that just mount the call to the function you want

  • Yes so far know, more how to do that when selecting the employee in the query he close the window and pass the code to the text field?

  • Closing the window depends on the way you are opening, script, framework and etc. I think your close button will work perfectly

  • About getting the code selected, I’m putting the script in the answer

  • I managed to solve.

0

Consider a input:

<form>
  <label>
    Qual é o seu número da sorte?
    <input name="number" type="text" />
    <sub>Pressione <kbd>F2</kbd> para exibir a lista.</sub>
  </label>
</form>

To check the key pressed, use the function keypress jQuery:

$("input[name=number]").keypress(event => {
    event.preventDefault();
    if (event.key == "F2")
    {
        // A tecla F2 foi presisonada.
    }
});

To open a new window, it will depend a lot on the logic of your application. As you were not very clear about the problem, I used a simple modal window of Bootstrap CSS:

<div id="modal" class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Número da Sorte</h4>
      </div>
      <div class="modal-body">
        <fieldset>
          <input type="radio" name="my_number" value="1" /> 1
          <input type="radio" name="my_number" value="2" /> 2
          <input type="radio" name="my_number" value="3" /> 3
          <input type="radio" name="my_number" value="4" /> 4
          <input type="radio" name="my_number" value="5" /> 5
        </fieldset>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
        <button type="button" class="btn btn-primary" id="modal-save">Salvar</button>
      </div>
    </div>
  </div>
</div>

To display it, it’s enough:

$('#modal').modal('show');

Staying, then:

$("input[name=number]").keypress(event => {
    event.preventDefault();
    if (event.key == "F2")
    {
        // A tecla F2 foi presisonada.
        $('#modal').modal('show');
    }
});

In the modal window, the user selects the desired value and clicks the "Save" button. When this is clicked, we update the value of the input:

$("#modal-save").on("click", (event) => {
  event.preventDefault();
  const value = $("input[name=my_number]:checked").val();
  $('#modal').modal('hide');
  $("input[name=number]").val(value);
});

The complete code, with a functional example, can be seen below:

$(() => {
  $("input[name=number]").keypress(event => {
    if (event.key == "F2")
    {
      $('#modal').modal('show');
    }
  });
  
  $("#modal-save").on("click", (event) => {
    event.preventDefault();
    const value = $("input[name=my_number]:checked").val();
    $('#modal').modal('hide');
    $("input[name=number]").val(value);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<form>
  <label>
    Qual é o seu número da sorte?
    <input name="number" type="text" />
    <sub>Pressione <kbd>F2</kbd> para exibir a lista.</sub>
  </label>
</form>

<div id="modal" class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Número da Sorte</h4>
      </div>
      <div class="modal-body">
        <fieldset>
          <input type="radio" name="my_number" value="1" /> 1
          <input type="radio" name="my_number" value="2" /> 2
          <input type="radio" name="my_number" value="3" /> 3
          <input type="radio" name="my_number" value="4" /> 4
          <input type="radio" name="my_number" value="5" /> 5
        </fieldset>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
        <button type="button" class="btn btn-primary" id="modal-save">Salvar</button>
      </div>
    </div>
  </div>
</div>

  • I changed the javascript to be able to read F2, like with onkeypress that passed me does not read keys that do not generate characters, so I switched to keyup that reads keys precionadas by code: document.onkeyup=function(e){&#xA; if(e.which == 113){&#xA; $('#modal').modal('show');&#xA; }&#xA; $("#modal-save").on("click", (event) => {&#xA; event.preventDefault();&#xA; const value = $("input[name=my_number]:checked").val();&#xA; $('#modal').modal('hide');&#xA; $("input[name=number]").val(value);&#xA; });&#xA;}

  • And what do you mean by that?

  • Sorry I hit enter unintentionally. I already edited the comment. As you’ve given me, I believe it will work, I will now have to create a dynamic consultation within the modal. Thought it might have a function inside library in jquery that would do this.

  • Exactly. Here it worked, but I read that the behavior may vary depending on the browser. If the modal content is dynamic, you can use AJAX requests with jQuery even to load it.

  • Yes I will. Strange that I’ve seen somewhere running with a simpler window, as if it were a direct jquery. To find the code of a product simply clicked F2, it brought a Datepicker type window of Jquery with all products, and in the search field was just typing and it was filtering according to the query, after 2 clicks it passed the code to the text field.

  • If you find it again, you can pass here the link that we have analyzed how it was done.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.