首页 > php教程 > PHP开发 > 正文

Yii2使用dropdownlist实现地区三级联动功能的方法

高洛峰
发布: 2016-12-23 17:48:08
原创
1532 人浏览过

本文实例讲述了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
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!