需要忽略自动填充框中的重复条目

时间:2011-05-17 18:29:35

标签: javascript jquery ajax vb.net

我有一个使用jquery自动完成插件的输入。有了这个我想要返回5个结果。我只是在我使用的存储过程中输入5个返回结果,但我想删除重复项,同时仍然显示5个结果。我不确定这样做的最好方法 这是我的回调页面背后的代码

Public Class SearchCallback
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Response.Clear()
    Dim searchText As String = Request("searchText")
    Dim searchField As String = Request("searchField")


    Dim dtCustomer As CustomerCollection = CustomerService.SearchRecords(searchText, searchField, "Contains", searchField, "Asc", CInt("1"), CInt("5"))
    Dim returnString As String
    If searchField = "Company" Then
        For Each drCustomer As Customer In dtCustomer
            If returnString = "" Then
                returnString = drCustomer.Company
            else
                returnString = returnString & "," & drCustomer.Company
            End If
        Next
        Response.Write(returnString)

之后我有一个更多的搜索字段选项列表,以便为我提供我想要的数据。我把所需的信息放入一个字符串,所以我可以使用我的javascript代码,如下所示。

$(document).ready(function () {
$(".searchTextBox").autocomplete({
    source: callback,
    autoFill: true
});
});

var callback = function (request, response) {
var searchText = request.term;
var searchField = $(".ddlist > option:selected").attr("value");
$.ajax({
    type: "GET",
    dataType: "text",
    url: "SearchCallback.aspx?searchText=" + searchText + "&searchField=" + searchField,
    success: function (data) {
        var splitData = data.split(",");
        response(splitData);
    }
});
}
一切都有效,然后我有的复制问题......请帮忙!


它有点混乱,但我按照我想要的方式使用以下代码

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Response.Clear()
    Dim searchText As String = Request("searchText")
    Dim searchField As String = Request("searchField")
    Dim lastValue As String
    Dim resultsCounter As Integer = 0

    Dim dtCustomer As CustomerCollection = CustomerService.SearchRecords(searchText, searchField, "Contains", searchField, "Asc", CInt("1"), CInt("100"))
    Dim returnString As String
    If searchField = "Company" Then
        resultsCounter = 0
        For Each drCustomer As Customer In dtCustomer
            If returnString = "" Then
                returnString = drCustomer.Company
                lastValue = drCustomer.Company
                resultsCounter += 1
            ElseIf resultsCounter < 6 Then
                If lastValue <> drCustomer.Company Then
                    returnString = returnString & "," & drCustomer.Company
                    lastValue = drCustomer.Company
                    resultsCounter += 1
                End If
            End If
        Next

        Response.Write(returnString)

1 个答案:

答案 0 :(得分:1)

您使用的是哪种ORM框架?使用SQL,DISTINCT关键字用于此,这可能会有所帮助