Set Initial Selection

To set initial selected value during page load, use set_init_selection() method. The parameter must be an object array with both “id” and “text” properties. You can use it to set initial selected value for both <select> and <input type=”hidden”>, single or multiple selection.

For example:

$pac->set_init_selection(array(
                            array("id"=>"1", "text"=>"Apple"),
                            array("id"=>"5", "text"=>"Twitter")));

Example: Set initial multiple selection for <input type=”hidden”>

<?php
$data = array(array('id'=>1, 'text'=>'Apple'),
    array('id'=>2, 'text'=>'Google'),
    array('id'=>3, 'text'=>'Microsoft'),
    array('id'=>4, 'text'=>'Facebook'),
    array('id'=>5, 'text'=>'Twitter'));

$pac = new C_PhpAutocomplete('init_selection_INPUT', $data);
$pac->enable_multiple(true);    // hidden input only
$pac->set_init_selection(array(
                            array("id"=>"1", "text"=>"Apple"),
                            array("id"=>"5", "text"=>"Twitter")));
$pac->display();
?>
<input id="init_selection_INPUT" type="hidden" value="" />

<script>
    function get_init_selection(ctrl_id){
        alert($(ctrl_id).select2("val"));
        return false;
    };
</script>
<a href="#" onclick="get_init_selection('#init_selection_INPUT2')">show selected value</a>

No related content found.

Tags: