jQuery Autocomplete using extraParams to pass additional GET variables

后端 未结 13 723
独厮守ぢ
独厮守ぢ 2021-01-31 17:23

I am referring specifically to the jQuery Autocomplete v1.1 plugin by Jörn Zaefferer [source: http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/] as there seems to

相关标签:
13条回答
  • 2021-01-31 17:45

    With regards to the most voted answer, I think there is a much simpler syntax by just appending the extra request value into the source url.

    This:

    $("#city_id").autocomplete({
        source: src+"?country_id="+$("#country_id").val().
        min_length: 3,
        delay: 300
    });
    

    does the same as:

    $("#city_id").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: src,
                dataType: "json",
                data: {
                    term : request.term,
                    country_id : $("#country_id").val()
                },
                success: function(data) {
                    response(data);
                }
            });
        },
        min_length: 3,
        delay: 300
    });
    

    given that src is an url string.

    0 讨论(0)
  • 2021-01-31 17:46

    use a .each first, then you can use $(this) and set whatever you need into a variable. the resulting variable can be used in the autocomplete

    $(".autosuggest").each(function (index, object) {
        var autosuggestType = $(this).attr("autoSuggestType");
        $(this).autocomplete("url",
                {                    
                    extraParams: {
                        autoSuggestType: autosuggestType
                    },
    
    0 讨论(0)
  • 2021-01-31 17:51

    Try this:

    $('.ajax-auto input').setOptions({
      extraParams: {
        search_type: function(){
          return $(this).attr('name');
        }
      }
    })
    

    See also here

    0 讨论(0)
  • 2021-01-31 17:52

    I am using the autocomplete function that is now part of jquery ui. Passing an 'extraParams' field does not work but you can just append the values in the request query string.

    $(document).ready(function() {
        src = 'http://domain.com/index.php';
    
        // Load the cities straight from the server, passing the country as an extra param
        $("#city_id").autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: src,
                    dataType: "json",
                    data: {
                        term : request.term,
                        country_id : $("#country_id").val()
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            min_length: 3,
            delay: 300
        });
    });
    
    0 讨论(0)
  • 2021-01-31 17:55

    Try with

    $( "#ricerca" ).autocomplete({
                    source: "response.php?param=param",
                    minLength: 2
    });
    
    0 讨论(0)
  • 2021-01-31 17:56

    I understand that its been answered already. but I hope this will help someone in future and saves so much time and pain.

    (you can replace 'CRM.$' with '$' or 'jQuery' depending on your jQuery version)

    complete code is below: This one I did for a textbox to make it Autocomplete in CiviCRM. Hope it helps someone

    CRM.$( 'input[id^=custom_78]' ).autocomplete({
                autoFill: true,
                select: function (event, ui) {
                        var label = ui.item.label;
                        var value = ui.item.value;
                        // Update subject field to add book year and book product
                        var book_year_value = CRM.$('select[id^=custom_77]  option:selected').text().replace('Book Year ','');
                        //book_year_value.replace('Book Year ','');
                        var subject_value = book_year_value + '/' + ui.item.label;
                        CRM.$('#subject').val(subject_value);
                        CRM.$( 'input[name=product_select_id]' ).val(ui.item.value);
                        CRM.$('input[id^=custom_78]').val(ui.item.label);
                        return false;
                },
                source: function(request, response) {
                    CRM.$.ajax({
                        url: productUrl,
                            data: {
                                            'subCategory' : cj('select[id^=custom_77]').val(),
                                            's': request.term,
                                        },
                        beforeSend: function( xhr ) {
                            xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
                        },
    
                        success: function(result){
                                    result = jQuery.parseJSON( result);
                                    //console.log(result);
                                    response(CRM.$.map(result, function (val,key) {
                                                             //console.log(key);
                                                             //console.log(val);
                                                             return {
                                                                     label: val,
                                                                     value: key
                                                             };
                                                     }));
                        }
                    })
                    .done(function( data ) {
                        if ( console && console.log ) {
                         // console.log( "Sample of dataas:", data.slice( 0, 100 ) );
                        }
                    });
                }
      });
    

    PHP code on how I'm returning data to this jquery ajax call in autocomplete:

    /**
     * This class contains all product related functions that are called using AJAX (jQuery)
     */
    class CRM_Civicrmactivitiesproductlink_Page_AJAX {
      static function getProductList() {
            $name   = CRM_Utils_Array::value( 's', $_GET );
        $name   = CRM_Utils_Type::escape( $name, 'String' );
        $limit  = '10';
    
            $strSearch = "description LIKE '%$name%'";
    
            $subCategory   = CRM_Utils_Array::value( 'subCategory', $_GET );
        $subCategory   = CRM_Utils_Type::escape( $subCategory, 'String' );
    
            if (!empty($subCategory))
            {
                    $strSearch .= " AND sub_category = ".$subCategory;
            }
    
            $query = "SELECT id , description as data FROM abc_books WHERE $strSearch";
            $resultArray = array();
            $dao = CRM_Core_DAO::executeQuery( $query );
            while ( $dao->fetch( ) ) {
                $resultArray[$dao->id] = $dao->data;//creating the array to send id as key and data as value
            }
            echo json_encode($resultArray);
        CRM_Utils_System::civiExit();
      }
    }
    
    0 讨论(0)
提交回复
热议问题