Yii2 列表数据中的下拉列表

Yii2 列表数据中的下拉列表,yii2,yii2-advanced-app,yii2-basic-app,Yii2,Yii2 Advanced App,Yii2 Basic App,这是我的代码: [ 'attribute' => 'status', 'value' => function ($model) { return Html::dropDownList('status', ['10' => 'Active', '20' => 'Deactive']); }, ], 我只想要状态栏中的下拉列表。如果记录处于活动或非活动状态,则将选中该记录 使用属性呈现HTML元素。例如: [ 'attribu

这是我的代码:

[
    'attribute' => 'status',
    'value' => function ($model) {
        return Html::dropDownList('status', ['10' => 'Active', '20' => 'Deactive']);
    },
],
我只想要状态栏中的下拉列表。如果记录处于活动或非活动状态,则将选中该记录

使用属性呈现HTML元素。例如:

[
    'attribute' => 'status',
    'content' => function ($model) {
       return Html::dropDownList('status', $model->status, ['10' => 'Active', '20' => 'Deactive']);
    },
],
使用属性呈现HTML元素。例如:

[
    'attribute' => 'status',
    'content' => function ($model) {
       return Html::dropDownList('status', $model->status, ['10' => 'Active', '20' => 'Deactive']);
    },
],
您需要对列选项使用'format'=>'raw',并且您对dropDownList的定义是错误的。您需要将选择字符串作为第二个参数,将下拉选项作为第三个参数。将代码更改为以下内容:

[
    'attribute' => 'status',
    'format' => 'raw',
    'value' => function ($model) {
        return Html::dropDownList('status', $model->status, ['10' => 'Active', '20' => 'Deactive']);
    },
],
编辑 在最初的需求中,当下拉列表更改时,您也不希望更新状态。您可以将ajax调用绑定到下拉列表

在初始化GridView的视图顶部添加以下javascript

注意:将ajax调用中的url:“controller/updatestatus?id”+id更改为要更新行状态的相对控制器,但不要删除该id

然后转到控制器并添加更新状态的操作代码

注意:更改第一行中的模型$Model=Model::findOne$id;使用您正在使用的相应型号命名

public function actionUpdateStatus($id) {
    $model = Affiliate::findOne($id);
    $app = Yii::$app;
    $request = $app->request;

    if($request->IsAjax && $request->isPost) {

        Yii::$app->response->format = Response::FORMAT_JSON;

        if($model->load($request->post()) && $model->save()) {
            return ['success' => true];
        } else {
            return [
                'success' => false,
                'message' => implode('<br />', ArrayHelper::getColumn($model->errors, '0'))
            ];
        }
    }
}
您需要对列选项使用'format'=>'raw',并且您对dropDownList的定义是错误的。您需要将选择字符串作为第二个参数,将下拉选项作为第三个参数。将代码更改为以下内容:

[
    'attribute' => 'status',
    'format' => 'raw',
    'value' => function ($model) {
        return Html::dropDownList('status', $model->status, ['10' => 'Active', '20' => 'Deactive']);
    },
],
编辑 在最初的需求中,当下拉列表更改时,您也不希望更新状态。您可以将ajax调用绑定到下拉列表

在初始化GridView的视图顶部添加以下javascript

注意:将ajax调用中的url:“controller/updatestatus?id”+id更改为要更新行状态的相对控制器,但不要删除该id

然后转到控制器并添加更新状态的操作代码

注意:更改第一行中的模型$Model=Model::findOne$id;使用您正在使用的相应型号命名

public function actionUpdateStatus($id) {
    $model = Affiliate::findOne($id);
    $app = Yii::$app;
    $request = $app->request;

    if($request->IsAjax && $request->isPost) {

        Yii::$app->response->format = Response::FORMAT_JSON;

        if($model->load($request->post()) && $model->save()) {
            return ['success' => true];
        } else {
            return [
                'success' => false,
                'message' => implode('<br />', ArrayHelper::getColumn($model->errors, '0'))
            ];
        }
    }
}

如何选择并更新此值ie ONCHANGE您应该使用ajax将更改请求发送到后端,这必须更新模型的状态如何选择并更新此值ie ONCHANGE您应该使用ajax将更改请求发送到后端,它必须更新模型的状态