Yii 1 開発日記 ----------- 検索機能とチェックボックスの実装
yii 1 を使用してバックグラウンド検索機能を実装すると、次のような効果が得られます:
1. モデル内:
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> search()</span><span style="color: #008080;"> 2</span> <span style="color: #000000;"> {</span><span style="color: #008080;"> 3</span> <span style="color: #008080;"> 4</span> <span style="color: #800080;">$criteria</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> CDbCriteria;</span><span style="color: #008080;"> 5</span> <span style="color: #008000;">//</span><span style="color: #008000;">独立高级搜索</span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>( <span style="color: #800080;">$_GET</span>['goods'<span style="color: #000000;">]) ) {</span><span style="color: #008080;"> 7</span> <span style="color: #008000;">//</span><span style="color: #008000;">商品货号</span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_GET</span>['goods']['goods_sn']) && <span style="color: #800080;">$_GET</span>['goods']['goods_sn'] != ""<span style="color: #000000;">)</span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> {</span><span style="color: #008080;">10</span> <span style="color: #800080;">$criteria</span>->compare('goods_sn',<span style="color: #800080;">$_GET</span>['goods']['goods_sn'], <span style="color: #0000ff;">true</span><span style="color: #000000;"> );</span><span style="color: #008080;">11</span> <span style="color: #000000;"> }</span><span style="color: #008080;">12</span> <span style="color: #008000;">//</span><span style="color: #008000;">商品名称</span><span style="color: #008080;">13</span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_GET</span>['goods']['goods_name']) && <span style="color: #800080;">$_GET</span>['goods']['goods_name'] != ""<span style="color: #000000;">)</span><span style="color: #008080;">14</span> <span style="color: #000000;"> {</span><span style="color: #008080;">15</span> <span style="color: #800080;">$criteria</span>->compare('goods_name',<span style="color: #800080;">$_GET</span>['goods']['goods_name'], <span style="color: #0000ff;">true</span><span style="color: #000000;">);</span><span style="color: #008080;">16</span> <span style="color: #000000;"> }</span><span style="color: #008080;">17</span> <span style="color: #008000;">//</span><span style="color: #008000;">商品分类</span><span style="color: #008080;">18</span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_GET</span>['goods']['cat_id']) && <span style="color: #800080;">$_GET</span>['goods']['cat_id'] != ""<span style="color: #000000;">)</span><span style="color: #008080;">19</span> <span style="color: #000000;"> {</span><span style="color: #008080;">20</span> <span style="color: #800080;">$criteria</span>->compare('cat_id',<span style="color: #800080;">$_GET</span>['goods']['cat_id'], <span style="color: #0000ff;">true</span><span style="color: #000000;">);</span><span style="color: #008080;">21</span> <span style="color: #000000;"> }</span><span style="color: #008080;">22</span> <span style="color: #008000;">//</span><span style="color: #008000;">是否上架</span><span style="color: #008080;">23</span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_GET</span>['goods']['is_on_sale']) && <span style="color: #800080;">$_GET</span>['goods']['is_on_sale'] != ""<span style="color: #000000;">)</span><span style="color: #008080;">24</span> <span style="color: #000000;"> {</span><span style="color: #008080;">25</span> <span style="color: #800080;">$criteria</span>->compare('is_on_sale',<span style="color: #800080;">$_GET</span>['goods']['is_on_sale'<span style="color: #000000;">]);</span><span style="color: #008080;">26</span> <span style="color: #000000;"> }</span><span style="color: #008080;">27</span> <span style="color: #008000;">//</span><span style="color: #008000;">是否精品</span><span style="color: #008080;">28</span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_GET</span>['goods']['is_best']) && <span style="color: #800080;">$_GET</span>['goods']['is_best'] != ""<span style="color: #000000;">)</span><span style="color: #008080;">29</span> <span style="color: #000000;"> {</span><span style="color: #008080;">30</span> <span style="color: #800080;">$criteria</span>->compare('is_best',<span style="color: #800080;">$_GET</span>['goods']['is_best'<span style="color: #000000;">]);</span><span style="color: #008080;">31</span> <span style="color: #000000;"> }</span><span style="color: #008080;">32</span> <span style="color: #008000;">//</span><span style="color: #008000;">是否新品</span><span style="color: #008080;">33</span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_GET</span>['goods']['is_new']) && <span style="color: #800080;">$_GET</span>['goods']['is_new'] != ""<span style="color: #000000;">)</span><span style="color: #008080;">34</span> <span style="color: #000000;"> {</span><span style="color: #008080;">35</span> <span style="color: #800080;">$criteria</span>->compare('is_new',<span style="color: #800080;">$_GET</span>['goods']['is_new'<span style="color: #000000;">]);</span><span style="color: #008080;">36</span> <span style="color: #000000;"> }</span><span style="color: #008080;">37</span> <span style="color: #008000;">//</span><span style="color: #008000;">是否热销</span><span style="color: #008080;">38</span> <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_GET</span>['goods']['is_hot']) && <span style="color: #800080;">$_GET</span>['goods']['is_hot'] != ""<span style="color: #000000;">)</span><span style="color: #008080;">39</span> <span style="color: #000000;"> {</span><span style="color: #008080;">40</span> <span style="color: #800080;">$criteria</span>->compare('is_hot',<span style="color: #800080;">$_GET</span>['goods']['is_hot'<span style="color: #000000;">]);</span><span style="color: #008080;">41</span> <span style="color: #000000;"> }</span><span style="color: #008080;">42</span> <span style="color: #008080;">43</span> <span style="color: #000000;"> }</span><span style="color: #008080;">44</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> CActiveDataProvider(<span style="color: #800080;">$this</span>, <span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;">45</span> 'criteria'=><span style="color: #800080;">$criteria</span><span style="color: #008080;">46</span> <span style="color: #000000;"> ));</span><span style="color: #008080;">47</span> }
2. コントローラー内:
<span style="color: #800080;">$model</span>=<span style="color: #0000ff;">new</span> B2cGoods('search');
モデル内の手段 モデル内の検索を検索として有効にします。
3. ビュー内:
<div <span style="color: #0000ff;">class</span>="well"> <div <span style="color: #0000ff;">class</span>="search-box"> <form <span style="color: #0000ff;">class</span>="form-inline" method="get" action=""><br> <span style="color: #0000ff;">//指定form表单提交的页面,很重要</span> <input type='hidden' name='r' value='B2CShop/b2cGoods/goodsList/id/<?php echo $id ?>'> <div <span style="color: #0000ff;">class</span>="form-group"> <<span style="color: #000000;">input name</span>="goods[goods_sn]"<span style="color: #000000;"> type</span>="text" <span style="color: #0000ff;">class</span>="form-control"<span style="color: #000000;"> style</span>="width:140px;"<span style="color: #000000;"> placeholder </span>= "商品货号"<span style="color: #000000;"> value</span>=<?php <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_GET</span>['goods']['goods_sn'] ; ?> > </div>&<span style="color: #000000;">nbsp; </span><div <span style="color: #0000ff;">class</span>="form-group"> <<span style="color: #000000;">input name</span>="goods[goods_name]"<span style="color: #000000;"> type</span>="text" <span style="color: #0000ff;">class</span>="form-control"<span style="color: #000000;"> style</span>="width:140px;"<span style="color: #000000;"> placeholder </span>= "商品名称"<span style="color: #000000;"> value</span>=<?php <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_GET</span>['goods']['goods_name'] ; ?> > </div> &<span style="color: #000000;">nbsp; </span><div <span style="color: #0000ff;">class</span>="form-group"> <?php <span style="color: #0000ff;">echo</span> CHtml::dropDownList( "goods[cat_id]" , <span style="color: #800080;">$_GET</span>['goods']['cat_id'] ,<span style="color: #000000;"> B2cCategory</span>::listData( <span style="color: #800080;">$id</span> ) , <span style="color: #0000ff;">array</span>( "class"=>"form-control" , 'empty'=>'请选择类型...', 'encode' => <span style="color: #0000ff;">false</span>, "style"=>"width:140px") ); ?> </div> &<span style="color: #000000;">nbsp; </span><div <span style="color: #0000ff;">class</span>="checkbox"> <label style="font-size: 16px"><span style="color: #000000;">上架 </span><<span style="color: #000000;">input type</span>="checkbox"<span style="color: #000000;"> name</span>="goods[is_on_sale]"<span style="color: #000000;"> style</span>="width: 24px;"<span style="color: #000000;"> value</span>="1"<br /> <span style="color: #0000ff;">//实现checkbox,刷新页面保持原状态</span> <?php <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_GET</span>['goods']['is_on_sale']?'checked="checked"':'' ?> > </label> </div> &<span style="color: #000000;">nbsp; </span><div <span style="color: #0000ff;">class</span>="checkbox"> <label style="font-size: 16px"><span style="color: #000000;">精品 </span><<span style="color: #000000;">input type</span>="checkbox"<span style="color: #000000;"> name</span>="goods[is_best]"<span style="color: #000000;"> style</span>="width: 24px;"<span style="color: #000000;"> value</span>="1" <?php <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_GET</span>['goods']['is_best']?'checked="checked"':'' ?> > </label> </div> &<span style="color: #000000;">nbsp; </span><div <span style="color: #0000ff;">class</span>="checkbox"> <label style="font-size: 16px"><span style="color: #000000;">新品 </span><<span style="color: #000000;">input type</span>="checkbox"<span style="color: #000000;"> name</span>="goods[is_new]"<span style="color: #000000;"> style</span>="width: 24px;"<span style="color: #000000;"> value</span>="1" <?php <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_GET</span>['goods']['is_new']?'checked="checked"':'' ?> > </label> </div> &<span style="color: #000000;">nbsp; </span><div <span style="color: #0000ff;">class</span>="checkbox"> <label style="font-size: 16px"><span style="color: #000000;">热销 </span><<span style="color: #000000;">input type</span>="checkbox"<span style="color: #000000;"> name</span>="goods[is_hot]"<span style="color: #000000;"> style</span>="width: 24px;"<span style="color: #000000;"> value</span>="1" <?php <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_GET</span>['goods']['is_hot']?'checked="checked"':'' ?> > </label> </div> <button type="submit" <span style="color: #0000ff;">class</span>="btn btn-default"><span <span style="color: #0000ff;">class</span>="glyphicon glyphicon-search"></span> 搜 索</button> </form> </div></div>
ここで注意すべき点は、チェックボックスを実装し、元の状態を維持することです。echo $_GET['goods']['is_hot']?'checked="checked"':'' ?>、つまり、PHP を使用して値があるかどうかを判断します。