Mostrando entradas con la etiqueta Autocomplete. Mostrar todas las entradas
Mostrando entradas con la etiqueta Autocomplete. Mostrar todas las entradas

viernes, 17 de mayo de 2013

Pasar más de un parámetro al jQuery autocomplete

Normalmente pasamos un sólo valor al autocomplete: el valor escrito en el textbox (request.term). ¿Y si queremos pasar algún otro valor, por ejemplo, para usarlo como parámetro en un sp?
 
HTML
====
<table width="800px" border="0">
    <tr>
        <td style="width:90px;">
            N° Nota Salida
        </td>
        <td>
            <asp:TextBox ID="txtNumeroDoc" runat="server" Width="80px" MaxLength="10" CssClass="IntegerType">

        </td>
        <td>
             <asp:HiddenField runat="server" ID="hdnCodUsuario" Value="1" />
        </td>
    </tr>
</table>


Javascript
==========
$(document).ready(function () {

    $('#txtNumeroDoc').autocomplete({
        source: function (request, response) {
            $.ajax({
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                url: '../../ws/autocomplete.asmx/NotaIngresoSalida',
                data: "{'tag': '" + request.term + "', 'CodUsuario': '" + $('#hdnCodUsuario').val() + "'}",
          
                dataType: 'json',
                async: true,
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            value: item.value,
                            label: item.label,
                            seriedoc: item.seriedoc,
                            numerodoc: item.numerodoc
                        };
                    }));
                },
                error: function (result) {
                    alert('No se pudo cargar la lista de notas de salida.');
                }
            });
        },
        minLength: 2,
        select: function (event, ui) {
            NotaIngresoSalida_Buscar(ui.item.seriedoc, ui.item.numerodoc);
        }
    });

});

function NotaIngresoSalida_Buscar(strSerieDoc, strNumeroDoc) {
    alert(strSerieDoc + '-' + strNumeroDoc);

    return false;
}



WebServices
===========
   <WebMethod()> _
    Public Function NotaIngresoSalida(ByVal tag As String, ByVal CodUsuario As String) As Object
        Dim output As String = ""

        Dim lhtbResultado = New With {.value = "", .label = "", .seriedoc = "", .numerodoc = ""}
        Dim Lista = {lhtbResultado}.ToList()

        Try
            Dim objbnNotaIngresoSalida As New bnNotaIngresoSalida
            Dim dt As New DataTable

            dt = objbnNotaIngresoSalida.pf_NotaIngresoSalida_Salidas_Autocomplete(tag, CodUsuario)

            If dt.Rows.Count > 0 Then
                Lista.RemoveAt(0)

                For i = 0 To dt.Rows.Count - 1
                    Dim lhtbItem = New With {.value = "", .label = "", .seriedoc = "", .numerodoc = ""}

                    With lhtbItem
                        .value = dt(i).Item(0)
                        .label = dt(i).Item(0)
                        .seriedoc = dt(i).Item(1)
                        .numerodoc = dt(i).Item(2)
                    End With

                    Lista.Add(lhtbItem)
                Next
            End If

            output = Lista.ToString()

        Catch ex As Exception

        End Try

        Return Lista
    End Function