Home > php教程 > PHP开发 > Yii2 uses dropdownlist to implement regional three-level linkage function

Yii2 uses dropdownlist to implement regional three-level linkage function

高洛峰
Release: 2016-12-23 17:48:08
Original
1585 people have browsed it

The example in this article describes how Yii2 uses dropdownlist to implement the regional three-level linkage function. Share it with everyone for your reference, the details are as follows:

View part:

<?php
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\search\service\ItemSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="row">
  <div class="item-search">
    <?php $form = ActiveForm::begin([
      &#39;action&#39; => [&#39;index&#39;],
      &#39;method&#39; => &#39;get&#39;,
      &#39;options&#39; => [&#39;class&#39; => &#39;form-inline&#39;]
    ]); ?>
    <?= $form->field($model, &#39;cityName&#39;, [&#39;options&#39; => [&#39;class&#39; => &#39;form-group col-lg-2&#39;]])->dropDownList(ArrayHelper::map($cities, &#39;id&#39;, &#39;name&#39;), [&#39;prompt&#39; => &#39;请选择城市&#39;])->label(&#39;请选择城市&#39;, [&#39;class&#39; => &#39;sr-only&#39;]) ?>
    <?= $form->field($model, &#39;areaName&#39;, [&#39;options&#39; => [&#39;class&#39; => &#39;form-group col-lg-2&#39;]])->dropDownList(ArrayHelper::map($areas, &#39;id&#39;, &#39;name&#39;), [&#39;prompt&#39; => &#39;请选择区县&#39;])->label(&#39;请选择区县&#39;, [&#39;class&#39; => &#39;sr-only&#39;]) ?>
    <?= $form->field($model, &#39;communityName&#39;, [&#39;options&#39; => [&#39;class&#39; => &#39;form-group col-lg-2&#39;]])->dropDownList(ArrayHelper::map($communities, &#39;id&#39;, &#39;name&#39;), [&#39;prompt&#39; => &#39;请选择小区&#39;])->label(&#39;请选择小区&#39;, [&#39;class&#39; => &#39;sr-only&#39;]) ?>
    <div class="col-lg-2 col-lg-offset-1">
      <input class="form-control" id="keyword" placeholder="请输入小区名" value="" />
    </div>
    <div class="col-lg-1">
      <button type="button" id="search-community" class="btn btn-info">搜索</button>
    </div>
    <p></p>
    <div class="form-group col-lg-1 pull-right">
      <?= Html::submitButton(&#39;搜索&#39;, [&#39;class&#39; => &#39;btn btn-primary&#39;]) ?>
    </div>
    <?php ActiveForm::end(); ?>
  </div>
</div>
<p> </p>
<?php
$this->registerJs(&#39;
  //市地址改变
  $("#itemsearch-cityname").change(function() {
    //市id值
    var cityid = $(this).val();
    $("#itemsearch-areaname").html("<option value=\"0\">请选择区县</option>");
    $("#itemsearch-communityname").html("<option value=\"0\">请选择小区</option>");
    if (cityid > 0) {
      getArea(cityid);
    }
  });
  //区地址改变
  $("#itemsearch-areaname").change(function() {
    //区id值
    var areaid = $(this).val();
    $("#itemsearch-communityname").html("<option value=\"0\">请选择小区</option>");
    if (areaid > 0) {
      getCommunity(areaid);
    }
  });
  //获取市下面的区列表
  function getArea(id)
  {
    var href = "&#39; . Url::to([&#39;/service/base/get-area-list&#39;], true). &#39;";
    $.ajax({
      "type" : "GET",
      "url"  : href,
      "data" : {id : id},
      success : function(d) {
        $("#itemsearch-areaname").append(d);
      }
    });
  }
  //获取区下面的小区列表
  function getCommunity(id)
  {
    var href = "&#39; . Url::to([&#39;/service/base/get-community-list&#39;], true) . &#39;";
    $.ajax({
      "type" : "GET",
      "url"  : href,
      "data" : {id : id},
      success : function(d) {
        $("#itemsearch-communityname").append(d);
      }
    });
  }
  //搜索小区
  $("#search-community").click(function() {
    var word  = $("#keyword").val();
    var areaid = $("#itemsearch-areaname option:selected").val();
    var href  = "&#39; . Url::to([&#39;/service/base/search-community&#39;], true) . &#39;";
    if (areaid > 0) {
      $.ajax({
        "type" : "GET",
        "url"  : href,
        "data" : {id : areaid, word : word},
        success : function(d) {
          $("#itemsearch-communityname").html(d);
        }
      });
    }
  });
&#39;);
?>
Copy after login

Model part:

is our commonly used ajax request. Of course, it needs to be directly combined into

class HuangYeError extends \yii\db\ActiveRecord
{
  public $cityName;
  public $areaName;
  public $communityName;
  public $group;
  public $cate;
  /**
   * @inheritdoc
   */
  public static function tableName()
  {
    return &#39;ll_hy_huangye_error&#39;;
  }
  public static function getDb()
  {
    return Yii::$app->get(&#39;dbnhuangye&#39;);
  }
}
Copy after login

It used to be multiple tables, and you needed to use jjoinWith() to join the tables. Later, I converted them all into a single table. Multiple tables are really slow. If you can convert them into a single table, just use a single table. Table:

class HuangYeErrorSearch extends HuangYeError
{
  const PAGE_SIZE = 20;
  public $communityName;
  public $startTime;
  public $endTime;
  /**
   * @inheritdoc
   */
  public function rules()
  {
    return [
      [[&#39;id&#39;, &#39;serviceid&#39;, &#39;userid&#39;, &#39;categoryid&#39;, &#39;communityid&#39;, &#39;sortorder&#39;, &#39;ctime&#39;, &#39;utime&#39;, &#39;status&#39;], &#39;integer&#39;],
      [[&#39;username&#39;, &#39;name&#39;, &#39;logo&#39;, &#39;phone&#39;, &#39;address&#39;, &#39;content&#39;, &#39;error&#39;, &#39;communityName&#39;, &#39;startTime&#39;, &#39;endTime&#39;], &#39;safe&#39;],
    ];
  }
  /**
   * @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 = HuangYeError::find();
    //status == 9 删除状态
    $condition = &#39; `status` != :status&#39;;
    $p[&#39;:status&#39;] = 9;
    $query->where($condition, $p);
    $dataProvider = new ActiveDataProvider([
      &#39;query&#39; => $query,
      &#39;pagination&#39; => [
        &#39;pageSize&#39; => self::PAGE_SIZE,
      ],
    ]);
    $this->load($params);
    if (!$this->validate()) {
      // uncomment the following line if you do not want to any records when validation fails
      // $query->where(&#39;0=1&#39;);
      return $dataProvider;
    }
    $query->andFilterWhere([
      &#39;userid&#39; => $this->userid
    ]);
    $query->andFilterWhere([&#39;like&#39;, &#39;username&#39;, $this->username])
      ->andFilterWhere([&#39;like&#39;, &#39;name&#39;, $this->name])
      ->andFilterWhere([&#39;like&#39;, &#39;phone&#39;, $this->phone])
      ->andFilterWhere([&#39;like&#39;, &#39;address&#39;, $this->address])
      ->andFilterWhere([&#39;like&#39;, &#39;content&#39;, $this->content])
      ->andFilterWhere([&#39;ll_hy_huangye_error.status&#39; => $this->status])
      ->andFilterWhere([&#39;ll_hy_huangye_error.categoryid&#39; => $this->categoryid])
      ->andFilterWhere([&#39;between&#39;, &#39;ctime&#39;, strtotime($this->startTime . &#39;0:0:0&#39;), strtotime($this->endTime . &#39;23:59:59&#39;)])
      ->andFilterWhere([&#39;like&#39;, &#39;error&#39;, $this->error]);
    if (intval($this->communityName)) {
      $query->andFilterWhere([&#39;ll_hy_huangye_error.communityid&#39; => intval($this->communityName)]);
    }
    $order = &#39; `ctime` DESC&#39;;
    $query->orderBy($order);
    return $dataProvider;
  }
}
Copy after login

Writing in the controller is a little simpler, just call it directly:

/**
* ajax请求小区
*
* @param $id
* @return string
*/
public function actionGetCommunityList($id)
{
    $option = &#39;&#39;;
    $result = self::getCommunity($id);
    if ($result) {
      foreach ($result as $value) {
        $option .= &#39;<option value="&#39; . $value[&#39;id&#39;] . &#39;">&#39; . $value[&#39;name&#39;] . &#39;</option>&#39;;
      }
    } else {
      $option .= &#39;<option value="0">暂未开通可选择的小区</option>&#39;;
    }
    echo $option;
}
Copy after login

I hope this article will be helpful to everyone’s PHP programming based on the Yii framework.

For more related articles on how Yii2 uses dropdownlist to implement regional three-level linkage functions, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template