How to configure ajax select2 for working with a barcode scanner












0















I have an ajax select2 (v 3.5.4) perfectly working but now I need it to work with a barcode scanner.
I need to detect when enter key is pressed. The scanner inserts code into select2 input followed by an enter key.



The problem is the scanner is too fast and the ajax call does not get to finish before enter key is pressed.



This is my code so far:



$('#my_select').select2
initSelection: (elm, callback) ->
data =
id: $(elm).data "record-id"
name: $(elm).data "record-text"
callback(data)
ajax:
url: url
dataType: "jsonp"
quietMillis: 100
data: (term, page) ->
query: term
limit: 10
page: page
results: (data, page) ->
more = (page * 10) < data.total

results: data.records
more: more









share|improve this question

























  • Add your Relevent code what you have done so far..!!!

    – Haresh Vidja
    Sep 12 '16 at 11:31











  • @HareshVidja sorry, I've added my code to the question

    – nigal
    Sep 12 '16 at 11:42











  • Do you mean, enter is pressed in search textbox?

    – dns_nx
    Sep 12 '16 at 12:58











  • @dns_nx yes. Imagine search textbox has focus. Then if you scan the code 12345 the scanner enters '12345' and Enter key.

    – nigal
    Sep 12 '16 at 13:06
















0















I have an ajax select2 (v 3.5.4) perfectly working but now I need it to work with a barcode scanner.
I need to detect when enter key is pressed. The scanner inserts code into select2 input followed by an enter key.



The problem is the scanner is too fast and the ajax call does not get to finish before enter key is pressed.



This is my code so far:



$('#my_select').select2
initSelection: (elm, callback) ->
data =
id: $(elm).data "record-id"
name: $(elm).data "record-text"
callback(data)
ajax:
url: url
dataType: "jsonp"
quietMillis: 100
data: (term, page) ->
query: term
limit: 10
page: page
results: (data, page) ->
more = (page * 10) < data.total

results: data.records
more: more









share|improve this question

























  • Add your Relevent code what you have done so far..!!!

    – Haresh Vidja
    Sep 12 '16 at 11:31











  • @HareshVidja sorry, I've added my code to the question

    – nigal
    Sep 12 '16 at 11:42











  • Do you mean, enter is pressed in search textbox?

    – dns_nx
    Sep 12 '16 at 12:58











  • @dns_nx yes. Imagine search textbox has focus. Then if you scan the code 12345 the scanner enters '12345' and Enter key.

    – nigal
    Sep 12 '16 at 13:06














0












0








0








I have an ajax select2 (v 3.5.4) perfectly working but now I need it to work with a barcode scanner.
I need to detect when enter key is pressed. The scanner inserts code into select2 input followed by an enter key.



The problem is the scanner is too fast and the ajax call does not get to finish before enter key is pressed.



This is my code so far:



$('#my_select').select2
initSelection: (elm, callback) ->
data =
id: $(elm).data "record-id"
name: $(elm).data "record-text"
callback(data)
ajax:
url: url
dataType: "jsonp"
quietMillis: 100
data: (term, page) ->
query: term
limit: 10
page: page
results: (data, page) ->
more = (page * 10) < data.total

results: data.records
more: more









share|improve this question
















I have an ajax select2 (v 3.5.4) perfectly working but now I need it to work with a barcode scanner.
I need to detect when enter key is pressed. The scanner inserts code into select2 input followed by an enter key.



The problem is the scanner is too fast and the ajax call does not get to finish before enter key is pressed.



This is my code so far:



$('#my_select').select2
initSelection: (elm, callback) ->
data =
id: $(elm).data "record-id"
name: $(elm).data "record-text"
callback(data)
ajax:
url: url
dataType: "jsonp"
quietMillis: 100
data: (term, page) ->
query: term
limit: 10
page: page
results: (data, page) ->
more = (page * 10) < data.total

results: data.records
more: more






jquery ajax select2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 12 '16 at 11:40







nigal

















asked Sep 12 '16 at 11:28









nigalnigal

8618




8618













  • Add your Relevent code what you have done so far..!!!

    – Haresh Vidja
    Sep 12 '16 at 11:31











  • @HareshVidja sorry, I've added my code to the question

    – nigal
    Sep 12 '16 at 11:42











  • Do you mean, enter is pressed in search textbox?

    – dns_nx
    Sep 12 '16 at 12:58











  • @dns_nx yes. Imagine search textbox has focus. Then if you scan the code 12345 the scanner enters '12345' and Enter key.

    – nigal
    Sep 12 '16 at 13:06



















  • Add your Relevent code what you have done so far..!!!

    – Haresh Vidja
    Sep 12 '16 at 11:31











  • @HareshVidja sorry, I've added my code to the question

    – nigal
    Sep 12 '16 at 11:42











  • Do you mean, enter is pressed in search textbox?

    – dns_nx
    Sep 12 '16 at 12:58











  • @dns_nx yes. Imagine search textbox has focus. Then if you scan the code 12345 the scanner enters '12345' and Enter key.

    – nigal
    Sep 12 '16 at 13:06

















