1

I want to populate a ListBox using jQuery/Json. Below is the code that I am attempting to use.

jQuery inside document.ready:

$('#<%=txtSearch.ClientID %>').keyup(function() {
        if ($('#<%=txtSearch.ClientID %>').val().length > 1) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                //data: "{ prefixText: '" + $('#<%=txtSearch.ClientID %>').val() + "', count: '5'}",
                data: "{ sText: '" + $('#<%=txtSearch.ClientID %>').val() + "', sFilter: ' " + $('#<%=lsResults.ClientID %>').val() + "'}",
                url: "../cspm/s3.asmx/GetResults",
                dataType: "json",
                success: function(data) {
                    var results = data.d;
                    if (results.length > 0) {
                        var listItems = [];
                        for (var key in results) {
                            listItems.push('<option value="' +
                            results[0].toString() + '">' + results[1].toString()
                            + '</option>');
                        }
                        $('#<%=lsResults.ClientID %>').append(listItems.join(''));
                    }
                }
            });
        }

    });

Webservice method code:

<WebMethod()> _
Public Function GetResults(ByVal sText As String, ByVal sFilter As String) As String(,)

    Dim searchText As String() = sText.Trim().Split(New Char() {" "c})
    Dim array(2, 2) As String

    sql = New StringBuilder()

    'If ddlProductLine.SelectedValue <> "INSTRUMENT" Then
    sql.Append("select msi.inventory_item_id,msi.description, msi.segment1 || '.' || msi.segment2 || '  -  ' || msi.description text ")
    sql.Append("from apps.mtl_system_items_b MSI, APPS.MTL_ITEM_CATEGORIES MIC, APPS.MTL_CATEGORIES_B MC ")
    sql.Append("where MSI.INVENTORY_ITEM_ID = MIC.INVENTORY_ITEM_ID ")
    sql.Append("AND MSI.ORGANIZATION_ID = MIC.ORGANIZATION_ID AND MIC.CATEGORY_ID = MC.CATEGORY_ID AND MIC.CATEGORY_SET_ID = 1 AND MSI.organization_id = 83 ")
    sql.Append("AND msi.inventory_item_status_code = 'Active' ")
    sql.Append("and msi.description like '%" + searchText(0).ToUpper() + "%' ")

    For i As Integer = 1 To searchText.Length - 1
        sql.Append("and msi.description like '%" + searchText(i).ToUpper() + "%' ")
    Next

    sql.Append("and mc.SEGMENT1 like '%" + sFilter + "%' ")
    sql.Append("order by msi.description ")
    'End If

    Dim dt As DataTable = db.ExecuteDataTable(sql.ToString())

    For i As Integer = 0 To dt.Rows.Count
        array(i, 0) = dt.Rows(i)("inventory_item_id").ToString()
        array(i, 1) = dt.Rows(i)("text").ToString()
    Next

    Return array

End Function

This looks like it should work but I am new to using json with jQuery.

1 Answer 1

1

I guess problem is in this block

for (var key in results) {
   listItems.push('<option value="' +
   results[0].toString() + '">' + results[1].toString()
   + '</option>');
}

change it to

for (var i=0; i<results.length; i++) {
   listItems.push('<option value="' +
   results[i][0] + '">' + results[i][1]
   + '</option>');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.