Yii 2 - radioList 模板 [英] Yii 2 - radioList Template

查看:23
本文介绍了Yii 2 - radioList 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 yii2 中向 radioList 添加模板,我试过了,但我无法获得正确的 o/p.

HTML 是

<label class="gender-head">性别</label><label class="signup-radio"><input type="radio" name="signup-gender" id="signupMale" 勾选 tabindex="3"/><i></i><span>男</span><label class="signup-radio"><input type="radio" name="signup-gender" id="signupFemale" tabindex="3"/><i></i><span>女性</span>

o/p 应该是这样的

我尝试的 Yii2 代码是...

<div class="clearfix"><?= $form->field($model, 'gender', ['radioTemplate' => '<label class="gender-head">{label}</label><label class="signup-radio">{input}</label>'])->inline()->radioList([1 => 'Male', 0 => 'Female'], ['分隔符' => '', 'tabindex' => 3]);?>

<div class="help-block"></div>

我在模板上搜索了很多,但没有得到任何正确的回应.

解决方案

我终于找到了修改Yii2中输入标签生成逻辑的方法

为了获得单选按钮的上述结果,我开发了以下代码

 

<div class="clearfix" id="UserLogin-gender"><label class="radio-head">性别</label><?=$form->field($model, '性别')->radioList([1 =>'男',0 =>'女性'],['项目' =>函数($index,$label,$name,$checked,$value){$return = '';返回 $return;}])-> 标签(假);?>

<div class="help-block"></div>

radioList 中的item"选项是 Yii2 编写的输入生成逻辑的回调函数.我们可以轻松地修改使用此回调函数生成的每个元素的布局及其参数.

I want to add template to radioList in yii2, which I tried, but I am unable to get the proper o/p.

The HTML is

<div class="input-wrap">
    <label class="gender-head">Gender</label>
    <label class="signup-radio">
        <input type="radio" name="signup-gender" id="signupMale" checked  tabindex="3" />
        <i></i>
        <span>Male</span>
    </label>
    <label class="signup-radio">
        <input type="radio" name="signup-gender" id="signupFemale" tabindex="3" />
        <i></i>
        <span>Female</span>
    </label>
</div>

The o/p should look like this

The Yii2 code which I tried is...

<div class="input-wrap">
    <div class="clearfix">
        <?= $form->field($model, 'gender', ['radioTemplate' => '<label class="gender-head">{label}</label><label class="signup-radio">{input}</label>'])->inline()->radioList([1 => 'Male', 0 => 'Female'], ['separator' => '', 'tabindex' => 3]); ?>
     </div>
     <div class="help-block"></div>
</div>

I have searched a lot on the template but did not get any proper response.

解决方案

I finally got the way through which we can modify the input tag generation logic in Yii2

To get the above result of the radio buttons, I have developed the following code

            <div class="input-wrap">
                <div class="clearfix" id="UserLogin-gender">
                    <label class="radio-head">Gender</label>
                    <?=
                    $form->field($model, 'gender')
                        ->radioList(
                            [1 => 'Male', 0 => 'Female'],
                            [
                                'item' => function($index, $label, $name, $checked, $value) {

                                    $return = '<label class="modal-radio">';
                                    $return .= '<input type="radio" name="' . $name . '" value="' . $value . '" tabindex="3">';
                                    $return .= '<i></i>';
                                    $return .= '<span>' . ucwords($label) . '</span>';
                                    $return .= '</label>';

                                    return $return;
                                }
                            ]
                        )
                    ->label(false);
                    ?>
                </div>
                <div class="help-block"></div>
            </div>

The "item" option in the radioList is a callback function to the input generation logic written in Yii2. We can easily modify the layout of each element generated using this callback function and it's parameters.

这篇关于Yii 2 - radioList 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