0

ここで何が問題なのか理解できないようです。これは、3.5 を使用している場合は問題なく機能しましたが、4.0 では機能しません。

この方法での入力の使用をサポートする select2.full.js も使用しています。

html:

<input id="vcreate-filter" type="text" name="settings[filter]" class="form-control" style="width:100%;"/>

js:

$("#vcreate-filter").select2({
    placeholder: "Select or enter application...",
    allowClear: true,
    multiple: false,
    ajax: {
        dataType: 'json',
        delay: 1000,
        type: 'post',
        url: '/process/get_application_list.php',
        data: function (term, page) {
            return {
                term: term, // search term
                page_limit: 25, // page size
                page: page // page number
            };
        },
        results: function (data, page) {
            var more = (page * 25) < data.total; // whether or not there are more results available
            return {
                results: data.results,
                more: more
            };
        }
    },
    createSearchChoice:function(term, data) {
        if ($(data).filter(function() {
            return this.text.localeCompare(term)===0; }).length===0) {
                return {id:term, text:term};
            }
        }
}).on('change', function() {
    $(this).valid();
});

get_application_list.php:

.......
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

// make sure there are some results else a null query will be returned
if( count($results) > 0 )
{
    foreach( $results as $row )
    {
        $ajax_result['results'][] = array(
            'id' => htmlspecialchars($row['event_target'], ENT_QUOTES, 'UTF-8'),
            'text' => htmlspecialchars($row['event_target'], ENT_QUOTES, 'UTF-8')
        );
    }
} 
else 
{
    // 0 results send a message back to say so.
    $ajax_result['results'][] = array(
        'id' => 0,
        'text' => 'No results found...'
    );
}

// return result array to ajax
echo json_encode($ajax_result);
4

1 に答える 1