uniapp搜索和分页的实现

1.在项目中导入uni-search-bar插件

2.直接使用

<template>
  <view class="active">
    <!-- 搜索框 -->
    <view class="active__search">
      <uni-search-bar class="search" placeholder="搜索"  cancelButton="none" bgColor="#EEEEEE" @input="search" />
    </view>
    <view class="recommend">
      <!-- 标题 -->
      <!-- <view class="recommend__title">{{title}}</view> -->
      <!-- 内容 -->
      <view class="recommend__content" v-for="(item, index) in recommendList" :key="index" @click="lookInfo(item.Id)">
        <view class="flex-space-between">
          <view class="recommend__content__title">{{item.Name}}</view>
          <view class="recommend__content__look" >{{item.Type}}</view>
        </view>
        <view class="time flex-space-between">
          <view>{{dateFormat(item.PublishDate)}}</view>
          <view>{{item.Office}}</view>
        </view>
        <!-- <view v-if="index === recommendList.length - 1" style="margin: 45rpx 10rpx 0;"></view>
        <u-divider v-else style="margin: 0 10rpx 0;"></u-divider> -->
      </view>
    </view>
    <!-- 没有更多 -->
    <view class="hint" v-if="showmore">
    	没有更多..
    </view>
    <!-- 暂无 -->
    <view class="empty" v-if="recommendList.length <= 0">
    	<image src="../static/sk/null.png" style="width: 414rpx; height: 254rpx;"></image>
    	<view>
    		暂无更多
    	</view>
    </view>
  </view>
</template>

<script>
  import apiutl from '@/common/operate.js'
  export default {
    data() {
      return {
        imghosts: apiutl.imghost,
        pageindex: 1, //页号
        pagesize: 10, //分页大小 后台默认10条,不用更改
        total: 0, //数据总条数
        isloading: false, // 是否正在请求数据
        showmore: false, // 没有更多
        title:'',
        recommendList:[],
        keyword:'',
        id:'',
      }
    },
    onLoad(options) {
      this.id = options.id
      this.search(this.keyword)
    },
    methods: {
      //前往详情
      lookInfo(e){
        uni.navigateTo({
          url: '/pages/control/lawsDetails?id='+e+'&flag='+this.id
        })
      },
      search(keyword) {
        // uni.showToast({
        // 	title: '搜索:' + keyword,
        //   icon: 'none'
        // })
        //打开节流阀
        this.isloading = true;
       
        if(this.id==1){
           this.recommendList = [],
          this.$api.GetSafeLaw({
            SeachKey:keyword,
            PageIndex:this.pageindex,//分页
          }).then(res => {
          	if (res) {
              this.recommendList=[...this.recommendList,...res.data];
              this.total = res.count;
              return;
          	}
          });
        }else if(this.id==2){
           this.recommendList = [],
          this.$api.GetSafeNationalStandards({
            SeachKey:keyword,
            PageIndex:this.pageindex,//分页
          }).then(res => {
          	if (res) {
              this.recommendList=[...this.recommendList,...res.data];
              this.total = res.count;
              return;
          	}
          });
        }else if(this.id==3){
           this.recommendList = [],
          this.$api.GetSafeNationalStandard({
            SeachKey:keyword,
            PageIndex:this.pageindex,//分页
          }).then(res => {
          	if (res) {
              this.recommendList=[...this.recommendList,...res.data];
              this.total = res.count;
              return;
          	}
          });
        }
        
        //关闭节流阀
        this.isloading = false;
        
      },
     
      // 触底的事件
      onReachBottom() {
        console.log("触底的事件")
      	// 判断是否还有下一页数据
      	if (this.pageindex * this.pagesize >= this.total){
      		if(this.total > 0){
      			this.showmore = true;
      			uni.showToast({
      				title: '没有更多数据了',
      				icon: 'none'
      			});
      		}
      		return;
      	}
      	// 判断是否正在请求其它数据,如果是,则不发起额外的请求
      	if (this.isloading) {
      		return;
      	}
      	// 让页码值自增 +1
      	this.pageindex += 1
      	// 重新获取列表数据
      	this.search(this.keyword)
      },
      //日期格式化处理
      dateFormat(data) {
        if (data) {
          let date = new Date(data.replace(/-/g, '/'));
          let year = date.getFullYear();
          let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
          let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
          return year + "-" + month + "-" + day;
        }
      },
    },
    
  }
</script>

<style lang="scss">
  .active {
    background: #FAFAFA;
    // padding-top: 30rpx;
    min-height: 100vh;
    overflow-x: hidden;
    &__search{
      background: #FFFFFF;
      .search {
        margin: 0 30rpx;
      }
    }
    .recommend{
      margin: 28rpx 0;
      border-radius: 8px 8px 8px 8px;
      opacity: 1;

      &__content{
        margin-top: 30rpx;
        padding: 30rpx 24rpx 10rpx;
        background: #FFFFFF;
        &__title{
           margin-bottom: 40rpx;
           width: 480rpx;
           overflow: hidden; /*隐藏*/
           white-space: nowrap;  /*不换行*/
           text-overflow: ellipsis;  /* 超出部分省略号 */
        }
        &__look{
          background: #52C46D;
          border-radius: 0px 0px 0px 20px;
          padding: 12rpx 36rpx;
          font-size: 26rpx;
          font-weight: 400;
          color: #FFFFFF;
          margin-right: -30rpx;
          margin-top: -80rpx;
        }
        .time{
          font-size: 26rpx;
          font-weight: 400;
          color: #999999;
          margin-bottom: 20rpx;
        }
      }
     
    }
    .hint {
    	padding: 20rpx 0;
    	line-height: 120rpx;
    	text-align: center;
    	font-size: 24rpx;
    	color: #999;
    }
    
    .empty {
    	padding: 250rpx 0;
    	// width: 690rpx;
    	text-align: center;
    	font-size: 32rpx;
    	color: #999;
    }
  }
</style>

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
uniapp实现分页,可以使用scroll-view组件结合分页器组件来实现。具体实现步骤如下: 1. 在scroll-view组件上设置scroll-y属性为true,表示开启纵向滚动。 2. 在scroll-view组件内部添加列表内容,可以使用v-for指令动态生成列表项。 3. 在分页器组件上设置总页数和当前页数,可以使用计算属性动态计算总页数。 4. 在分页器组件上监听页码改变事件,根据页码计算需要滚动的距离,然后通过调用scroll-view组件的scrollTo方法实现滚动。 下面是一个示例代码: ```html <template> <view> <scroll-view class="scroll-view" scroll-y :scroll-with-animation="true"> <view v-for="(item, index) in list" :key="index" class="item">{{ item }}</view> </scroll-view> <paginator :total="totalPage" :current="currentPage" @change="onPageChange"></paginator> </view> </template> <script> import Paginator from '@/components/Paginator.vue'; export default { components: { Paginator, }, data() { return { list: [], // 列表数据源 pageSize: 10, // 每页显示数量 currentPage: 1, // 当前页码 }; }, computed: { totalPage() { // 计算总页数 return Math.ceil(this.list.length / this.pageSize); }, }, methods: { onPageChange(page) { // 页码改变事件 this.currentPage = page; const scrollTop = (page - 1) * this.pageSize * 50; // 计算需要滚动的距离 uni.pageScrollTo({ scrollTop, duration: 300, }); }, }, }; </script> ``` 需要注意的是,这里的列表项高度为50rpx,需要根据实际情况进行调整。同时,需要在scroll-view组件上设置scroll-with-animation属性为true,表示开启滚动动画。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值