我想在Yii2中进行文本字段搜索。以前我在Yii2上没有Gii就犯了。如何在不使用Gii的情况下做到这一点?
视图中的代码index.php
<?php
use yii\helpers\Html;
use app\models\Donaturs;
use yii\grid\GridView;
use app\models\DonaturSearch;
?>
<h1>Data Donatur</h1>
<?php if (Yii::$app->session->hasFlash('success', 'delete')): ?>
<div class="alert alert-success alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
<h4><i class="icon fa fa-check"></i>Informasi!</h4>
<?= Yii::$app->session->getFlash('success') ?>
<?= Yii::$app->session->getFlash('delete') ?>
</div>
<?php endif; ?>
<?php if (Yii::$app->session->hasFlash('delete')): ?>
<div class="alert alert-success alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
<h4><i class="icon fa fa-check"></i>Informasi!</h4>
<?= Yii::$app->session->getFlash('delete') ?>
</div>
<?php endif; ?>
<?php
echo Html::a('Tambah data', ['create'],['class'=>'btn btn-primary']);
echo "<p></p>";
echo "<table class='table table-bordered table-striped'>";
echo "<tr>";
echo "<th>Kode Donatur</th>";
echo "<th>Nama Donatur</th>";
echo "<th>Alamat</th>";
echo "<th>Telepon</th>";
echo "<th>Aksi</th>";
echo "</tr>";
foreach ($donatur as $donatur) {
echo "<tr>";
echo "<td>".$donatur->kode_donatur."</td>";
echo "<td>".$donatur->nama_donatur."</td>";
echo "<td>".$donatur->alamat."</td>";
echo "<td>".$donatur->telepon."</td>";
echo "<td>";
echo Html::a('<i class="glyphicon glyphicon-pencil"></i>', ['update', 'kode_donatur'=>$donatur->kode_donatur]);
echo " ";
echo " ";
echo " ";
echo " ";
echo Html::a('<i class="glyphicon glyphicon-trash"></i>', ['delete', 'kode_donatur'=>$donatur->kode_donatur],
['onclick'=>'return (confirm("Apakah data ingin dihapus ?")?true:false);']);
echo "</td>";
echo "</tr>";
}
echo "</table>";
// display pagination
echo \yii\widgets\LinkPager::widget(['pagination' => $pages,
]);
控制器中的代码
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\Donatur;
class DonaturController extends Controller
{
public function actionCreate()
{
$model = new Donatur();
if (Yii::$app->request->post()) {
$model->load(Yii::$app->request->post());
if ($model->save()) {
\Yii::$app->session->setFlash('success', 'Data berhasil disimpan!');
} else {
Yii::$app->session->setFlash('error', 'Data gagal disimpan!');
}
return $this->redirect(['index']);
return $this->refresh();
}
else {
return $this->render('create', ['model' => $model]);
}
}
public function actionUpdate($kode_donatur)
{
$model = Donatur::findOne($kode_donatur);
if (Yii::$app->request->post()) {
$model->load(Yii::$app->request->post());
if ($model->save()) {
Yii::$app->session->setFlash('success', 'Data berhasil diupdate!');
} else {
Yii::$app->session->setFlash('error', 'Data gagal diupdate!');
}
return $this->redirect(['index']);
return $this->refresh();
}
else {
return $this->render('update', ['model' => $model]);
}
}
public function actionDelete($kode_donatur)
{
$model = Donatur::findOne($kode_donatur);
$model->delete();
Yii::$app->session->setFlash('delete', 'Data berhasil dihapus!');
return $this->redirect(['index']);
}
function actionIndex()
{
$query = Donatur::find();
$countQuery = clone $query;
$pages = new \yii\data\Pagination([
'totalCount' => $countQuery->count(),
'defaultPageSize' => 5,
]);
$donatur = $query->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('index', [
'donatur' => $donatur,
'pages' => $pages,
]);
}
}
代码模型
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Donatur extends ActiveRecord
{
public static function tableName()
{
return 'donatur';
}
public function rules()
{
return [
[['kode_donatur', 'nama_donatur', 'alamat', 'telepon'], 'required'],
[['kode_donatur', 'nama_donatur', 'alamat'], 'string'],
[['telepon'], 'integer'],
];
}
}
答案 0 :(得分:0)
答案 1 :(得分:-1)
只需将这一行放在列表视图上方,并保留要搜索的必填字段。
echo $this->render('_search', ['model' => $searchModel]);