uniapp 小程序 类型电话侧边索引

数组格式
newlist:[
{
brandId: 382
categoryId: 5
enFirstChar: "A"
id: 382
logo: "https://imgproduct.cehome.com//4/20150729/4LGZjwzGHif1438156941LczN4hBIbI4.png"
name: "奥邦"
},
{
brandId: 6
categoryId: 5
enFirstChar: "A"
id: 6
logo: "https://imgproduct.cehome.com/data/brandlogo/1280714691012.jpg"
name: "阿特拉斯中国"
}
]

util.js修改格式
function groupBy(array, key) {
  return array.reduce(function(groups, item) {
    var group = item[key];
    if (!groups[group]) {
      groups[group] = [];
    }
    groups[group].push(item);
    return groups;
  }, {});
}

转换效果为
newlist:[
A:[
{
brandId: 382
categoryId: 5
enFirstChar: "A"
id: 382
logo: "https://imgproduct.cehome.com//4/20150729/4LGZjwzGHif1438156941LczN4hBIbI4.png"
name: "奥邦"
},
{
brandId: 6
categoryId: 5
enFirstChar: "A"
id: 6
logo: "https://imgproduct.cehome.com/data/brandlogo/1280714691012.jpg"
name: "阿特拉斯中国"
}
]
]








wxml

  <scroll-view scroll-y="true" style="height:calc(100vh - 138px - {{CustomBar}})" scroll-into-view="{{scrollIntoView}}" scroll-top="0">
        <view class="brand-all">
          <view wx:for="{{newlist}}" wx:key="index" bindtap="scrollToGroup" data-letter="{{index}}">{{index}}</view>
        </view>
        <view wx:for="{{newlist}}" wx:key="index">
          <view id="{{'group-' + index}}" class="group">
            <view class="spec_1">{{index}}</view>
            <view wx:for="{{newlist[index]}}" wx:for-item="brand" wx:key="index" class="brand-l flex align-center">
              <view>
                <image src="{{brand.logo}}" />
              </view>
              <view>{{brand.name}}</view>
            </view>
          </view>
        </view>
      </scroll-view>


js

data:{
 scrollIntoView: ''
}

  saleBrand({category_id:e.currentTarget.dataset.id}).then(res =>{
        const groups = utils.groupBy(res, 'enFirstChar'); // 转格式
        this.setData({
          newlist:groups
        })
      })

点击事件
    scrollToGroup: function(e) {
      var letter = e.currentTarget.dataset.letter;
      this.setData({
        scrollIntoView: 'group-' + letter
      });
    },




const firstLetter = Object.keys(groups)[0];获取第一个元素的下标


.brand-l {
  height: 80rpx;
  padding-left: 20rpx;
  display: flex;
  align-items: center;
  border-bottom: 2rpx solid #00000019;
}

.brand-l image {
  width: 76rpx;
  height: 50rpx;
  margin-right: 100rpx;
}

.brand-l:nth-last-child(1) {
  border-bottom: none;
}

.spec_1 {
  background-color: #F6F6F6;
  height: 64rpx;
  padding-left: 20rpx;
  display: flex;
  align-items: center;
}

.brand-all {
  position: fixed;
  right: 20rpx;
  font-size: 24rpx;
  
}
.letter {
  width: 40rpx;
  height: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}
.letter1 {
  width: 40rpx;
  height: 40rpx;
  background: #DD2330;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50rpx;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

呱嗨喵

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

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

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

打赏作者

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

抵扣说明:

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

余额充值