Yii 2.0 AJAX搜索,分页,关键字变红

//控制器方法

 

    public function actionStrylist(){
        $username = \Yii::$app->request->post('username');  //接收搜索的数
        $where=1;
        if(!empty($username)){
            $where = " b_name like '%$username%'";
        }
        $page = \Yii::$app->request->post('pages');
        //判断当前页码是否存在
        $pages = isset($page) ? $page : 1 ;
        //计算总条数
        $count = Brand::find()->count();
        //设置每一页显示的条数
        $pageSize = 5 ;
        //计算总页数
        $pageSum = ceil($count/$pageSize);
        //计算偏移量
        $offset = ($pages - 1)*$pageSize;
        //计算上一页 下一页
        $last = $pages<=1 ? 1 : $pages-1 ;
        $next = $pages>=$pageSum ? $pageSum : $pages+1 ;
        //拼接A链接
        $str = '';
        $str .= "<a href='javascript:void(0);' >共"."$count"."项目&nbsp;".$pageSum."页</a>&nbsp;&nbsp;";
        $str .= "<a href='javascript:void(0);' onclick='page(1)'>首页</a>&nbsp;&nbsp;";
        $str .= "<a href='javascript:void(0);' onclick='page($last)'>上一页</a>&nbsp;&nbsp;";
        $str .= "<a href='javascript:void(0);' onclick='page($next)'>下一页</a>&nbsp;&nbsp;";
        $str .= "<a href='javascript:void(0);' onclick='page($pageSum)'>尾页</a>&nbsp;&nbsp;";
        $str .= "<a href='javascript:void(0);' >第"."$pages"."页</a>&nbsp;&nbsp;";
        //查询分页后的数据信息
        $sql = "SELECT a.b_id,a.b_name,a.b_company,a.b_addtime,b.c_name,c.a_area,d.d_name,e.shop_name,f.t_price FROM brand a,cate b,area c,dustry d,shop_type e,tment_scope f where a.c_id=b.c_id and a.r_id=c.a_id and a.i_id=d.d_id and a.t_id=e.s_id and a.p_id=f.t_id and $where limit $offset,$pageSize";
        $userInfo = Brand::findBySql($sql)->asArray()->all();
       //搜索后关键字标红
       foreach($userInfo as $key => $value){
          $userInfo[$key]['b_name'] = str_replace($username,"<font color='red'>$username</font>",$value['b_name']);
       }
        return $this->render('show',['arr'=>$userInfo,'page'=>$str,'username'=>$username]);
    }

show视图页

 

 

<div class="public-content-header">
     <input type="text" class="page-input" id="keyword" value="<?php echo $username?>">
      <button onclick="page()">搜索</button>
</div>

 

echo分页变量

 

<div class="page">
    <?php echo $page;?>
</div>

script 搜索分页方法   //加载jQuery

 

 

function page(page){
    //获取搜索的数据
    var username = $("#keyword").val();
    //发送AJAX请求  
    $.ajax({
        url:"index.php?r=industry/strylist",
        data:{pages:page,username:username},
        type:"POST",
        success:function(msg){
            $("#body").html(msg);
        }
    });
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑漆#000000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值