jQuery Autocomplete & jTemplates - handling response

风格不统一 提交于 2019-12-25 14:25:47

问题


Has anyone had any experience with using jTemplates to display autocomplete results.

I have the following

$("#address-search").autocomplete({
    source: "/Address/SearchAddress",
    minLength: 2,
    delay: 400,
    focus: function (event, ui) {
      $('#address-search').val(ui.item.name);
       return false;
    },
    parse: function(data) {
      $("#autocomplete-results").setTemplate($("#templateHolder").html());
      $("#autocomplete-results").processTemplate(data);
    },
    select: function (event, ui) {
    $('#address-search').val(ui.item.name);
    $('#search-address-id').val(ui.item.id);
    $('#search-description').html(ui.item.address);

    });

and the simple jtemplate holder:

<script type="text/html" id="templateHolder">
    <ul class="autocomplete">
        {#foreach $T as data}
        <li>{$T.name}</li>
        {#/for}
    </ul>
</script>

Above i'm using 'Parse' to format results, I've also tried the autocomplete result method but not having any luck so far. The only success I've had is by using the private method ._renderItem and formatting the data that way but we want to render the output using the jTemplate.

Any advice appreciated.


回答1:


What kind of issues are you running into? Just looking at your code real quick, it seems like you may not be getting the values you want into the template, or it may be erroring out? Within your foreach, you're calling the individual objects in your array data, but you're appending the value of {$T.name}. Maybe you want {$T.data.name} instead?



来源:https://stackoverflow.com/questions/2946949/jquery-autocomplete-jtemplates-handling-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!