入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

yii 2.0 ajax 的即点即改 多条件搜索

创建时间:2016-09-22 投稿人: 浏览次数:132
<tr>  
<td>商品</td>
</tr>
<tr>
  <td><span class="click" id="<?php echo $v["id"]?>"><?php echo $v["goods"]?></span></td>
</tr>
jquery
  $(function(){
        $(document).on("click",".click",function(){
           var id=$(this).attr("id");//获取id
            var aa=$(this).html();//获取html值
          //  alert(aa)
        $(this).parent().html("<input type="text" value="+aa+" class="one">");//通过当前获取父节点的值
            $(".one").focus();//聚焦事件
            $(".one").blur(function(){//鼠标离开事件
                _this=$(this);
               var  bb=$(this).val();//获取新值
            //    alert(bb);
                $.post("?r=lianxi/pi",{id:id,bb:bb},function(msg){
                   // alert(msg)
                   if(msg==1){
                     _this.parent().html("<span class="click" id="+id+">"+bb+"</span>");
                   }else{
                      alert("失败");
                   }
            })

            })
        })
    })
控制器
    public  function actionPi(){
        $id=yii::$app->request->post("id");
        $bb=yii::$app->request->post("bb");
        $result=shang::find()->where(["id"=>$id])->one();
     //   print_r($result);die;
        $result->goods=$bb;
        $re=$result->save();
      //  print_r($re);die;
        if($re){
            return  1;
        }else{
            echo 0;
        }
 }
yii2.0多条件搜索
<tr>
    <td colspan="2">名称:<input type="text" id="search1"></td>
    <td colspan="2">排序:<input type="text" id="search2"> </td>
    <td><input type="button" value="搜一搜" id="btn"></td>
</tr>
 $(function(){
        $("#btn").click(function(){
            var search1 = $("#search1").val();
            var search2 = $("#search2").val();
            //发送AJAX请求
            $.ajax({
                url:"?r=lianxi/search",
                data:{search1:search1,search2:search2},
                type:"POST",
                success:function(msg) {

                    msg= eval(msg);
                    var str="<tr>" +
                        "<td>编号</td>"+
                        "<td>名称</td>"+
                        "<td>货号</td>"+
                        "<td>排序</td>"+
                        "<td>库存</td>"+
                        "<td>图片</td>"+
                        "<td>上架</td>"+
                        "<td>热销</td>"+
                        "</tr> ";
                    for(var i=0;i<msg.length;i++){
                        str+="<tr>" +
                        "<td>"+msg[i].id+"</td>" +
                        "<td>"+msg[i].goods_name+"</td>" +
                        "<td>"+msg[i].money+"</td>" +
                        "<td>"+msg[i].paixu+"</td>" +
                        "<td>"+msg[i].kucun+"</td>" +
                        "<td><img src="+msg[i].filename+" height="50" width="50"/>";
                        if(msg[i].shangjia==1){
                            str+="<td>√<td>"
                        }else{
                            str+="<td>错<td>"
                        }
                        if(msg[i].rexiao==1){
                            str+="<td>√<td>"
                        }else{
                            str+="<td>错<td>"
                        }
                        str+="</tr>";
                    }
                    $(".tab").html(str);
              }
            })
        })
    })
控制器
    $search1=yii::$app->request->post("search1");
    $search2=yii::$app->request->post("search2");
    //print_r($search2);die;
    $where=1;
    if(!empty($search1)){
        $where .= " and goods_name like "%$search1%"";
    }
    if(!empty($search2)){
        $where .= " and paixu like "%$search2%"";
    }
    $blogInfo = goods::find()->where($where)->asArray()->all();
    //搜索后关键字标红
    foreach($blogInfo as $key => $value) {
        $blogInfo[$key]["goods_name"] = str_replace($search1, "<font color="red">$search1</font>", $value["goods_name"]);
        $blogInfo[$key]["paixu"] = str_replace($search2, "<font color="red">$search2</font>", $value["paixu"]);
    }
    echo json_encode($blogInfo);

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。