本文实例讲述了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([
'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> </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)中的变量数据表中不一定有,得在模型中自己定义,并设置安全字段,而且搜索模型也可能需要修改成自己需要的样子,模型可能要这样:
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()连表,后来被我全部转化为单表了,多表实在是慢,能转化成单表就用单表吧:
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;
}
}
控制器中写比较简单一点,直接调用就行了:
/**
* 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程序设计有所帮助。
更多yii2使用dropdownlist实现地区三级联动功能的方法。