【问题标题】:My searchable drop down list in yii2 is not working我在 yii2 中的可搜索下拉列表不起作用
【发布时间】:2021-02-03 13:01:12
【问题描述】:

yii2 开发人员,我是 yii2 的新手,所以我做了一个下拉搜索,当我从下拉列表中选择数据并点击显示“未找到结果”的搜索按钮时,我想搜索账单月份但是当我使用输入字段进行搜索时,它可以完美运行。

_搜索

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Bill;
use app\models\BillSearch;
use yii\helpers\ArrayHelper;


/* @var $this yii\web\View */
/* @var $model app\models\BillSearch */
/* @var $form yii\widgets\ActiveForm */
?> 

<div class="bill-search">

    <?php $form = ActiveForm::begin([
        'action' => ['bill-report'],
        'method' => 'get',
    ]); ?>

    <div class="card">
        <div class="row">
            <div class="col-lg-12">

                <div class="row" style="margin: 10px;">


                        <?php // echo $form->field($model, 'consumer_id') ?>

                        

                        <?php // echo $form->field($model, 'total_months_conservancy') ?>

                        <?php //echo $form->field($model, 'water_arrears') ?>

                        <?php // echo $form->field($model, 'water_remarks') ?>

                        

                        <?php // echo $form->field($model, 'cnic') ?>

                        
                           
                        <?php // echo $form->field($model, 'fileNo') ?>

                       

                        <?php // echo $form->field($model, 'total_water_and_conservancy_amount') ?>

                        <?php // echo $form->field($model, 'tinure') ?>

                        <?php // echo $form->field($model, 'dues_amount') ?>

                        <?php // echo $form->field($model, 'per_month_charges') ?>

                        <?php // echo $form->field($model, 'no_of_months') ?>

                        <?php // echo $form->field($model, 'total_amount_of_current_months') ?>

                        <?php // echo $form->field($model, 'advanced_pay') ?>

                        <?php // echo $form->field($model, 'arrears') ?>

                        <?php // echo $form->field($model, 'arrears_period') ?>

                        <?php // echo $form->field($model, 'total_bill') ?>

                        <?php // echo $form->field($model, 'after_due_date_charges') ?>

                        <?php // echo $form->field($model, 'total_after_due_date') ?>

                        <?php // echo $form->field($model, 'balance_arears') ?>

                        <?php // echo $form->field($model, 'remarks') ?>

                        <?php // echo $form->field($model, 'water_charges_per_month') ?>

                        <?php // echo $form->field($model, 'conservancy_charges_per_month') ?>

                        <?php // echo $form->field($model, 'four_months_water_charges') ?>

                        <?php // echo $form->field($model, 'four_months_conservancy_charges') ?>
                        <div class="col-lg-3">

                     <?= $form->field($model, 'billing_months')->dropDownList(
                ArrayHelper::map(bill::find()->all(),'id', 'billing_months'),
              ['prompt'=>'Select Tenure'])?>

                    </div>
                    <div class="col-lg-3"></div>
                    <div class="col-lg-3"></div>
                    <div class="col-lg-3"></div>

                        <?php // echo $form->field($model, 'issue_date') ?>

                        <?php // echo $form->field($model, 'due_date') ?>

                        <?php // echo $form->field($model, 'reference_no') ?>
                 

     <div class="col-lg-3">
       
       <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
        <?= Html::resetButton('Reset', ['class' => 'btn btn-outline-secondary']) ?>
    </div>
     </div>
      </div>
       </div>
        </div>

    <?php ActiveForm::end(); ?>

</div>

BillSearch 模型

<?php

namespace app\models;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Bill;
 
/**
 * BillSearch represents the model behind the search form of `app\models\Bill`.
 */
