经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序实现选择内容显示对应内容
来源:jb51  时间:2022/7/19 19:04:47  对本文有异议

微信小程序中,经常可见选择地区或者商品分类或者其他的分类,选择后显示,选择后删除

先介绍一下主要功能:点击 ‘地区’ ,下面选择区域出现,点击 ‘选择地区’ 中的按钮,上面 ‘已选地区’ 显示选择的地区,点击 ‘x’ 已选的地区就取消,点击 “完成” 整个选择区域隐藏,只留下地区。

整体样式用的弹性布局,就不细说了。

wxml:

  1. <view class="container">
  2. ? <text bindtap="show" class="color">地区</text>
  3. ? <view class="choose" wx-if="{{a}}">
  4. ? ? <!-- 已选地区 -->
  5. ? ? <view class="chosed">
  6. ? ? ? <view class="chosedtop">
  7. ? ? ? ? <text>已选地区</text>
  8. ? ? ? ? <text bindtap="finish">完成</text>
  9. ? ? ? </view>
  10. ? ? ? <view class="view">
  11. ? ? ? ? <view class="position" wx:for='{{area}}' wx:key='index'>
  12. ? ? ? ? ? <button class="buttond">{{item}}</button>
  13. ? ? ? ? ? <image src='' bindtap="shut" data-index="{{index}}" data-name="{{item}}"></image>
  14. ? ? ? ? ? //这是按钮右上角的关闭图片
  15. ? ? ? ? </view>
  16. ? ? ? </view>
  17. ? ? </view>
  18. ? ? <!-- 选择地区 -->
  19. ? ? <view class="area">
  20. ? ? ? <text>选择地区</text>
  21. ? ? ? <view class="chos">
  22. ? ? ? ? <block wx:for='{{array}}' wx:key='index'>
  23. ? ? ? ? ? <button class="button {{tabindex == index?'active':''}}" data-index="{{index}}" data-name="{{item}}" bindtap="tabarea">{{item}}</button>
  24. ? ? ? ? </block>
  25. ? ? ? </view>
  26. ? ? </view>
  27. ? </view>
  28. </view>

 js:

  1. var chosedarea = [];
  2. var area = [];
  3. Page({?
  4. ? data: {
  5. ? ? a:false,
  6. ? ? tabindex:0,
  7. ? ? array: ["北京", '南京', '上海', '天津', '杭州', '云南', "北京", '南京', '上海', '天津', '杭州', '云南',"北京", '南京', '上海', '天津', '杭州', '云南'],
  8. ? },
  9. ? // 选地区
  10. ? ?tabarea:function(e){
  11. ? ? this.setData({
  12. ? ? ? tabindex:e.currentTarget.dataset.index
  13. ? ? });
  14. ? ? ?var id = e.currentTarget.dataset.index;
  15. ? ? ?var name = e.currentTarget.dataset.name;
  16. ? ? ?chosedarea.push(name);
  17. ? ? ? ?this.setData({
  18. ? ? ? ? ?"area": chosedarea
  19. ? ? ? ?})
  20. ? },
  21. ? // 取消已选地区
  22. ? shut:function(e){
  23. ? ? this.setData({
  24. ? ? ? index: e.currentTarget.dataset.index,
  25. ? ? ? name : e.currentTarget.dataset.name
  26. ? ? });
  27. ? ? var yid = e.currentTarget.dataset.index;
  28. ? ? var yname = e.currentTarget.dataset.name;
  29. ? ? chosedarea.splice(yid,1)
  30. ? ? this.setData({
  31. ? ? ? "area": chosedarea
  32. ? ? })
  33. ? },
  34. ? // 点击完成隐藏
  35. ? finish:function(){
  36. ? ? var that = this;
  37. ? ? if (that.data.a == true) {
  38. ? ? ? that.setData({
  39. ? ? ? ? a: false ??
  40. ? ? ? })
  41. ? ? }
  42. ? },
  43. ? // 点击地区显示
  44. ? show:function(){
  45. ? ? var that = this;
  46. ? ? if (that.data.a == false) {
  47. ? ? ? that.setData({
  48. ? ? ? ? a: true ? ?
  49. ? ? ? })
  50. ? ? }
  51. ? },
  52. ? })

css

  1. .container{
  2. ? width: 100%;
  3. ? height: 300rpx;
  4. }
  5. .choose{
  6. ? width: 100%;
  7. }
  8. .buttond{
  9. ? width: 88%;
  10. ? padding: 0;
  11. ? height: 68rpx;
  12. ? line-height: 68rpx;
  13. ? font-size: 32rpx;
  14. ? margin-bottom: 10rpx;
  15. }
  16. .area{
  17. ? display: flex;
  18. ? flex-direction: column;
  19. ? margin: 0 20rpx;
  20. }
  21. .chos{
  22. ? display: flex;
  23. ? flex-wrap: wrap;
  24. ? margin-top: 15rpx;
  25. }
  26. .button{
  27. ? width: 22%;
  28. ? padding: 0;
  29. ? height: 68rpx;
  30. ? line-height: 68rpx;
  31. ? font-size: 32rpx;
  32. ? margin: 0 10rpx;
  33. ? margin-bottom: 10rpx;
  34. }
  35. .view{
  36. ? display: flex;
  37. ? flex-wrap: wrap;
  38. ? height: auto;
  39. ? margin: 0 20rpx;
  40. }
  41. .position{
  42. ? width: 24%;
  43. }
  44. .chosedtop{
  45. ? display: flex;
  46. ? justify-content: space-between;
  47. ? margin: 0 30rpx 15rpx
  48. }

