欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Yii2使用dropdownlist實(shí)現(xiàn)地區(qū)三級(jí)聯(lián)動(dòng)功能的方法

 更新時(shí)間:2016年07月18日 12:01:25   作者:zhgxun  
這篇文章主要介紹了Yii2使用dropdownlist實(shí)現(xiàn)地區(qū)三級(jí)聯(lián)動(dòng)功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了dropdownlist下拉列表實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)調(diào)用的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Yii2使用dropdownlist實(shí)現(xiàn)地區(qū)三級(jí)聯(lián)動(dòng)功能的方法。分享給大家供大家參考,具體如下:

視圖部分:

<?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' => '請(qǐng)選擇城市'])->label('請(qǐng)選擇城市', ['class' => 'sr-only']) ?>
    <?= $form->field($model, 'areaName', ['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($areas, 'id', 'name'), ['prompt' => '請(qǐng)選擇區(qū)縣'])->label('請(qǐng)選擇區(qū)縣', ['class' => 'sr-only']) ?>
    <?= $form->field($model, 'communityName', ['options' => ['class' => 'form-group col-lg-2']])->dropDownList(ArrayHelper::map($communities, 'id', 'name'), ['prompt' => '請(qǐng)選擇小區(qū)'])->label('請(qǐng)選擇小區(qū)', ['class' => 'sr-only']) ?>
    <div class="col-lg-2 col-lg-offset-1">
      <input class="form-control" id="keyword" placeholder="請(qǐng)輸入小區(qū)名" 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\">請(qǐng)選擇區(qū)縣</option>");
    $("#itemsearch-communityname").html("<option value=\"0\">請(qǐng)選擇小區(qū)</option>");
    if (cityid > 0) {
      getArea(cityid);
    }
  });
  //區(qū)地址改變
  $("#itemsearch-areaname").change(function() {
    //區(qū)id值
    var areaid = $(this).val();
    $("#itemsearch-communityname").html("<option value=\"0\">請(qǐng)選擇小區(qū)</option>");
    if (areaid > 0) {
      getCommunity(areaid);
    }
  });
  //獲取市下面的區(qū)列表
  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);
      }
    });
  }
  //獲取區(qū)下面的小區(qū)列表
  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);
      }
    });
  }
  //搜索小區(qū)
  $("#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請(qǐng)求,當(dāng)然php中需要直接組合成<option value=""></option>這樣的結(jié)構(gòu)直接用,$form->field($model, $var)中的變量數(shù)據(jù)表中不一定有,得在模型中自己定義,并設(shè)置安全字段,而且搜索模型也可能需要修改成自己需要的樣子,模型可能要這樣:

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()連表,后來被我全部轉(zhuǎn)化為單表了,多表實(shí)在是慢,能轉(zhuǎn)化成單表就用單表吧:

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 刪除狀態(tài)
    $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;
  }
}

控制器中寫比較簡(jiǎn)單一點(diǎn),直接調(diào)用就行了:

/**
* ajax請(qǐng)求小區(qū)
*
* @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">暫未開通可選擇的小區(qū)</option>';
    }
    echo $option;
}

更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • thinkPHP簡(jiǎn)單遍歷數(shù)組方法分析

    thinkPHP簡(jiǎn)單遍歷數(shù)組方法分析

    這篇文章主要介紹了thinkPHP簡(jiǎn)單遍歷數(shù)組方法,結(jié)合實(shí)例形式分析了thinkPHP使用volist標(biāo)簽遍歷數(shù)組的技巧,并對(duì)比分析了織夢(mèng)cms的arclist標(biāo)簽加強(qiáng)對(duì)thinkPHP數(shù)組遍歷的理解,需要的朋友可以參考下
    2016-05-05
  • PHP實(shí)現(xiàn)兩種排課方式

    PHP實(shí)現(xiàn)兩種排課方式

    這篇文章主要介紹了PHP實(shí)現(xiàn)兩種排課方式,第一種排課方式比較好理解,開始時(shí)間和結(jié)束時(shí)間,第二種比較復(fù)雜一些,有開始上課時(shí)間和結(jié)束上課時(shí)間和第幾周,具體實(shí)現(xiàn)代碼參考下本文
    2021-06-06
  • 詳解php curl帶有csrf-token驗(yàn)證模擬提交方法

    詳解php curl帶有csrf-token驗(yàn)證模擬提交方法

    這篇文章主要介紹了詳解php curl帶有csrf-token驗(yàn)證模擬提交方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • 在Debian系統(tǒng)下配置LNMP的教程

    在Debian系統(tǒng)下配置LNMP的教程

    這篇文章主要介紹了在Debian系統(tǒng)下配置LNMP的教程,較之常見的LAMP,即把Apache換成高性能的Nginx服務(wù)器,需要的朋友可以參考下
    2015-07-07
  • 19個(gè)Android常用工具類匯總

    19個(gè)Android常用工具類匯總

    這篇文章主要介紹了19個(gè)Android常用工具類匯總,需要的朋友可以參考下
    2014-12-12
  • php刪除數(shù)組指定元素實(shí)現(xiàn)代碼

    php刪除數(shù)組指定元素實(shí)現(xiàn)代碼

    這篇文章主要介紹了php刪除數(shù)組指定元素實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 深入php-fpm的兩種進(jìn)程管理模式詳解

    深入php-fpm的兩種進(jìn)程管理模式詳解

    本篇文章是對(duì)php-fpm的兩種進(jìn)程管理模式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP中如何使用session實(shí)現(xiàn)保存用戶登錄信息

    PHP中如何使用session實(shí)現(xiàn)保存用戶登錄信息

    這篇文章主要給大家介紹在php中是如何使用session實(shí)現(xiàn)保存用戶登錄信息的,涉及到php session 用戶登錄等一些知識(shí)點(diǎn),使用session保存用戶登錄信息要比cookie安全很多。感興趣的朋友一起學(xué)習(xí)吧
    2015-10-10
  • Joomla簡(jiǎn)單判斷用戶是否登錄的方法

    Joomla簡(jiǎn)單判斷用戶是否登錄的方法

    這篇文章主要介紹了Joomla簡(jiǎn)單判斷用戶是否登錄的方法,涉及Joomla基于服務(wù)器端變量結(jié)合Joomla文件進(jìn)行判斷與跳轉(zhuǎn)的相關(guān)技巧,需要的朋友可以參考下
    2016-05-05
  • PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(十四)

    PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(十四)

    這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀的資源更新頁面部分,需要的朋友可以參考下
    2014-06-06

最新評(píng)論