小程序模拟大巴车在线选择座位

首先上demo地址:https://github.com/AloneYan/WeChat-SeatSelection

2020/11/02代码迁移至gitee:https://gitee.com/han96/WeChat-SeatSelection.git

最近公司接了新项目 就有新需求啦

有一个功能是要求在线选座,像是买火车票买电影票哪种

此教程由花花同学友情赞助(为啥酱紫说 是因为花花写的demo 我写教程~)

一个很丑的demo如下图所示!!(为啥这么丑-。-不应该呀)

下面就开始实现它了
具体部分的实现请看图片中的注释部分,或者直接联系me

<wxs src="./util.wxs" module="tools" /> <!-- 自定义过滤器 -->
<view class='box'>
  <view class='content'>红色:已选择 /  绿色:正在选择</view>
  <view wx:for="{{seats}}"  wx:for-item="seat" wx:key="index"><!-- 两层循环 -->
    <view wx:for="{{seat}}" 
          wx:key='index'
          wx:for-index="idx" 
          wx:for-item="item" 
          class="{{( tools.fn(myselectSite, item.num).indexOf?'active':item.userInfo!=null?'used':'seat')}}" 
          style="{{idx%2!==0&&'margin-right:40rpx'}}"
          bindtap='selctedSite' 
          data-num='{{item.num}}'
          data-used='{{item.userInfo==null}}'><!-- 根据不同的判断定义class,绑定点击事件,传输数据 -->
     {{item.num}}
    </view>
  </view>
  <view class='content'>您选择了:{{myselectSite}}</view><!-- 展示选择的座位号 -->
</view>

js部分:

Page({
  data: {
    //模拟假数据
    seats: [
      { num: 1, userInfo: { uid: 20190101, name: '阎阎' } },
      { num: 2, userInfo: { uid: 20190101, name: '阎阎' } },
      { num: 3, userInfo: { uid: 20190102, name: '花花' } },
      { num: 4, userInfo:null},
      { num: 5, userInfo: { uid: 20190103, name: '仙球' } },
      { num: 6, userInfo: { uid: 20190104, name: '亮亮' } },
      { num: 7, userInfo: null },
      { num: 8, userInfo: null },
      { num: 9, userInfo: null },
      { num: 10, userInfo: null },
      { num: 11, userInfo: null },
      { num: 12, userInfo: { uid: 20190102, name: '花花' } },
      { num: 13, userInfo: null },
      { num: 14, userInfo: null },
      { num: 15, userInfo: null },
      { num: 16, userInfo: null }
    ],
    myselectSite:[]
  },

  //重组数据-分为四个一组
  reSetData(dataList,num) {
    let arr = [];
    let len = dataList.length;
    for (let i = 0; i < len; i += num) {
      arr.push(dataList.slice(i, i + num));
    }
    return arr;
  },

  //移除选择的座位
  remove (arr,val) {
    var index = arr.indexOf(val);
    if (index > -1) {
      arr.splice(index, 1);
    }
  },

  //选择座位
  selctedSite(e){
    let { myselectSite } = this.data;
    const param = e.target.dataset;
    const { num, used } = param;
    if (!used) { return  false};
    if (myselectSite.indexOf(num)===-1){
      myselectSite.push(num)
    }else{
      this.remove(myselectSite,num)
    }
    this.setData({ myselectSite })
  },

  onLoad: function (options) {
    let { seats } = this.data;
    let temp = this.reSetData(seats,4)
    this.setData({ seats: temp })
  }
})

util.wxs文件——过滤器  (这个文件就相当与在前台试用的js  之前我有写过这种过滤器的教程哦)

function fn(arr, arg) {
  var result = {
    indexOf: false,
    toString: ''
  }
  result.indexOf = arr.indexOf(arg) > -1;
  result.toString = arr.join(","); return result;
}
module.exports.fn = fn;

css文件(这个不重要 毕竟我写的样式超级难看)

.box{
  width: 240px;
  margin:20rpx auto; 
  padding-left:-40rpx;
}


/* 这里还是有点重要的 */
.seat,.used,.active{
  display: inline-block;
  width: 70rpx;
  margin:10rpx;
  text-align: center;
}
.seat{
  background-color: gainsboro;
}
.used{
  background-color: red;
}
.active{
  background-color: green;
}


.content{
  font-size: 30rpx;
  padding:40rpx 0;
}

当然这只是一个简单的实现思路
现实的大客车还有许多例外 比如说最后一排一般会有5个座位,还有其他限制条件
这就需要在从需求出发 不断完善了~

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
下面是一个简单的大巴管理系统的JSP+MySQL实现: 1. 创建数据库 首先创建一个名为“bus”的数据库,包含以下两个表: - bus_info:存储大巴信息,包括牌号、座位数、型号等字段。 - driver_info:存储司机信息,包括司机姓名、电话、驾照类型等字段。 2. 创建JSP页面 创建以下几个JSP页面: - index.jsp:系统首页,包含大巴和司机信息的查询和添加功能。 - bus.jsp:大巴信息页面,包含大巴信息的查询和添加功能。 - driver.jsp:司机信息页面,包含司机信息的查询和添加功能。 3. 连接数据库 在JSP页面中使用JDBC连接数据库,示例代码如下: ``` <% String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/bus"; String user = "root"; String password = "123456"; Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); if(conn != null) { out.println("成功连接到数据库!"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } %> ``` 4. 实现查询功能 在JSP页面中使用SQL语句查询数据库,示例代码如下: ``` <% Statement stmt = conn.createStatement(); String sql = "SELECT * FROM bus_info WHERE bus_id = '001'"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { out.println("牌号:" + rs.getString("bus_id")); out.println("座位数:" + rs.getInt("seat_num")); out.println("型号:" + rs.getString("model")); } %> ``` 5. 实现添加功能 在JSP页面中使用SQL语句添加数据到数据库,示例代码如下: ``` <% Statement stmt = conn.createStatement(); String sql = "INSERT INTO driver_info (name, phone, license_type) VALUES ('张三', '13812345678', 'C1')"; int result = stmt.executeUpdate(sql); if(result > 0) { out.println("成功添加司机信息!"); } %> ``` 以上就是一个简单的JSP+MySQL大巴管理系统的实现。当然,这只是一个示例,实际上你需要根据自己的需求来设计和完善系统。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

花贝是只猫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值