尝试在Yii2中加载双重列表框时出错 [英] Error when trying to load Dual Listbox in Yii2

查看:72
本文介绍了尝试在Yii2中加载双重列表框时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我要开发的项目中,用户可以将Excel文件(xls,xlsx)上传到系统. Excel工作表的第一行包含标题,另一行包含值.系统具有默认的excel模板, 包含标头序列的规则,例如(名称,年龄,性别),但有时用户使用自己的excel模板,因此有时标头序列变得像这样(性别,名称,年龄).

In the project that I want to develop, user can upload an Excel file (xls,xlsx) to system. The excel sheet has headers in the first row, and value in another row. System has a default excel template that consist the rule for headers sequence such as (Name, Age, Sex), but sometimes user use their own excel template so sometimes the header sequence become like this (Sex, Name, Age).

要处理此序列,我计划在将值保存到数据库之前进行映射过程以处理标头序列.我想使用双重清单框. 我在数据库中有2个表用于此过程:

To handle this sequence, I've a plan to make a mapping process to handle the headers sequence before save the value to database. I wanna use dual list box. I've 2 a table in database for this process:

  1. Header->必须列(header_id,header_name),文件的所有标题都保存在这里. 每个标头都保存有自己的header_id和header_name
  2. 信息-> excel文件中的值保存在这里.
  1. Header -> has to column (header_id, header_name), all the headers from file has been save in here. Each headers saved with their own header_id and header_name
  2. Info -> the value from the excel file save here.

而且我还有一个纯粹的(不是Gii生成的)CostumizedHeaderController.php,CostumizeHeader.php,Index.php

and I also has a pure (not generated by Gii) CostumizedHeaderController.php, CostumizeHeader.php, Index.php

这是CostumizeHeaderController中的代码:

This is code in CostumizeHeaderController:

class CostumizeHeaderController extends Controller {

//put your code here

public function actionShowHeaders() {
$model = new CostumizeHeader();
$model->loadHeaders();
    $items = \backend\models\CostumizeHeader::getAllHeader();

    return $this->render('index', [
                'model' => $model,
                'items' => $items
    ]);
}}

模型中的代码(CostumizeHeader.php)

Code in model (CostumizeHeader.php)

class CostumizeHeader {
//put your code here

/**
 * @var array IDs of the favorite foods
 */
public $list_headers = [];

public function rules() {
    return [
            [['list_headers'], 'string', 'max' => 255],
    ];
}

/**
 * @return array customized attribute labels
 */
public function attributeLabels() {
    return [
        'list_headers' => 'list Costumized Header',
    ];
}

public function loadHeaders() {
    $this->list_headers = [];
    $headers = Header::find()->all();
    foreach ($headers as $ff) {
        $this->list_headers[] = $ff->header_id;
    }
}

public static function getAllHeader() {
    $headers = Header::find()->asArray()->all();
    $items = ArrayHelper::map($headers, 'header_id', 'nama_header');
    return $items;
}

index.php中的代码

code in index.php

 <?php
$form = ActiveForm::begin([
            'id' => 'favorite-form',
            'enableAjaxValidation' => false,
]);
?>

<?= $form->field($model->list_headers, 'list_headers')->widget(DualListbox::className(), [
            'model' => $model,
            'items' => $items,
            'name'=>'nama_header',
            'attribute' => 'list_headers', 
            'options' => [
                'multiple' => true,
                'size' => 15,
            ],
            'clientOptions' => [
                'nonSelectedListLabel' => 'Available Header',
                'selectedListLabel' => 'Final Header',
                'moveOnSelect' => false,
            ],
        ])
        ->hint('Select the header according the sequence.');
?>

我尝试在控制器中使用var_dump并获得了这个Array([1] => age [2] => sex [3] => name. 而且我一直在检查标头表,并且excel文件中的所有标头都已导入数据库. 但是我仍然遇到错误,例如在非对象上调用成员函数formName(). 我希望任何人都能帮助我解决这个问题.谢谢

I've try to var_dump in controller and got this Array ( [1] => age [2] => sex [3] => name . And I've been check the header table, and all the headers from excel file have been imported to database. But I still got an error, like this Call to a member function formName() on a non-object. I wish anybody can help me to solve this problem. Thankyou

推荐答案

在您的视图页面内,我认为您错误地使用了字段$model->list_headers应该仅是$model.

Inside your view page I think you are using field incorretly $model->list_headers should be $model only.

您的index.php必须如下:

<?php
$form = ActiveForm::begin([
            'id' => 'favorite-form',
            'enableAjaxValidation' => false,
]);
?>

<?= $form->field($model, 'list_headers')->widget(DualListbox::className(), [
            'model' => $model,
            'items' => $items,
            'name'=>'nama_header',
            'attribute' => 'list_headers', 
            'options' => [
                'multiple' => true,
                'size' => 15,
            ],
            'clientOptions' => [
                'nonSelectedListLabel' => 'Available Header',
                'selectedListLabel' => 'Final Header',
                'moveOnSelect' => false,
            ],
        ])
        ->hint('Select the header according the sequence.');
?>

这篇关于尝试在Yii2中加载双重列表框时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