搜尋
首頁php教程PHP开发Yii2使用dropdownlist實現地區三級連動功能的方法

本文實例講述了Yii2使用dropdownlist實現地區三級連動功能的方法。分享給大家供大家參考,具體如下:

視圖部分:

<?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;);
?>

模型部分:

就是我們常用的ajax請求,當然php中需要直接組合成這樣的結構直接用,$form->field($model, $var)中的變數資料表中不一定有,得在模型中自己定義,並設定安全欄位,而且搜尋模型也可能需要修改成自己需要的樣子,模型可能要這樣:

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;);
  }
}

之前是多表,需要使用jjoinWith()連表,後來被我全部轉化為單表了,多表實在是慢,能轉化成單表就用單表吧:

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;
  }
}

控制器中寫比較簡單一點,直接調用就行了:

/**
* 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;
}

希望本文所述對大家基於Yii框架的PHP程式設計有所幫助。

更多Yii2使用dropdownlist實現地區三級連動功能的方法相關文章請關注PHP中文網!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版