jQuery Autocomplete using extraParams to pass additional GET variables

后端 未结 13 721
独厮守ぢ
独厮守ぢ 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: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();
      }
    }
    

提交回复
热议问题