Cascade Dropdownlist fill and pick up two fields

Asked

Viewed 54 times

0

Friends good afternoon!!!

first I have a file (Repository) ASP that stores my selects for some dropdowns, in this have the function below listing my UF’s:

public function ListarUF()
        dim sql
        dim cmd
        dim rs
        dim nota
        dim lista
        dim i

        sql = "SELECT * FROM UF ORDER BY UF"

        set cmd = ObterCommand(sql)

        set rs = cmd.Execute

        i = 0
        do while not rs.eof
            if i > 0 then
                lista = lista &  "|" & rs("UF")
            else
                lista = rs("UF")
            end if
            i = i + 1
            rs.moveNext
        loop
        rs.close

        set rs = nothing
        set cmd = nothing
        db.close

        ListarUF = lista
    end function

2nd - I have another file in ASP that mention the top to fetch the function in question ListarUF() as follows:

in the page header enter the command below:

<!--#include file="Repositorios/RepositorioAsimov.asp"-->

then create the method below:

caso "listarUF"  
  set repositorio = new clsRepositorioAsimov
  lista = repositorio.ListarUF()

  if err.number = 0 then
       Response.Write(lista)
  else
       Response.Write(err.Description)
  end if

  set lista = nothing
  set repositorio = nothing

- On the page I have to load the Dropdown enter the code ajax down below:

function popularDropDownListUf() {
            $("#uf").empty();
            $("#uf").append("<option value='' selected></option>");

            $.ajax({
                type: "POST",
                url: "ServiceAsimov.asp?metodo=listarUF",
                cache: false,
                dataType: "html",
                async: false,
                success: function (response) {
                    if (response != null) {
                        var lista = response.split("|");

                        $.each(lista, function (key, value) {
                            $("#uf").append("<option value='" + value + "'>" + value + "</option>");
                        })
                    }
                },
                error: function (xhr, options, error) {
                    alert(error);
                    $(".loading").fadeOut();
                }
            });
}

My problem is I’m not getting the Id of UF and the Acronym of UF together, it’s only coming to me the acronym of UF.

What am I doing wrong?

  • Although you’re bringing all the data from the table, you’re iterating only with the UF and not with the UF and the ID. What you can do is use the recordset to read Id and concatenate the Id next to the UF, separating by another special character and giving split in javascript.

1 answer

0

You can do the following, concatenate the id in your Asp:

  public function ListarUF()
    dim sql
    dim cmd
    dim rs
    dim nota
    dim lista
    dim i

    sql = "SELECT * FROM UF ORDER BY UF"

    set cmd = ObterCommand(sql)

    set rs = cmd.Execute

    i = 0
    do while not rs.eof
        if i > 0 then
            lista = lista &  "|" & rs("UF") &"-"&rs("ID")
        else
            lista = rs("UF")
        end if
        i = i + 1
        rs.moveNext
    loop
    rs.close

    set rs = nothing
    set cmd = nothing
    db.close

    ListarUF = lista
end function 

And then in Java, use the split:

function popularDropDownListUf() {
        $("#uf").empty();
        $("#uf").append("<option value='' selected></option>");

        $.ajax({
            type: "POST",
            url: "ServiceAsimov.asp?metodo=listarUF",
            cache: false,
            dataType: "html",
            async: false,
            success: function (response) {
                if (response != null) {
                    var lista = response.split("|");

                    $.each(lista, function (key, value) {
                     var ufDados = value.split("-");

                        $("#uf").append("<option value='" + ufDados[1]+ "'>" + ufDados[0]+ "</option>");
                    })
                }
            },
            error: function (xhr, options, error) {
                alert(error);
                $(".loading").fadeOut();
            }
        });}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.