Add your Relevent code what you have done so far..!!!

– Haresh Vidja
Sep 12 '16 at 11:31





Add your Relevent code what you have done so far..!!!

– Haresh Vidja
Sep 12 '16 at 11:31













@HareshVidja sorry, I've added my code to the question

– nigal
Sep 12 '16 at 11:42





@HareshVidja sorry, I've added my code to the question

– nigal
Sep 12 '16 at 11:42













Do you mean, enter is pressed in search textbox?

– dns_nx
Sep 12 '16 at 12:58





Do you mean, enter is pressed in search textbox?

– dns_nx
Sep 12 '16 at 12:58













@dns_nx yes. Imagine search textbox has focus. Then if you scan the code 12345 the scanner enters '12345' and Enter key.

– nigal
Sep 12 '16 at 13:06





@dns_nx yes. Imagine search textbox has focus. Then if you scan the code 12345 the scanner enters '12345' and Enter key.

– nigal
Sep 12 '16 at 13:06












2 Answers
2






active

oldest

votes


















1





+50









I have similar problem and manage to fix it with following patch. (It's against v4.0.2.)



--- a/select2.js    2018-03-09 08:33:38.000000000 +0700
+++ b/select2.js 2018-05-04 10:38:22.605348402 +0700
@@ -896,7 +896,7 @@
var $loading = this.option(loading);
$loading.className += ' loading-results';

- this.$results.prepend($loading);
+ this.$results.html($loading);
};

