Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programmatically force to search #5

Open
BlaM opened this issue Jan 2, 2012 · 1 comment
Open

Programmatically force to search #5

BlaM opened this issue Jan 2, 2012 · 1 comment

Comments

@BlaM
Copy link

BlaM commented Jan 2, 2012

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
    1. pineapple
    1. 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
Copy link
Author

BlaM commented Jan 3, 2012

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant