문제

필드에 대한 제안 사항을 사용하여 검색을 수행하도록 Typeahead + Bloodhound를 설정하려고 합니다.해당 필드의 HTML 코드는 다음과 같습니다.

<div class="col-sm-10" id="products_forms">
    <input type="text" placeholder="Producto" id="ProductoForm_0_product_id" name="ProductoForm[0][product_id]" class="form-control typeahead">
</div>

저는 Symfony의 두 가지 주요 기능을 사용합니다.하나는 모든 제품을 반품하고 이것을 prefetch 다른 하나는 필터링된 제품용입니다.해당 기능의 경로는 다음과 같습니다.

 // the one I use as prefetch parameter in bloodhound
 * @Route("/get_products", name="all_products")

 // the one I use as remote
 * @Route("/get_products/{filter}", name="filter_products")

보시다시피 첫 번째는 모든 제품을 JSON 값으로 반환하기 때문에 매개 변수를 얻지 못했지만 두 번째는 매개 변수를 가져옵니다. {filter} 수행에 대한 주장으로 LIKE 필터링된 제품만 반환합니다.

이제 저는 Bloodhound가 어떻게 작동하는지 전혀 이해하지 못해서 다음 내용을 읽었습니다. 문서 그리고 미리 입력하려면 다음을 읽어보세요. 문서 그리고 예를 들어 여기 그만큼 Remote 하나를 선택하고 다음 코드를 만들었습니다.

// Trigger typeahead + bloodhound
var products = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    prefetch: Routing.generate('all_products'),
    remote: Routing.generate('filter_products', { 'filter' : '%query' })
});

products.initialize();
$('#products_forms .typeahead').typeahead(null, {
    name: 'products',
    displayKey: 'value',
    source: products.ttAdapter()
});

그렇죠?즉, 페이지가 로드되면 모든 제품을 미리 가져오지만 아무 제품이나 입력하면 .typeahead 요소 필터링된 요소만 가져오나요?나는 모른다 %query 내가 전달해야 할 올바른 값은 무엇입니까? filter_products 필터링된 값을 얻기 위해 경로를 지정합니다.도움이 필요하세요?

Typeahead + Bloodhound를 처음 사용합니다.

도움이 되었습니까?

해결책

바꾸다 remote: Routing.generate('filter_products', { 'filter' : '%query' }) 예를 들어

var url = Routing.generate('filter_products', {filter: 'WILDCARD'});
// ... some code
remote: {
            url: url,
            wildcard: 'WILDCARD'
        }

먼저 RoutingBundle을 사용하여 일반 URL을 생성하고 'WILDCARD'를 필터로 사용합니다.블러드하운드에게 URL의 'WILDCARD'를 자리 표시자로 사용하라고 지시하면 입력된 쿼리로 완벽하게 대체됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top