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

Yii2使用dropdownlist實現地區三級連動功能的方法

高洛峰
發布: 2016-12-23 17:48:08
原創
1563 人瀏覽過

本文實例講述了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中文網!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板