搜索
首页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中需要直接组合成deb802d11e8b0cc341f68c280f65dae74afa15d3069109ac30911f04c56f3338这样的结构直接用,$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脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。