Search code examples
jqueryajaxrestsharepointsharepoint-2010

SharePoint / REST / Populating Dropdown List


so I've been banging my head against this for awhile. I'm having formatting issues but most importantly I cannot seem to get the auto-generated dropdown list to run a function.

Edited Code: (Fixed some things but still getting unexpected token error.

$(document).ready(function() {

$.ajax({


    type: "get",

contentType: "application/json; charset=utf-8",


    url: "SiteURL/_vti_bin/ListData.svc/Region",

    headers: {"Accept": "application/json;odata=verbose"},

    success: function (data) {

            $.each(data.d.results, function (i, result) {

                $("#RegionUL").append("<li><a onclick='fnRegionalSearch('" + result.Title + "')' href='#'>" + result.Title + "</a></li>");

            });   

    },

error: function (data) {

        alert(data.responseJSON.error);

    }

});

});

function fnRegionSearch(choice) {

fnWaitDialog("show");

var searchId = choice;

$("#tableBody tr").remove();

$.getJSON("SiteURL/_vti_bin/ListData.svc/LIST?$filter=SecondaryRegion eq '" + searchId + "'&$orderby=Name", function (data) {

    var d = data.d;
    if (d.results.length == 0) {
        $("#noResultsAlert").show();
        $("#notingQueried").hide();
    }
    else {
        $.each(d.results, function (n, i) {
            var path = "ListURL" + "/" + i.Name;

                $("#tableBody").append("<tr><td>Reference Id: " + i.ReferenceId + " | <a href='" + path + "'>" + "<i class='fa fa-pencil'></i></a>" + "<br/>Employee Name: " + i.LastName + ", " + i.FirstName + "<br/>Current Assignment: " + i.SecondaryRegion + " | " + i.SecondaryCountry + " | " + i.SecondaryPost + "</td></tr>");

        });

        $("#noResultsAlert").hide();
        $("#notingQueried").hide();
    }
})
.always(function () {
    fnWaitDialog("hide");
});
}


function fnWaitDialog(showOrHide) {
    $("#pleaseWaitDialog").modal(showOrHide);
    }

I'm confused because I can get my list to auto-populate which is awesome but I cannot seem to get the URL to run the function that I need. Also the formatting of the list choices is all screwed up with an unnecessary "unspecified" option.

Any help with this would be awesome - its giving me a huge headache.


Solution

  • You have an error when you append your <li> You can do this different ways, but I recommend escaping like this. Should fix it

    $("#RegionUL").append("<li><a onclick=\"fnRegionalSearch('" + result.Title + "')\" href='#'>" + result.Title + "</a></li>");