jQuery UI autocomplete with item and id

前端 未结 11 1765
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 00:32

I have the following script which works with a 1 dimensional array. Is it possible to get this to work with a 2 dimensional array? Then whichever item is selected, by clic

相关标签:
11条回答
  • 2020-11-29 00:51

    This can be done without the use of hidden field. You have to take benefit of the JQuerys ability to make custom attributes on run time.

    ('#selector').autocomplete({
        source: url,
        select: function (event, ui) {
            $("#txtAllowSearch").val(ui.item.label); // display the selected text
            $("#txtAllowSearch").attr('item_id',ui.item.value); // save selected id to hidden input
        }
    });
    
    $('#button').click(function() {
        alert($("#txtAllowSearch").attr('item_id')); // get the id from the hidden input
    }); 
    
    0 讨论(0)
  • 2020-11-29 00:54

    From the Overview tab of jQuery autocomplete plugin:

    The local data can be a simple Array of Strings, or it contains Objects for each item in the array, with either a label or value property or both. The label property is displayed in the suggestion menu. The value will be inserted into the input element after the user selected something from the menu. If just one property is specified, it will be used for both, eg. if you provide only value-properties, the value will also be used as the label.

    So your "two-dimensional" array could look like:

    var $local_source = [{
        value: 1,
        label: "c++"
    }, {
        value: 2,
        label: "java"
    }, {
        value: 3,
        label: "php"
    }, {
        value: 4,
        label: "coldfusion"
    }, {
        value: 5,
        label: "javascript"
    }, {
        value: 6,
        label: "asp"
    }, {
        value: 7,
        label: "ruby"
    }];
    

    You can access the label and value properties inside focus and select event through the ui argument using ui.item.label and ui.item.value.

    Edit

    Seems like you have to "cancel" the focus and select events so that it does not place the id numbers inside the text boxes. While doing so you can copy the value in a hidden variable instead. Here is an example.

    0 讨论(0)
  • 2020-11-29 00:54

    My code only worked when I added 'return false' to the select function. Without this, the input was set with the right value inside the select function and then it was set to the id value after the select function was over. The return false solved this problem.

    $('#sistema_select').autocomplete({
    
        minLength: 3,
        source: <?php echo $lista_sistemas;?> ,
        select: function (event, ui) {
             $('#sistema_select').val(ui.item.label); // display the selected text
             $('#sistema_select_id').val(ui.item.value); // save selected id to hidden input
             return false;
         },
        change: function( event, ui ) {
            $( "#sistema_select_id" ).val( ui.item? ui.item.value : 0 );
        } 
    });
    

    In addition, I added a function to the change event because, if the user writes something in the input or erases a part of the item label after one item was selected, I need to update the hidden field so that I don´t get the wrong (outdated) id. For example, if my source is:

    var $local_source = [
           {value: 1,  label: "c++"}, 
           {value: 2,  label: "java"}]
    

    and the user type ja and select the 'java' option with the autocomplete, I store the value 2 in the hidden field. If the user erase a letter from 'java', por exemple ending up with 'jva' in the input field, I can´t pass to my code the id 2, because the user changed the value. In this case I set the id to 0.

    0 讨论(0)
  • 2020-11-29 00:56

    At last i did it Thanks alot friends, and a special thanks to Mr https://stackoverflow.com/users/87015/salman-a because of his code i was able to solve it properly. finally my code is looking like this as i am using groovy grails i hope this will help somebody there.. Thanks alot

    html code looks like this in my gsp page

      <input id="populate-dropdown" name="nameofClient" type="text">
      <input id="wilhaveid" name="idofclient" type="text">
    

    script Function is like this in my gsp page

      <script>
            $( "#populate-dropdown").on('input', function() {
                $.ajax({
                    url:'autoCOmp',
                    data: {inputField: $("#populate-dropdown").val()},
                    success: function(resp){
                        $('#populate-dropdown').autocomplete({
                            source:resp,
                            select: function (event, ui) {
                                $("#populate-dropdown").val(ui.item.label);
                                $("#wilhaveid").val(ui.item.value);
                                 return false;
                            }
                        })
                    }
                });
            });
        </script>
    

    And my controller code is like this

       def autoCOmp(){
        println(params)
        def c = Client.createCriteria()
        def results = c.list {
            like("nameOfClient", params.inputField+"%")
        }
    
        def itemList = []
        results.each{
            itemList  << [value:it.id,label:it.nameOfClient]
        }
        println(itemList)
        render itemList as JSON
    }
    

    One more thing i have not set id field hidden because at first i was checking that i am getting the exact id , you can keep it hidden just put type=hidden instead of text for second input item in html

    Thanks !

    0 讨论(0)
  • 2020-11-29 01:00
    <script type="text/javascript">
    $(function () {
        $("#MyTextBox").autocomplete({
            source: "MyDataFactory.ashx",
            minLength: 2,
            select: function (event, ui) {
                $('#MyIdTextBox').val(ui.item.id);
                return ui.item.label;
            }
        });
    });
    

    The above responses helped but, did not work in my implementation. The instead of using setting the value using jQuery, I am returning the value from the function to the select option.

    The MyDataFactory.ashx page has a class with three properties Id, Label, Value.

    Pass the List into the JavaScript serializer, and return the response.

    0 讨论(0)
提交回复
热议问题