【问题标题】:jQuery select2 dynamic optionsjQuery select2 动态选项
【发布时间】:2016-03-16 10:46:15
【问题描述】:

我有一个多选,我想将其用作搜索框,以便用户可以按类别、事件类型、位置和关键字进行搜索。它的结构如下:

<select name="search-term[]" multiple="multiple">
    <optgroup label="Categories">
        <option value="category_4">Internal</option>
        <option value="category_2">Business</option>
        <option value="category_5">External</option>
        <option value="category_1">Science</option>
        <option value="category_6">Sports and Social</option>
    </optgroup>
    <optgroup label="Event Types">
        <option value="eventtype_2">Meeting</option>
        <option value="eventtype_3">Social Activity</option>
        <option value="eventtype_4">Sporting Activity</option>
        <option value="eventtype_1">Symposium</option>
    </optgroup>
    <optgroup label="Locations">
        <option value="location_2">Office 1</option>
        <option value="location_3">Office 2</option>
        <option value="location_1">Office 3</option>
    </optgroup>
</select>

我已经初始化了 select2,并将 tags 选项设置为 true,如下所示:

$('select').select2({
    tags : true,
    createTag: function (params)
    {
        return {
            id: 'keyword_' + params.term,
            text: params.term,
            newOption: true
        }
    }
});

这允许用户在新选项不存在时输入新选项并满足关键字要求。任何新标签都附加keyword_,以便服务器在提交表单时知道如何处理它们。

一切正常它只会让他们选择现有的选项。例如,如果我搜索Office 1,我可能想要搜索位于办公室 1 的活动,或者我可能想要进行关键字搜索,以便搜索标题中包含办公室 1 的活动。问题是目前我只能选择无法创建新标签的位置选项。有谁知道我如何做到这一点?

【问题讨论】:

    标签: javascript jquery select2


    【解决方案1】:

    我最终通过使用 AJAX 数据源实现了这一点,它使您可以更好地控制向用户显示哪些选项。这是我的代码:

    $('select').select2({
        ajax: {
            url: "/server.php",
            dataType: 'json',
            type: "GET",
            delay: 0,
            data: function (params) {
                var queryParameters = {
                    term: params.term
                }
                return queryParameters;
            },
            processResults: function (data) {
                return {
                    results: $.map(data, function (item) {
                        return {
                            text: item.text,
                            id: item.id,
                            children: item.children
                        }
                    })
                };
            },
            cache: false
        },
        templateSelection: function(item)
        {
            return item.parent+': '+item.text;
        }
    });
    

    server.php 的内容:

    <?php
    $term = !isset($_GET['term']) ? null : ucfirst($_GET['term']);
    
    $categories = array('Meeting', 'Seminar', 'Sports and Social');
    $locations = array('Cambridge', 'London', 'Northwich');
    $matching_categories = array();
    $matching_locations = array();
    
    foreach($categories as $i => $cat) {
        if(is_null($term) || stripos($cat, $term)!==false) {
            $matching_categories[] = array(
                'id' => 'category_'.$i,
                'text' => $cat,
                'parent' => 'Category'
            );
        }
    }
    
    foreach($locations as $i => $loc) {
        if(is_null($term) || stripos($loc, $term)!==false) {
            $matching_locations[] = array(
                'id' => 'location_'.$i,
                'text' => $loc,
                'parent' => 'Location'
            );
        }
    }
    
    $options = array();
    
    if(!empty($matching_categories)) {
        $options[] = array(
            'text' => 'Category',
            'children' => $matching_categories
        );
    }
    
    if(!empty($matching_locations)) {
        $options[] = array(
            'text' => 'Location',
            'children' => $matching_locations
        );
    }
    
    if(!is_null($term)) {
        $options[] = array(
            'text' => 'Keyword',
            'children' => array(
                array(
                    'id' => 'keyword_'.$term,
                    'text' => $term,
                    'parent' => 'Keyword'
                )
            )
        );
    }
    
    echo json_encode($options);
    

    【讨论】:

      猜你喜欢
      • 2021-09-03
      • 2015-10-10
      • 2018-06-12
      • 2015-08-16
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      相关资源
      最近更新 更多