DataTable .rowIndex始终返回0

时间:2015-07-12 13:35:00

标签: jsf datatable jsf-2.2

我做得不对?

index

在JSF中:

@Named("utilityController")
@RequestScoped
public class UtilityController {
    public DataModel<Result> getResultSample() {
        Result[] resultSample = new Result[11];
        //Populate the array
        return new ArrayDataModel<>(resultSample);
    }
}

enter image description here

如上所示,rowIndex总是返回0。请帮助我指出我做错了什么

1 个答案:

答案 0 :(得分:3)

  

我做得不对吗?

在getter方法中创建模型。永远不要那样做。所有getter方法应如下所示:

   <?php

namespace app\models;

use Yii;

/**
* This is the model class for table "faixas".
*
* @property integer $id
* @property string $nome
* @property string $ficheiro
* @property string $dummy1
* @property string $dummy2
* @property string $dummy3
* @property string $dummy4
* @property string $dummy5
*/
class Faixas extends \yii\db\ActiveRecord
{
  /**
   * @inheritdoc
   */
 public static function tableName()
{
    return 'faixas';
}

public $files;

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['nome', 'ficheiro', 'dummy1', 'dummy2', 'dummy3', 'dummy4', 'dummy5'], 'string', 'max' => 255],
        [['files'], 'file', 'skipOnEmpty' => false, 'extensions' => 'mp3, ogg, webm', 'maxFiles' => 30],
    ];
}

public function upload()
{   

    if ($this->validate()) { 
        foreach ($this->files as $file) {
                $file->saveAs('audio/' . $file->baseName . '.' . $file->extension);
                $model = new Faixas();
                // this part in not working, why ????
                $filePath = 'audio/'  . $file->baseName . '.' . $file->extension;
                $model->ficheiro = $filePath;
                $model->save();
    }
        return true;
    } else {
        return false;
    }
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => Yii::t('app', 'ID'),
        'nome' => Yii::t('app', 'Nome'),
        'ficheiro' => Yii::t('app', 'Ficheiro'),
        'dummy1' => Yii::t('app', 'Dummy1'),
        'dummy2' => Yii::t('app', 'Dummy2'),
        'dummy3' => Yii::t('app', 'Dummy3'),
        'dummy4' => Yii::t('app', 'Dummy4'),
        'dummy5' => Yii::t('app', 'Dummy5'),
    ];
}

// coloca o Audio na GridView
public function getAudio()
{
    return \Yii::$app->request->BaseUrl.'/'.$this->ficheiro;
}

每次迭代轮次都会调用getter方法。您基本上清除了上一轮迭代中的模型并返回一个全新的模型,所有状态(例如当前行索引)都重置为默认值。

将该作业移至bean的public DataModel<Result> getResultSample() { return resultSample; } 方法。

@PostConstruct

关于您的具体功能要求,您也可以引用UIData#getRowIndex()而不将值包装在private DataModel<Result> resultSample; @PostConstruct public void init() { Result[] results = new Result[11]; // ... resultSample = new ArrayDataModel<Result>(results); } public DataModel<Result> getResultSample() { return resultSample; } 中。

DataModel
public Result[] getResults() { // Consider List<Result> instead.
    return results;
}

请注意,我将其增加为1,因为它基于0,而人类期望基于1的索引。

另见: