Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Programmatically force to search #5

Open
BlaM opened this Issue · 1 comment

1 participant

@BlaM

Hey, what about a challenge? :)

What I need to do is to have an option to trigger a search from my program code - followed by selecting an item from the list.

Example:

The list contains the items

  • 1. peach
  • 2. pineapple
  • 3. passiflora

The user clicks on a button somewhere in the page. That button should trigger an event and force the list to be set to "apple"

ajaxChosen would need to do an ajax request to fetch the id and the item "apple", modify the list and then select the specified item.

I will try to solve that myself now, but I'll do it in JavaScript. No time to learn CoffeeScript now.

@BlaM

For everybody interested: here is my jQuery code to force the list to include a given item. It's based on my special case where #chzn is the chosen box:

var setBox = function(id) {
    var $opt = $('#chzn option[value="' + id+ '"]');
    if ($opt.length == 0) {
        // Need to load first
        $.ajax({
            'url': 'ajax.asp',
            'data': {"id": id},
            success: function(data, t, x) {
                if (data.length == 1) {
                    var $opt = $('<option value="' + data[0]['id'] + '">').html(data[0]['text']);
                    $('#chzn').append($opt).val(data[0]['id']).trigger("liszt:updated");
                } else {
                    // Deal with "not found" state.
                }
            }
        });
    } else {
        // Already exists
        $('#chzn').val(id).trigger("liszt:updated");
    }
}

This code exists outside the ajax-chosen environment but might be worth to add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.