微信小程序中:使用三目运算设置样式

样式:年龄小于30,颜色为红色,反之为蓝色

wxml

<!-- 表格 -->
<view style="padding:5px;margin-bottom:15%;">
  <view class="table">
    <view class="table-tr">
      <view class="table-th1">name</view>
      <view class="table-th2">age</view>
      <view class="table-th3">occupation</view>
    </view>
    <view class="table-tr" wx:for="{{test}}" wx:key="unique">
      <view class="table-td1">{{item.name}}</view>
      <view class="table-td2 {{item.age < 30 ? 'style1' : 'style2' }}">{{item.age}}</view>
      <view class="table-td3">{{item.occupation}}</view>
    </view>
  </view>
</view>

wxss

/*设置不同的样式*/
.style1{
  color:red;
}
.style2{
  color:blue;
}
/* 表格样式 */
.table {
  display: table;
  width: 100%;
  border-collapse: collapse;
  box-sizing: border-box;
}
.table-tr {
  display: table-row;
}
.table-th1 {
  width:10%;
  display: table-cell;
  font-weight: bold;
  border: 1rpx solid gray;
  background-color:#51aad6;
  text-align: center;
  vertical-align: middle;
  padding: 5px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
}
.table-th2 {
  width:30%;
  display: table-cell;
  font-weight: bold;
  border: 1rpx solid gray;
  background-color:#51aad6;
  text-align: center;
  vertical-align: middle;
  padding: 5px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
}
.table-th3 {
  width:20%;
  display: table-cell;
  font-weight: bold;
  border: 1rpx solid gray;
  background-color:#51aad6;
  text-align: center;
  vertical-align: middle;
  padding: 5px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
}
.table-td1{
  width:10%;
  display: table-cell;
  border: 1rpx solid gray;
  text-align: center;
  vertical-align: middle;
  padding: 5px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
}
.table-td2 {
  width:20%;
  display: table-cell;
  border: 1rpx solid gray;
  text-align: center;
  vertical-align: middle;
  padding: 5px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
}
.table-td3 {
  width:20%;
  display: table-cell;
  border: 1rpx solid gray;
  text-align: center;
  vertical-align: middle;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
  /* padding: 5px 0; */
}

js

const app = getApp()
Page({
  data: {
    test : [
      {
        name: "John",
        age: 35,
        occupation: "developer"
      },
      {
        name: "Jane",
        age: 28,
        occupation: "designer"
      },
      {
        name: "Bob",
        age: 42,
        occupation: "manager"
      }
    ]
  },
})

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值