【问题标题】:Searching/filtering an array搜索/过滤数组
【发布时间】:2015-12-21 06:30:02
【问题描述】:

我有一个问题。在我的网站(基于 Concrete 5)上,我有一个块来显示图标列表。我有这个数组。 (该数组的图标比我显示的要多得多,这只是一个示例。)

    <?php  $options = array(
    'icon-adjust' => 'Icon Adjust',
    'icon-adjustments' => 'Icon Adjustments',
    'icon-adn' => 'Icon Adn',
    'icon-alarmclock' => 'Icon Alarmclock',
    'icon-align-center' => 'Icon Align-center',
    'icon-align-justify' => 'Icon Align-justify',
    'icon-align-left' => 'Icon Align-left',
    'icon-align-right' => 'Icon Align-right',
    'icon-ambulance' => 'Icon Ambulance',
    'icon-anchor' => 'Icon Anchor',
    'icon-anchor2' => 'Icon Anchor2',
);
?>

我想修改我的搜索框,以便用户可以输入一些内容以过滤掉数组中的结果。 (例如:如果他们搜索锚点,他们会看到两个图标锚点字段)。

谁能帮我解决这个问题?

这是我的代码供将来参考:

    <script>
$('input[name=fromPages]').select2({
    placeholder: "<?=t('Search for a Page')?>",
    minimumInputLength: 1,
    width: '100%',
    multiple: true,
    ajax: {
        url: "<?=$view->action('load_pages')?>",
        dataType: 'json',
        quietMillis: 250,
        data: function (term, page) {
            return {
                q: term
            };
        },
        results: function (data, page) { // parse the results into the format expected by Select2.
            var results = [];
            $.each(data, function(i, concretePage) {
                results.push({'id': concretePage.id, 'text': concretePage.name});
            });
            return {
                results: results
            };
        },
        cache: true
    }
});
</script>

对于选择框:

<div class="form-group">
    <?php echo $form->label('fromPages', 'Search for an Icon')?>
    <div>
        <?php echo $form->hidden('fromPages'); ?>
    </div>
</div>

和一张图片:(顶部的搜索框会从打开的列表中过滤结果) http://i.imgur.com/Ksrb7iQ.png?1

【问题讨论】:

  • PROTIP:用您正在使用的语言标记问题。

标签: php arrays search filter typeahead


【解决方案1】:

查看 typeahead.js 库,您无法仅使用 php 来完成此操作

https://twitter.github.io/typeahead.js/

【讨论】:

猜你喜欢
  • 2020-05-30
  • 1970-01-01
  • 2018-12-25
  • 2012-03-30
  • 2022-01-02
  • 2021-12-16
  • 2021-07-22
  • 2023-01-19
相关资源
最近更新 更多