Elementor 开发者文档

title: "Select2 控件" post_status: publish comment_status: open taxonomy: category: - elementor-developers-docs post_tag: - Editor Controls - Src - Repos


Select2 控件

Select2 Control

Elementor select2 控件基于 Select2 库显示一个选择框控件。它接受一个数组,其中 key 是值,value 是选项名称。将 multiple 设置为 true 以允许多值选择。

该控件在继承 Base_Data_Control 类的 Control_Select2 类中定义。

使用此控件时,type 应设置为 \Elementor\Controls_Manager::SELECT2 常量。

参数

名称 类型 默认值 描述
type string select2 控件的类型。
label string 显示在字段上方的标签。
description string 显示在字段下方的描述。
show_label bool true 是否显示标签。
label_block bool false 是否在单独一行显示标签。
separator string default 设置控件分隔符的位置。可用值为 defaultbeforeafterdefault 将隐藏分隔符,除非控件类型有特定的分隔符设置。before / after 会将分隔符定位在控件之前/之后。
options array 一个 键 => 值 对的数组:[ '键' => '值', ... ]
multiple bool false 是否允许多值选择。
default string|array 选中的选项键,或者如果 multiple 设置为 true,则为选中值的数组。

Return Value

(string|array) The selected option key, or an array of selected keys if multiple is set to true.

Usage

```php {14-28,36-42,47-53}

start_controls_section( 'content_section', [ 'label' => esc_html__( 'Content', 'textdomain' ), 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'list', [ 'label' => esc_html__( 'Show Elements', 'textdomain' ), 'type' => \Elementor\Controls_Manager::SELECT2, 'label_block' => true, 'multiple' => true, 'options' => [ 'title' => esc_html__( 'Title', 'textdomain' ), 'description' => esc_html__( 'Description', 'textdomain' ), 'button' => esc_html__( 'Button', 'textdomain' ), ], 'default' => [ 'title', 'description' ], ] ); $this->end_controls_section(); } protected function render(): void { $settings = $this->get_settings_for_display(); if ( $settings['list'] ) { echo ''; } } protected function content_template(): void { ?>
    <# if ( settings.list.length ) { #>
        <ul>
        <# _.each( settings.list, function( item ) { #>
            <li>{{{ item }}}</li>
        <# } ) #>
        </ul>
    <# } #>
    <?php
}

} ```