class BillSearch extends Bill
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id','total_months_conservancy','billing_months','water_arrears', 'conservancy_amount', 'total_water_and_conservancy_amount', 'tinure', 'dues_amount', 'per_month_charges', 'no_of_months', 'total_amount_of_current_months', 'advanced_pay', 'arrears', 'arrears_period', 'total_bill', 'after_due_date_charges', 'total_after_due_date', 'balance_arears'], 'integer'],
            [['water_remarks','consumer_id', 'status','file_no', 'cnic',  'allottee_name','remarks', 'issue_date', 'due_date', 'reference_no'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Bill::find();
        $query->leftjoin('plot','plot.id=bill.consumer_id');

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

    
        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'total_months_conservancy' => $this->total_months_conservancy,
            'water_arrears' => $this->water_arrears,
            'conservancy_amount' => $this->conservancy_amount,
            'total_water_and_conservancy_amount' => $this->total_water_and_conservancy_amount,
            'tinure' => $this->tinure,
            'dues_amount' => $this->dues_amount,
            'per_month_charges' => $this->per_month_charges,
            'no_of_months' => $this->no_of_months,
            'total_amount_of_current_months' => $this->total_amount_of_current_months,
            'advanced_pay' => $this->advanced_pay,
            'arrears' => $this->arrears,
            'arrears_period' => $this->arrears_period,
            'total_bill' => $this->total_bill,
            'after_due_date_charges' => $this->after_due_date_charges,
            'total_after_due_date' => $this->total_after_due_date,
            'balance_arears' => $this->balance_arears, 
            'issue_date' => $this->issue_date,
            'due_date' => $this->due_date,
            'billing_months'=>$this->billing_months,
        ]);
 
        $query->andFilterWhere(['like', 'water_remarks', $this->water_remarks])
            ->andFilterWhere(['like', 'remarks', $this->remarks])
              ->andFilterWhere(['like', 'plot.allottee_name', $this->consumer_id])
             ->andFilterWhere(['like', 'plot.cnic', $this->cnic])
              ->andFilterWhere(['like', 'plot.file_no', $this->fileNo])
            // ->andFilterWhere(['like', 'bill.billing_months', $this->billing_months])
            ->andFilterWhere(['like', 'reference_no', $this->reference_no]);
            

        return $dataProvider;
    }
} 
 

比尔模型

<?php

namespace app\models;

use Yii;
 
/** 
 * This is the model class for table "bill".
 *
 * @property int $id
 * @property int $consumer_id
 * @property int $total_months_conservancy
 * @property int $water_arrears
 * @property string $water_remarks
 * @property int $conservancy_amount
 * @property int $total_water_and_conservancy_amount
 * @property int $tinure
 * @property int $dues_amount
 * @property int $per_month_charges
 * @property int $no_of_months
 * @property int $total_amount_of_current_months
 * @property int $advanced_pay
 * @property int $arrears

* @property int $water_bill 
 * @property int $arrears_period
 * @property int $total_bill
 * @property int $after_due_date_charges
 * @property int $total_after_due_date
 * @property int $balance_arears
 * @property string $remarks
 * @property int $conservancy_charges_per_month

 * @property string $billing_months
 * @property string $issue_date
 * @property string $due_date 
  * @property string $cnic
 * @property string $reference_no
 */