Results.prototype.hideLoading = function () {





share|improve this answer































    0














    I had a similar issue. I needed to set a value after page reload. I think, my workaround could help you, too. But maybe someone knows a better way?



    I initialized the select2 box with the ajax result (in your example code '12345'):



    var selectionList = [{ id: '12345', text: '12345' }];
    $("#my_select").select2({
    data: selectionList
    });


    Then I set the value and triggered the change event.



    $("#my_select").val("12345").trigger("change");


    After that, I initialized the select2 box again with ajax loading. This is the code you posted above:



    $('#my_select').select2
    initSelection: (elm, callback) ->
    data =
    id: $(elm).data "record-id"
    name: $(elm).data "record-text"
    callback(data)
    ajax:
    url: url
    dataType: "jsonp"
    quietMillis: 100
    data: (term, page) ->
    query: term
    limit: 10
    page: page
    results: (data, page) ->
    more = (page * 10) < data.total

    results: data.records
    more: more


    With this way, you have set your ajax value in the select2 box.
    I'm not 100% sure, if this is what you want to achieve. Or do you really need to fire the pressed enter key? Please let me know, if this solution helped you. Thanks.






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39449293%2fhow-to-configure-ajax-select2-for-working-with-a-barcode-scanner%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1





      +50









      I have similar problem and manage to fix it with following patch. (It's against v4.0.2.)



      --- a/select2.js    2018-03-09 08:33:38.000000000 +0700
      +++ b/select2.js 2018-05-04 10:38:22.605348402 +0700
      @@ -896,7 +896,7 @@
      var $loading = this.option(loading);
      $loading.className += ' loading-results';

      - this.$results.prepend($loading);
      + this.$results.html($loading);
      };

      Results.prototype.hideLoading = function () {





      share|improve this answer




























        1





        +50









        I have similar problem and manage to fix it with following patch. (It's against v4.0.2.)



        --- a/select2.js    2018-03-09 08:33:38.000000000 +0700
        +++ b/select2.js 2018-05-04 10:38:22.605348402 +0700
        @@ -896,7 +896,7 @@
        var $loading = this.option(loading);
        $loading.className += ' loading-results';

        - this.$results.prepend($loading);
        + this.$results.html($loading);
        };

        Results.prototype.hideLoading = function () {





        share|improve this answer


























          1





          +50







          1





          +50



          1




          +50





          I have similar problem and manage to fix it with following patch. (It's against v4.0.2.)



          --- a/select2.js    2018-03-09 08:33:38.000000000 +0700
          +++ b/select2.js 2018-05-04 10:38:22.605348402 +0700
          @@ -896,7 +896,7 @@
          var $loading = this.option(loading);
          $loading.className += ' loading-results';

          - this.$results.prepend($loading);
          + this.$results.html($loading);
          };

          Results.prototype.hideLoading = function () {





          share|improve this answer













          I have similar problem and manage to fix it with following patch. (It's against v4.0.2.)



          --- a/select2.js    2018-03-09 08:33:38.000000000 +0700
          +++ b/select2.js 2018-05-04 10:38:22.605348402 +0700
          @@ -896,7 +896,7 @@
          var $loading = this.option(loading);
          $loading.className += ' loading-results';

          - this.$results.prepend($loading);
          + this.$results.html($loading);
          };

          Results.prototype.hideLoading = function () {






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 21:22









          Jeremy MoritzJeremy Moritz

          6,09762227




          6,09762227

























              0














              I had a similar issue. I needed to set a value after page reload. I think, my workaround could help you, too. But maybe someone knows a better way?



              I initialized the select2 box with the ajax result (in your example code '12345'):



              var selectionList = [{ id: '12345', text: '12345' }];
              $("#my_select").select2({
              data: selectionList
              });


              Then I set the value and triggered the change event.



              $("#my_select").val("12345").trigger("change");


              After that, I initialized the select2 box again with ajax loading. This is the code you posted above:



              $('#my_select').select2
              initSelection: (elm, callback) ->
              data =
              id: $(elm).data "record-id"
              name: $(elm).data "record-text"
              callback(data)
              ajax:
              url: url
              dataType: "jsonp"
              quietMillis: 100
              data: (term, page) ->
              query: term
              limit: 10
              page: page
              results: (data, page) ->
              more = (page * 10) < data.total

              results: data.records
              more: more


              With this way, you have set your ajax value in the select2 box.
              I'm not 100% sure, if this is what you want to achieve. Or do you really need to fire the pressed enter key? Please let me know, if this solution helped you. Thanks.






              share|improve this answer






























                0














                I had a similar issue. I needed to set a value after page reload. I think, my workaround could help you, too. But maybe someone knows a better way?



                I initialized the select2 box with the ajax result (in your example code '12345'):



                var selectionList = [{ id: '12345', text: '12345' }];
                $("#my_select").select2({
                data: selectionList
                });


                Then I set the value and triggered the change event.



                $("#my_select").val("12345").trigger("change");


                After that, I initialized the select2 box again with ajax loading. This is the code you posted above:



                $('#my_select').select2
                initSelection: (elm, callback) ->
                data =
                id: $(elm).data "record-id"
                name: $(elm).data "record-text"
                callback(data)
                ajax:
                url: url
                dataType: "jsonp"
                quietMillis: 100
                data: (term, page) ->
                query: term
                limit: 10
                page: page
                results: (data, page) ->
                more = (page * 10) < data.total

                results: data.records
                more: more


                With this way, you have set your ajax value in the select2 box.
                I'm not 100% sure, if this is what you want to achieve. Or do you really need to fire the pressed enter key? Please let me know, if this solution helped you. Thanks.






                share|improve this answer




























                  0












                  0








                  0







                  I had a similar issue. I needed to set a value after page reload. I think, my workaround could help you, too. But maybe someone knows a better way?



                  I initialized the select2 box with the ajax result (in your example code '12345'):



                  var selectionList = [{ id: '12345', text: '12345' }];
                  $("#my_select").select2({
                  data: selectionList
                  });


                  Then I set the value and triggered the change event.



                  $("#my_select").val("12345").trigger("change");


                  After that, I initialized the select2 box again with ajax loading. This is the code you posted above:



                  $('#my_select').select2
                  initSelection: (elm, callback) ->
                  data =
                  id: $(elm).data "record-id"
                  name: $(elm).data "record-text"
                  callback(data)
                  ajax:
                  url: url
                  dataType: "jsonp"
                  quietMillis: 100
                  data: (term, page) ->
                  query: term
                  limit: 10
                  page: page
                  results: (data, page) ->
                  more = (page * 10) < data.total

                  results: data.records
                  more: more


                  With this way, you have set your ajax value in the select2 box.
                  I'm not 100% sure, if this is what you want to achieve. Or do you really need to fire the pressed enter key? Please let me know, if this solution helped you. Thanks.






                  share|improve this answer















                  I had a similar issue. I needed to set a value after page reload. I think, my workaround could help you, too. But maybe someone knows a better way?



                  I initialized the select2 box with the ajax result (in your example code '12345'):



                  var selectionList = [{ id: '12345', text: '12345' }];
                  $("#my_select").select2({
                  data: selectionList
                  });


                  Then I set the value and triggered the change event.



                  $("#my_select").val("12345").trigger("change");


                  After that, I initialized the select2 box again with ajax loading. This is the code you posted above:



                  $('#my_select').select2
                  initSelection: (elm, callback) ->
                  data =
                  id: $(elm).data "record-id"
                  name: $(elm).data "record-text"
                  callback(data)
                  ajax:
                  url: url
                  dataType: "jsonp"
                  quietMillis: 100
                  data: (term, page) ->
                  query: term
                  limit: 10
                  page: page
                  results: (data, page) ->
                  more = (page * 10) < data.total

                  results: data.records
                  more: more


                  With this way, you have set your ajax value in the select2 box.
                  I'm not 100% sure, if this is what you want to achieve. Or do you really need to fire the pressed enter key? Please let me know, if this solution helped you. Thanks.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 12 '16 at 13:40

























                  answered Sep 12 '16 at 13:34









                  dns_nxdns_nx

                  1,35011529




                  1,35011529






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39449293%2fhow-to-configure-ajax-select2-for-working-with-a-barcode-scanner%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen