• I’m trying to use the great jquery plugin select2
    But I want the select box to be dynamically populated … via JSON from my wordpress site. I’m trying to follow this tutorial https://www.codeproject.com/Articles/623566/Select-The-Ultimate-jQuery-Autocomplete but am in over my head in regards to the “Setting Up the JSON Service” part of the tutorial.

    This code section:

    public class Select2PagedResult
    {
        public int Total { get; set; }
        public List<Select2Result> Results { get; set; }
    }
    public class Select2Result
    {
        public string id { get; set; }
        public string text { get; set; }
    }

    I don’t fully get classes yet and how they work. Where would that code go? In functions.php? On the template page where the select box is, somewhere else?

    And then where does this code go? And how do I modify this to return the post ID and Title of a custom post?

    [HttpGet]
    public ActionResult GetAttendees(string searchTerm, int pageSize, int pageNum)
    {
        //Get the paged results and the total count of the results for this query.
        AttendeeRepository ar = new AttendeeRepository();
        List<Attendee> attendees = ar.GetAttendees(searchTerm, pageSize, pageNum);
        int attendeeCount = ar.GetAttendeesCount(searchTerm, pageSize, pageNum);
    
        //Translate the attendees into a format the select2 dropdown expects
        Select2PagedResult pagedAttendees = AttendeesToSelect2Format(attendees, attendeeCount);       
    
        //Return the data as a jsonp result
        return new JsonpResult
        {
            Data = pagedAttendees,
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }

    Note that this code also helps filter the ajax results in the dropdown dynamcially.

Viewing 1 replies (of 1 total)
  • Thread Starter bobblah

    (@bobblah)

    Actually, duh, that tutorial is using .net

    I need a solution for a an ajax populated multi select box. select2, chosen etc. I don’t care. Is it possible anyone anywhere has a working example that can function on wordpress?

Viewing 1 replies (of 1 total)
  • The topic ‘Setting Up JSON Service on WordPress (jquery, select2, AJAX)’ is closed to new replies.