jQuery的多列自动完成?

时间:2010-04-15 21:33:34

标签: jquery ajax jquery-plugins autocomplete

是否有支持多列的jQuery的AJAX自动完成功能?我的意思是多个列,如数据库表,而不仅仅是拆分列表。它将搜索第一列,但其余部分将在下拉列表中显示。

这样的事情存在吗?

3 个答案:

答案 0 :(得分:2)

try this code searching for one column but displaying multiple columns 
$(function() {
    $(".tb").autocomplete
            ({
                source: function(request, response) {
                    $.ajax({
                        type: "POST",
                        url: "WebService_GetData.asmx/GetCmbPostaKod",
                        dataType: "json",
                        data: "{ 'filterKey': '" + request.term + "' }",
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function(data) { return data; },
                        success: function(data) {
                            response($.map(data.d, function(item) {
                            return { value: item.PostaKodu, label: item.IlAdi + ' ' + item.IlceAdi + ' ' + item.SemtAdi + ' ' + item.PostaKodu }
                            }))
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            alert(textStatus);
                        }
                    });
                },
                minLength: 2,
                multiple: true,
                matchContains: true,
                formatItem: formatItem,
                formatResult: formatResult
            });
        });

        function formatItem(row) {
            return row[0] + " (<strong>id: " + row[1] + "</strong>)";
        }
        function formatResult(row) {
            return row[0].replace(/(<.+?>)/gi, ''); //removes html tags
        }

答案 1 :(得分:0)

是。常见的auto-complete plugin会这样做 - 查看“多鸟(远程)”框,它会搜索第一列但显示更多数据:

function formatItem(row) {
    return row[0] + " (<strong>id: " + row[1] + "</strong>)";
}
function formatResult(row) {
    return row[0].replace(/(<.+?>)/gi, ''); //removes html tags
}
$("#suggest4").autocomplete('search.php', {
    width: 300,
    multiple: true,
    matchContains: true,
    formatItem: formatItem,
    formatResult: formatResult
});

search.php?q = b 的结果是:

Great <em>Bittern</em>|Botaurus stellaris
Little Bittern|Ixobrychus minutus
American Bittern|Botaurus lentiginosus

答案 2 :(得分:0)

您可以将以下网址用于一列但显示多列

http://jsfiddle.net/alforno/g4stL/

 $("#search").mcautocomplete({
 showHeader: true,

columns: [{
    name: 'City',
    width: '150px',
    valueField: 'name'
}, {
    name: 'State',
    width: '120px',
    valueField: 'adminName1'
}, {
    name: 'Country',
    width: '120px',
    valueField: 'countryName'  }],
    select: function (event, ui) {
    this.value = (ui.item ? ui.item.name : '');
    return false;
    },



minLength: 1,
source: function (request, response) {
    $.ajax({
        url: 'http://ws.geonames.org/searchJSON',
        dataType: 'jsonp',
        data: {
            featureClass: 'P',
            style: 'full',
            maxRows: 12,
            name_startsWith: request.term,
            username: "demo"
        },
        // The success event handler will display "No match found" if no items are returned.
        success: function (data) {
            var result;
            if (!data || data.length === 0 || !data.geonames || data.geonames.length === 0) {
                result = [{
                    label: 'No match found.'
                }];
            } else {
                result = data.geonames;
            }
            response(result);
        }
    });
} });