一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - Yii2使用dropdownlist實現地區三級聯動功能的方法

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

2021-02-06 16:00zhgxun PHP教程

這篇文章主要介紹了Yii2使用dropdownlist實現地區三級聯動功能的方法,結合實例形式較為詳細的分析了dropdownlist下拉列表實現三級聯動調用的具體步驟與相關注意事項,需要的朋友可以參考下

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

視圖部分:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?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([
      'action' => ['index'],
      'method' => 'get',
      'options' => ['class' => 'form-inline']
    ]); ?>
    <?= $form->field($model, 'cityName', ['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($cities, 'id', 'name'), ['prompt' => '請選擇城市'])->label('請選擇城市', ['class' => 'sr-only']) ?>
    <?= $form->field($model, 'areaName', ['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($areas, 'id', 'name'), ['prompt' => '請選擇區縣'])->label('請選擇區縣', ['class' => 'sr-only']) ?>
    <?= $form->field($model, 'communityName', ['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($communities, 'id', 'name'), ['prompt' => '請選擇小區'])->label('請選擇小區', ['class' => 'sr-only']) ?>
    <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('搜索', ['class' => 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>
  </div>
</div>
<p>&nbsp;</p>
<?php
$this->registerJs('
  //市地址改變
  $("#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 = "' . Url::to(['/service/base/get-area-list'], true). '";
    $.ajax({
      "type" : "GET",
      "url"  : href,
      "data" : {id : id},
      success : function(d) {
        $("#itemsearch-areaname").append(d);
      }
    });
  }
  //獲取區下面的小區列表
  function getCommunity(id)
  {
    var href = "' . Url::to(['/service/base/get-community-list'], true) . '";
    $.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  = "' . Url::to(['/service/base/search-community'], true) . '";
    if (areaid > 0) {
      $.ajax({
        "type" : "GET",
        "url"  : href,
        "data" : {id : areaid, word : word},
        success : function(d) {
          $("#itemsearch-communityname").html(d);
        }
      });
    }
  });
');
?>

模型部分:

就是我們常用的ajax請求,當然php中需要直接組合成<option value=""></option>這樣的結構直接用,$form->field($model, $var)中的變量數據表中不一定有,得在模型中自己定義,并設置安全字段,而且搜索模型也可能需要修改成自己需要的樣子,模型可能要這樣:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class HuangYeError extends \yii\db\ActiveRecord
{
  public $cityName;
  public $areaName;
  public $communityName;
  public $group;
  public $cate;
  /**
   * @inheritdoc
   */
  public static function tableName()
  {
    return 'll_hy_huangye_error';
  }
  public static function getDb()
  {
    return Yii::$app->get('dbnhuangye');
  }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class HuangYeErrorSearch extends HuangYeError
{
  const PAGE_SIZE = 20;
  public $communityName;
  public $startTime;
  public $endTime;
  /**
   * @inheritdoc
   */
  public function rules()
  {
    return [
      [['id', 'serviceid', 'userid', 'categoryid', 'communityid', 'sortorder', 'ctime', 'utime', 'status'], 'integer'],
      [['username', 'name', 'logo', 'phone', 'address', 'content', 'error', 'communityName', 'startTime', 'endTime'], '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 = HuangYeError::find();
    //status == 9 刪除狀態
    $condition = ' `status` != :status';
    $p[':status'] = 9;
    $query->where($condition, $p);
    $dataProvider = new ActiveDataProvider([
      'query' => $query,
      'pagination' => [
        'pageSize' => 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('0=1');
      return $dataProvider;
    }
    $query->andFilterWhere([
      'userid' => $this->userid
    ]);
    $query->andFilterWhere(['like', 'username', $this->username])
      ->andFilterWhere(['like', 'name', $this->name])
      ->andFilterWhere(['like', 'phone', $this->phone])
      ->andFilterWhere(['like', 'address', $this->address])
      ->andFilterWhere(['like', 'content', $this->content])
      ->andFilterWhere(['ll_hy_huangye_error.status' => $this->status])
      ->andFilterWhere(['ll_hy_huangye_error.categoryid' => $this->categoryid])
      ->andFilterWhere(['between', 'ctime', strtotime($this->startTime . '0:0:0'), strtotime($this->endTime . '23:59:59')])
      ->andFilterWhere(['like', 'error', $this->error]);
    if (intval($this->communityName)) {
      $query->andFilterWhere(['ll_hy_huangye_error.communityid' => intval($this->communityName)]);
    }
    $order = ' `ctime` DESC';
    $query->orderBy($order);
    return $dataProvider;
  }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* ajax請求小區
*
* @param $id
* @return string
*/
public function actionGetCommunityList($id)
{
    $option = '';
    $result = self::getCommunity($id);
    if ($result) {
      foreach ($result as $value) {
        $option .= '<option value="' . $value['id'] . '">' . $value['name'] . '</option>';
      }
    } else {
      $option .= '<option value="0">暫未開通可選擇的小區</option>';
    }
    echo $option;
}

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 2020年最新国产精品视频免费 | 无码观看AAAAAAAA片 | 羞羞视频动漫 | 美女黄金大片视频免费看 | 男同桌扒开女同桌胸罩喝奶 | 免费观看一级特黄三大片视频 | 日本在线亚州精品视频在线 | 色悠久久久久综合欧美99 | 久久亚洲午夜牛牛影视 | 国产亚洲精品久久yy5099 | 国内老司机精品视频在线播出 | 第一福利在线视频 | 精品免费久久久久久影院 | 俺来操 | 好大好硬好紧太深了受不了 | 交换余生在线播放免费 | 色综合久久六月婷婷中文字幕 | 色在线亚洲视频www 色欲麻豆国产福利精品 | 免费看欧美一级特黄a大片一 | sao虎影院桃红视频在线观看 | 星空无限传媒xk8129 | 国产一级在线观看 | 雪恋电影完整版免费观看 | 国产精品欧美亚洲韩国日本99 | 日本高清在线看免费观看 | 国产区小视频 | 亚洲 欧美 国产 在线观看 | 性绞姿始动作动态图 | jazz中国在线视频 | 范冰冰特黄xx大片 | 第一次不是你高清在线观看 | 特级淫片欧美高清视频蜜桃 | 描写细腻的高h肉 | 青青青国产在线观看 | 国产精品四虎在线观看免费 | 皇上好大好硬好涨好深好爽 | 欧美日韩一区二区三区免费 | 亚洲国产成人久久综合一区 | 国产成人成人一区二区 | 日出水了特别黄的视频 | 肉性天堂 |