bootstrap typeahead return name and id

前端 未结 6 1492
予麋鹿
予麋鹿 2021-02-03 13:33

Hy! I\'m using twitter bootstraps typeahead:

I\'m calling a page that returns a response with json_encode the page returns a name and an ID,

I want that the type

6条回答
  •  你的背包
    2021-02-03 14:18

    My solution is:

    var productNames = new Array();
    var productIds = new Object();
    $.getJSON( '/getAjaxProducts', null,
            function ( jsonData )
            {
                $.each( jsonData, function ( index, product )
                {
                    productNames.push( product.id + product.name );
                    productIds[product.id + product.name] = product.id;
                } );
                $( '#product' ).typeahead( { source:productNames } );
            } );
    

    In my case, product.id is a code, so it is useful to display it in typeahead control. In this way i avoided duplicated names risk.

提交回复
热议问题