Home>

View section:

<?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>
  <div>
    <?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" =>"Please select a city"])->label ("Please select a city", ["class" =>"sr-only"])?>
    <?= $form->field ($model, "areaname", ["options"=>>["class" =>"form-group col-lg-2"]])->;dropdownlist (arrayhelper ::map ($areas, "id", "name"), ["prompt" =>"Please select a county"])->label ("Please select a county", ["class "=>" Sr-only "])?>
    ?= $form->field ($model, "communityname", ["options"=>>["class" =>"form-group col-lg-2"]])->;dropdownlist (arrayhelper ::map ($communities, "id", "name"), ["prompt" =>"Please select a cell"])->((Please select a cell), ["class" =>"sr-only"])?>
    <div>
      <input placeholder="Please enter the cell name" value="" />
    </div>
    <div>
      <button type="button">Search</button>
    </div>
    <p></p>
    <div>
      <?= html ::submitbutton ("Search", ["class" =>"btn btn-primary"])?>
    </div>
    <?php activeform ::end ();?>
  </div>
</div>
<p></p>
<?php
$this->registerjs ("
  //city address changed
  $("#itemsearch-cityname"). change (function () {
    //city id value
    var cityid=$(this) .val ();
    $("#itemsearch-areaname"). html ("<option value=\" 0 \ ">Please select a district</option>");
    $("#itemsearch-communityname"). html ("<option value=\" 0 \ ">Please select a community</option>");
    if (cityid>0) {
      getarea (cityid);
    }
  });
  //District address changed
  $("#itemsearch-areaname"). change (function () {
    //zone id value
    var areaid=$(this) .val ();
    $("#itemsearch-communityname"). html ("<option value=\" 0 \ ">Please select a community</option>");
    if (areaid>0) {
      getcommunity (areaid);
    }
  });
  //Get the list of districts below the city
  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);
      }
    });
  }
  //Get the list of cells below the area
  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
  $("#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);
        }
      });
    }
  });
");
?>

Model part:

It is our commonly used ajax request. Of course, it needs to be directly combined intoin php.The variable data table in $form->field ($model, $var) may not be available,You have to define it yourself in the model,And set security fields,And the search model may also need to be modified to look like it.The model might look like this:

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");
  }
}

Before it was multi-table,Need to use jjoinwith () to join the tables, and later all of them were converted into single tables,Multi-table is really slow,If it can be converted into a single table, use a single table:

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 delete status
    $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;
  }
}

Writing in the controller is relatively simple,Just call it directly:

/**
* ajax request cell
*
* @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 ">No selectable cell has been opened yet</option>";
    }
    echo $option;
}
php
  • Previous Yii2 framework dropDownList drop-down menu usage example analysis
  • Next Java implementation of Weibo background login to send Weibo