class Bill extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public $file;
    public static function tableName()
    {
        return 'bill';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['consumer_id', 'water_arrears',  'conservancy_amount', 'dues_amount',  'arrears', 'arrears_period', 'total_bill', 'after_due_date_charges', 'total_after_due_date', 'issue_date', 'due_date','status'], 'required'],
            [['file'],'file'],
            [['consumer_id', 'total_months_conservancy', 'water_arrears', 'conservancy_amount', 'total_water_and_conservancy_amount', 'tinure', 'dues_amount', 'per_month_charges', 'no_of_months', 'total_amount_of_current_months', 'advanced_pay', 'arrears', 'arrears_period', 'total_bill', 'after_due_date_charges', 'total_after_due_date', 'balance_arears','payment','debit','sector'], 'integer'],
            [['issue_date', 'water_bill', 'due_date','created_on','updated_on',  'advanced_pay',  'tinure','billing_months','email','cnic'], 'safe'],
            [['water_remarks','document'], 'string', 'max' => 250],
            [['remarks'], 'string', 'max' => 350],
            [['billing_months'], 'string', 'max' => 25],
            [['reference_no'], 'string', 'max' => 13],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'Bill ID',
            'cnic'=>'cnic',
            'email'=>'Email',
            'total_months_conservancy' => 'Total Months Conservancy',
            'water_arrears' => 'Water Arrears',
            'water_remarks' => 'Water Remarks',
            'conservancy_amount' => 'Conservancy Amount',
            'total_water_and_conservancy_amount' => 'Total Water And Conservancy Amount',
            'tinure' => 'Tinure',
            'sector' => 'Sector',
            'dues_amount' => 'Dues Amount',
            'per_month_charges' => 'Per Month Charges',
            'no_of_months' => 'No Of Months',
            'total_amount_of_current_months' => 'Total Amount Of Current Months',
            'advanced_pay' => 'Advanced Pay',
            'debit' => 'debit Pay',
            'arrears' => 'Arrears',
             'water_bill' => 'water_bill',
            'arrears_period' => 'Arrears Period',
            'total_bill' => 'Total Bill',
            'status'=>'Status',
            'after_due_date_charges' => 'After Due Date Charges',
            'total_after_due_date' => 'Total After Due Date',
            'balance_arears' => 'Balance Arears',
            'remarks' => 'Remarks',
            'conservancy_charges_per_month' => 'Conservancy Charges Per Month',
            'billing_months' => 'Tenure',
            'issue_date' => 'Issue Date',
            'due_date' => 'Due Date',
            'reference_no' => 'Reference No',
        ];
    }

   
            public function getconsumerId()
            {
                return $this->hasOne(Plot::className(),['id' => 'consumer_id']);
            }

            public function getfileNo()
            {
                return $this->hasOne(Plot::className(),['id' => 'consumer_id']);
            }


   
}

索引

<?php
use yii\helpers\Html;

use yii\grid\GridView;
use yii\helpers\Url;
use yii\widgets\pjax;
use app\models\Bill;
use app\models\BillSearch;

 
/* @var $this yii\web\View */
/* @var $searchModel app\models\BillSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'bill-report';
$this->params['breadcrumbs'][] = $this->title;
?>

<?php echo $this->render('_billsearch', ['model' => $searchModel]); ?>

  <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
                
            // 'status',
            //'conservancy_amount',
            //'total_water_and_conservancy_amount',
            //'tinure',
            //'dues_amount',
            //'per_month_charges',
            //'no_of_months',
            //'total_amount_of_current_months',
            //'advanced_pay',
            //'arrears',
            //'arrears_period',
            
            //'after_due_date_charges',
            //'total_after_due_date',
            //'balance_arears',
            //'remarks',
            //'water_charges_per_month',
            //'conservancy_charges_per_month',
            //'four_months_water_charges',
            //'four_months_conservancy_charges',
            'billing_months',
            'total_bill',
            // 'payment',
            //'issue_date',
            //'due_date',
      
        ],
    ]);

【问题讨论】:

    标签: gridview yii2


    【解决方案1】:

    您的选择必须是:

    &lt;?= $form-&gt;field($model, 'billing_months')-&gt;dropDownList( ArrayHelper::map(bill::find()-&gt;all(),'billing_months', 'billing_months'), ['prompt'=&gt;'Select Tenure'])?&gt;

    您的数据库中有string 字段,而不是integer,因此您的选项值和文本必须使用字符串数据设置。

    BillSearch 中也修正你关于billing_months 的规则,例如:

    ['billing_months', 'safe']

    编辑:您还需要groupBy 以避免重复:

    bill::find()-&gt;groupBy('billing_months')-&gt;all()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多