如果是选完 上面添加了对应的 下面可选择的与之对应的要隐藏掉 就这样: 

 js中

  1. // 选地区
  2. ? tabarea: function (e) {
  3. ? ? let that = this;
  4. ? ? that.setData({
  5. ? ? ? tabindex: e.currentTarget.dataset.index
  6. ? ? });
  7. ? ? var id = e.currentTarget.dataset.index;
  8. ? ? var name = e.currentTarget.dataset.name;
  9. ? ? chosedarea.push(name);
  10. ? ? that.data.array.splice(id, 1);
  11. ? ? that.setData({
  12. ? ? ? "area": chosedarea,
  13. ? ? ? "array": that.data.array
  14. ? ? })
  15. ? },

在上面点击删除的话 下面可选择的区域要对应的添加上:

wxml:

  1. <view class="container">
  2. ? <text bindtap="show" class="color">地区</text>
  3. ? <view class="choose" wx-if="{{a}}">
  4. ? ? <!-- 已选地区 -->
  5. ? ? <view class="chosed">
  6. ? ? ? <view class="chosedtop">
  7. ? ? ? ? <text>已选地区</text>
  8. ? ? ? ? <text bindtap="finish">完成</text>
  9. ? ? ? </view>
  10. ? ? ? <view class="view">
  11. ? ? ? ? <view class="position" wx:for='{{area}}' wx:key='index'>
  12. ? ? ? ? ? <button class="buttond" data-index="{{index}}" data-name="{{item}}" bindtap="addName">{{item}}</button>
  13. ? ? ? ? ? <!-- <image src='' bindtap="shut" data-index="{{index}}" data-name="{{item}}"></image> -->
  14. ? ? ? ? ? <!-- //这是按钮右上角的关闭图片 -->
  15. ? ? ? ? </view>
  16. ? ? ? </view>
  17. ? ? </view>
  18. ? ? <!-- 选择地区 -->
  19. ? ? <view class="area">
  20. ? ? ? <text>选择地区</text>
  21. ? ? ? <view class="chos">
  22. ? ? ? ? <block wx:for='{{array}}' wx:key='index'>
  23. ? ? ? ? ? <button class="button {{tabindex == index?'active':''}}" data-index="{{index}}" data-name="{{item}}" bindtap="tabarea">{{item}}</button>
  24. ? ? ? ? </block>
  25. ? ? ? </view>
  26. ? ? </view>
  27. ? </view>
  28. </view>

js

  1. var chosedarea = [];
  2. var area = [];
  3. Page({
  4. ? data: {
  5. ? ? a: false,
  6. ? ? tabindex: 0,
  7. ? ? array: ["北京", '南京', '上海', '天津', '杭州', '云南', "新疆", '黑龙江', '东北', '威海', '宁夏', '广西', "西安", '山东', '青岛', '济南', '烟台', '蓬莱'],
  8. ? },
  9. ? // 选地区
  10. ? tabarea: function (e) {
  11. ? ? let that = this;
  12. ? ? that.setData({
  13. ? ? ? tabindex: e.currentTarget.dataset.index
  14. ? ? });
  15. ? ? var id = e.currentTarget.dataset.index;
  16. ? ? var name = e.currentTarget.dataset.name;
  17. ? ? chosedarea.push(name);
  18. ? ? that.data.array.splice(id, 1);
  19. ? ? that.setData({
  20. ? ? ? "area": chosedarea,
  21. ? ? ? "array": that.data.array
  22. ? ? })
  23. ? },
  24. ? // 添加回
  25. ? addName:function(e){
  26. ? ? let that = this;
  27. ? ? console.log(e)
  28. ? ? that.setData({
  29. ? ? ? index: e.currentTarget.dataset.index,
  30. ? ? })
  31. ? ? var aname = e.currentTarget.dataset.name;
  32. ? ? chosedarea.splice(that.data.index,1);
  33. ? ? that.data.array.push(aname);
  34. ? ? that.setData({
  35. ? ? ? "area": chosedarea,
  36. ? ? ? "array": that.data.array
  37. ? ? })
  38. ? },
  39. ? // 取消已选地区
  40. ? shut: function (e) {
  41. ? ? this.setData({
  42. ? ? ? index: e.currentTarget.dataset.index,
  43. ? ? ? name: e.currentTarget.dataset.name
  44. ? ? });
  45. ? ? var yid = e.currentTarget.dataset.index;
  46. ? ? var yname = e.currentTarget.dataset.name;
  47. ? ? chosedarea.splice(yid, 1)
  48. ? ? this.setData({
  49. ? ? ? "area": chosedarea
  50. ? ? })
  51. ? },
  52. ? // 点击完成隐藏
  53. ? finish: function () {
  54. ? ? var that = this;
  55. ? ? if (that.data.a == true) {
  56. ? ? ? that.setData({
  57. ? ? ? ? a: false
  58. ? ? ? })
  59. ? ? }
  60. ? },
  61. ? // 点击地区显示
  62. ? show: function () {
  63. ? ? var that = this;
  64. ? ? if (that.data.a == false) {
  65. ? ? ? that.setData({
  66. ? ? ? ? a: true
  67. ? ? ? })
  68. ? ? }
  69. ? },
  70. })

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号