将用户状态从SwitchInput更改为下拉列表


Change User Status From SwitchInput into Dropdown list

目前,我有一个现有的代码,根据user表的ban_time字段将用户状态显示为"非活动"或"活动"。当用户状态为"非活动"时,ban_time字段将按当前时间跨度更新(我猜它使用外部包装插件)

$form->field($user, 'ban_time')->widget(SwitchInput::classname(), [
            'type' => SwitchInput::CHECKBOX,
            'containerOptions' => ['class' => 'inner-form-group'],
            'pluginOptions' => [
                'state' => empty($user->ban_time),
                'handleWidth' => 60,
                'onText' => 'Active',
                'offText' => 'Inactive'
            ],
            'pluginEvents' => [
                "switchChange.bootstrapSwitch" => "function(event, state) { $('[name=''User[ban_time]'']').val(state ? 0 : 1) }",
            ]
        ])->label('Status'); 

现在,我需要添加更多的状态,而不是"非活动"或"活动"。所以我想把这个字段改成dropDownList,但当改变User的状态时,ban_time并没有改变

$form->field($user, 'ban_time')->dropDownList(
            [empty($user->ban_time) =>'Active', !empty($user->ban_time) =>'Inactive']
        )->label('Status'); 

请帮助我如何更改

例如,如果您在下面有这样的下拉列表

  echo $form->dropDownListGroup(
                    $model, 'status', array(
                'wrapperHtmlOptions' => array(),
                'widgetOptions' => array(
                    'data' =>$model->getDropdownvalue(),
                    'htmlOptions' => array(
                        'prompt' => 'Select Project',
                        'ajax' => array(
                                'type' => 'POST',
                                'url' => your url,
                                //'dataType' => 'json',
                                'data'=>array('status'=>'js:this.value'),
                              )

在您的控制器中,您将使用url 获得下拉列表的值

public function actiondropdownvalue(){
$model = new status();
        $status = $_POST['status'];
       $model->save();

这个例子只是展示了它的工作原理。您需要用户id来保存特定用户的状态以更新或保存状态。

您可以将表单元素设置为dropDownList作为

     $items = [1 =>'Active', 0 =>'Inactive' ,2 => 'Subscribed' ,3 => 'Deleted'];
     $form->field($user, 'ban_time')->dropDownList($items)->label('Status'); 

请参阅DropDownList