当前位置:   article > 正文

uni-app:实现列表单选功能_uniapp单选功能

uniapp单选功能

效果图:

核心解析:

一、

  1. <view class="item_all" v-for="(item, index) in info" :key="index">
  2. <view class='position parameter-info text-over' :class='item.checked?"checked_parameter":""'
  3. :data-id="item.employee_num" @tap='selectcustomer'>
  4. <view class="vv_1">{{item.num_name}}</view>
  5. </view>
  6. </view>

v-for="(item, index) in info":将数据进行循环展示

:class='item.checked?"checked_parameter":"" ':表示如果当前行的item.checked为真吗,如果为真执行class="checked_parameter",如果不为真,就执行class="",也就是判断该行数据是否被选中,选中进行颜色更改

:data-id="item.employee_num":是在uni-app中使用 v-bind 指令将 data-id 属性绑定到 item.employee_num 这个表达式的值。data-id 是一个自定义属性,可以用于存储某个元素的额外数据。也就是绑定一个值,方便在js中引用

@tap='selectcustomer':点击事件

 二、

  1. info: [{
  2. employee_num: 1001,
  3. employee_name: '张三',
  4. checked: false,
  5. num_name: '1001-张三'
  6. },
  7. {
  8. employee_num: 1002,
  9. employee_name: '李四',
  10. checked: false,
  11. num_name: '1002-李四'
  12. }, {
  13. employee_num: 1003,
  14. employee_name: '王五',
  15. checked: false,
  16. num_name: '1003-王五'
  17. }, {
  18. employee_num: 1004,
  19. employee_name: '赵六',
  20. checked: false,
  21. num_name: '1004-赵六'
  22. }],
  23. parameterList: ''

在data中定义数据

info(也可以设置为空数组,请求服务器端的数据)

parameterList:定义一个字符串,用于存放被选中数据的行信息

三、

  1. // 参数点击响应事件
  2. selectcustomer: function(e) {
  3. var this_checked = e.currentTarget.dataset.id //获取对应的条目id
  4. var parameterList = this.info //获取Json数组
  5. console.log(this_checked)
  6. for (var i = 0; i < parameterList.length; i++) {
  7. if (parameterList[i].employee_num == this_checked) {
  8. parameterList[i].checked = true; //当前点击的位置为true即选中
  9. this.parameterList = parameterList[i]
  10. console.log('参数', this.parameterList)
  11. } else {
  12. parameterList[i].checked = false; //其他的位置为false
  13. }
  14. }
  15. this.info = parameterList;
  16. },

var this_checked=e.currentTarget.dataset.id:获取被选中行的:data-id中的值(employee_num)

var parameterList = this.info :获取全部数组的值info

for (var i = 0; i < parameterList.length; i++) :对info的数据进行循环

if (parameterList[i].employee_num == this_checked) :判断info中的每个employee_num是否有与被选中的行的employee_num相等的

parameterList[i].checked = true; :将满足条件的info数组中的这行数据中的checked 值设置为true,也就表示这行数据被选中

this.parameterList = parameterList[i] :也就是将data中定义的parameterList的值设置为数组info中的这行数据

parameterList[i].checked = false; :不满足的行,需要将checked的值设置为false

 this.info = parameterList; :更新完数据之后重新定义info数组的值

全部代码:

  1. <template>
  2. <view>
  3. <view class="top">
  4. <view class="search">
  5. <view class="search_in">
  6. <!-- 使用代码请更改图片路径 -->
  7. <image :src="search"></image>
  8. <input type="text" placeholder="请输入名称" placeholder-style="color:#CCCCCC" @confirm="search" />
  9. </view>
  10. </view>
  11. </view>
  12. <view class="center">
  13. <view class="pages_name">
  14. <view class="li"></view>
  15. <view class="li2">员工信息</view>
  16. </view>
  17. </view>
  18. <view class="all">
  19. <view class="item_all" v-for="(item, index) in info" :key="index">
  20. <view class='position parameter-info text-over' :class='item.checked?"checked_parameter":""'
  21. :data-id="item.employee_num" @tap='selectcustomer'>
  22. <view class="vv_1">{{item.num_name}}</view>
  23. </view>
  24. </view>
  25. </view>
  26. <view class="button_sure" @tap="sure">
  27. <view class="sure_text">确认</view>
  28. </view>
  29. <!-- 添加按钮 -->
  30. <image class='img' :src='add' @tap='add'></image>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. data() {
  36. return {
  37. search: getApp().globalData.icon + 'index/search.png',
  38. add: getApp().globalData.icon + 'index/index/add.png',
  39. info: [{
  40. employee_num: 1001,
  41. employee_name: '张三',
  42. checked: false,
  43. num_name: '1001-张三'
  44. },
  45. {
  46. employee_num: 1002,
  47. employee_name: '李四',
  48. checked: false,
  49. num_name: '1002-李四'
  50. }, {
  51. employee_num: 1003,
  52. employee_name: '王五',
  53. checked: false,
  54. num_name: '1003-王五'
  55. }, {
  56. employee_num: 1004,
  57. employee_name: '赵六',
  58. checked: false,
  59. num_name: '1004-赵六'
  60. }
  61. ],
  62. parameterList: ''
  63. }
  64. },
  65. methods: {
  66. // 参数点击响应事件
  67. selectcustomer: function(e) {
  68. var this_checked = e.currentTarget.dataset.id //获取对应的条目id
  69. var parameterList = this.info //获取Json数组
  70. console.log(this_checked)
  71. for (var i = 0; i < parameterList.length; i++) {
  72. if (parameterList[i].employee_num == this_checked) {
  73. parameterList[i].checked = true; //当前点击的位置为true即选中
  74. this.parameterList = parameterList[i]
  75. console.log('参数', this.parameterList)
  76. } else {
  77. parameterList[i].checked = false; //其他的位置为false
  78. }
  79. }
  80. this.info = parameterList;
  81. },
  82. },
  83. // onLoad() {
  84. // uni.request({
  85. // url: getApp().globalData.position + 'Produce/select_employee',
  86. // data: {
  87. // },
  88. // header: {
  89. // "Content-Type": "application/x-www-form-urlencoded"
  90. // },
  91. // method: 'POST',
  92. // dataType: 'json',
  93. // success: res => {
  94. // this.info = res.data.all_info
  95. // },
  96. // fail(res) {
  97. // console.log("查询失败")
  98. // }
  99. // });
  100. // }
  101. }
  102. </script>
  103. <style>
  104. /* 背景色 */
  105. page {
  106. background-color: #F0F4F7;
  107. }
  108. /* 搜索框 */
  109. .search {
  110. display: flex;
  111. align-items: center;
  112. justify-content: center;
  113. height: 60px;
  114. background-color: #fff;
  115. /* border:1px solid black; */
  116. margin-bottom: 5%;
  117. }
  118. .search .search_in {
  119. display: flex;
  120. align-items: center;
  121. justify-content: space-between;
  122. padding: 0 20rpx;
  123. box-sizing: border-box;
  124. height: 70rpx;
  125. width: 90%;
  126. background-color: #F0F4F7;
  127. border-radius: 5px;
  128. }
  129. .search_in image {
  130. height: 45rpx;
  131. width: 50rpx;
  132. margin-right: 10px;
  133. /* border:1px solid black; */
  134. }
  135. .search input {
  136. /* border:1px solid black; */
  137. width: 100%;
  138. }
  139. /* 列表 */
  140. .all {
  141. margin-bottom: 20%;
  142. }
  143. .item_all {
  144. /* border: 1px solid black; */
  145. margin-bottom: 3%;
  146. display: flex;
  147. flex-direction: column;
  148. align-items: center;
  149. justify-content: center;
  150. width: 100%;
  151. }
  152. .position {
  153. display: flex;
  154. flex-direction: column;
  155. justify-content: center;
  156. height: 80px;
  157. width: 95%;
  158. border-radius: 10px;
  159. background-color: #fff;
  160. box-shadow: 2px 2px 2px gainsboro;
  161. }
  162. .vv_1 {
  163. margin-left: 5%;
  164. word-break: break-all;
  165. }
  166. /* 选中之后的样式设置 */
  167. .checked_parameter {
  168. background: #74bfe7;
  169. color: #fff;
  170. }
  171. .footer button {
  172. width: 100%;
  173. }
  174. /* 标题信息 */
  175. .center {
  176. display: flex;
  177. flex-direction: column;
  178. align-items: center;
  179. justify-content: center;
  180. width: 100%;
  181. margin-bottom: 3%;
  182. }
  183. .pages_name {
  184. /* border: 1px solid black; */
  185. width: 95%;
  186. display: flex;
  187. align-items: center;
  188. }
  189. .li {
  190. /* border: 1px solid black; */
  191. width: 15px;
  192. height: 15px;
  193. border-radius: 100%;
  194. background-color: #74bfe7;
  195. }
  196. .li2 {
  197. /* border: 1px solid black; */
  198. font-size: 110%;
  199. margin-left: 2%;
  200. }
  201. /* 悬浮按钮 */
  202. .img {
  203. float: right;
  204. position: fixed;
  205. bottom: 10%;
  206. right: 2%;
  207. width: 100rpx;
  208. height: 100rpx;
  209. }
  210. /* 确认按钮 */
  211. .button_sure {
  212. bottom: 0px;
  213. position: fixed;
  214. width: 100%;
  215. height: 8%;
  216. background: #74bfe7;
  217. color: white;
  218. font-size: 105%;
  219. display: flex;
  220. align-items: center;
  221. justify-content: center;
  222. }
  223. </style>

扩展:给此界面增加了翻页和模糊查询功能

效果:

前端:

  1. <template>
  2. <view>
  3. <view class="top">
  4. <view class="search">
  5. <view class="search_in">
  6. <!-- 使用代码请更改图片路径 -->
  7. <image :src="search"></image>
  8. <input type="text" placeholder="请输入员工工号" placeholder-style="color:#CCCCCC" @confirm="search_num" />
  9. </view>
  10. </view>
  11. </view>
  12. <view class="center">
  13. <view class="pages_name">
  14. <view class="li"></view>
  15. <view class="li2">员工信息</view>
  16. </view>
  17. </view>
  18. <view class="all">
  19. <view class="item_all" v-for="(item, index) in info" :key="index">
  20. <view class='position parameter-info text-over' :class='item.checked?"checked_parameter":""'
  21. :data-id="item.employee_num" @tap='selectcustomer'>
  22. <view class="vv_1">{{item.num_name}}</view>
  23. </view>
  24. </view>
  25. <view class="pagination">
  26. <view class="page-button" @tap="prevPage">上一页</view>
  27. <view class="page-info">{{ page }}</view>
  28. <view class="page-info">/</view>
  29. <view class="page-info">{{ totalPage }}</view>
  30. <view class="page-button" @tap="nextPage">下一页</view>
  31. </view>
  32. </view>
  33. <view class="button_sure" @tap="sure">
  34. <view class="sure_text">确认</view>
  35. </view>
  36. <!-- 添加按钮 -->
  37. <image class='img' :src='add' @tap='add'></image>
  38. </view>
  39. </template>
  40. <script>
  41. export default {
  42. data() {
  43. return {
  44. search: getApp().globalData.icon + 'index/search.png',
  45. add: getApp().globalData.icon + 'index/index/add.png',
  46. info: [],
  47. parameterList: '',
  48. like_employee_num: '', //模糊查询的员工工号
  49. page: 1, // 当前页数
  50. pageSize: 10, // 每页展示的数据条数
  51. totalPage: 0, //总页数
  52. }
  53. },
  54. methods: {
  55. // 参数点击响应事件,单选的实现
  56. selectcustomer: function(e) {
  57. var this_checked = e.currentTarget.dataset.id //获取对应的条目id
  58. var List = this.info //获取Json数组
  59. // console.log(this_checked)
  60. for (var i = 0; i < List.length; i++) {
  61. if (List[i].employee_num == this_checked) {
  62. List[i].checked = true; //当前点击的位置为true即选中
  63. this.parameterList = List[i]
  64. console.log('参数', this.parameterList)
  65. } else {
  66. List[i].checked = false; //其他的位置为false
  67. }
  68. }
  69. this.info = List;
  70. },
  71. //确认
  72. sure() {
  73. if (!this.parameterList) {
  74. uni.showToast({
  75. title: '请选择员工',
  76. icon: 'none'
  77. })
  78. } else {
  79. uni.$emit('isRefresh', this.parameterList)
  80. uni.navigateBack({
  81. delta: 1
  82. })
  83. }
  84. },
  85. //模糊查询
  86. search_num(event) {
  87. this.page = 1;//模糊查询默认从首页开始
  88. this.like_employee_num = event.target.value;
  89. this.getdata()
  90. },
  91. getdata() {
  92. uni.request({
  93. url: getApp().globalData.position + 'Produce/select_employee',
  94. data: {
  95. like_employee_num: this.like_employee_num,
  96. page: this.page,
  97. pageSize: this.pageSize
  98. },
  99. header: {
  100. "Content-Type": "application/x-www-form-urlencoded"
  101. },
  102. method: 'POST',
  103. dataType: 'json',
  104. success: res => {
  105. this.info = res.data.all_info
  106. this.totalPage = Math.ceil(res.data.total / this.pageSize);
  107. },
  108. fail(res) {
  109. console.log("查询失败")
  110. }
  111. });
  112. },
  113. prevPage() {
  114. if (this.page > 1) {
  115. this.page--;
  116. this.getdata();
  117. }
  118. },
  119. nextPage() {
  120. if (this.page < this.totalPage) {
  121. this.page++;
  122. this.getdata();
  123. }
  124. },
  125. },
  126. onLoad() {
  127. this.getdata();
  128. }
  129. }
  130. </script>
  131. <style>
  132. .pagination {
  133. display: flex;
  134. align-items: center;
  135. justify-content: left;
  136. margin-bottom: 20%;
  137. /* border: 1px solid black; */
  138. }
  139. .page-button {
  140. height: 30px;
  141. line-height: 30px;
  142. padding: 0 10px;
  143. border: 1px solid white;
  144. border-radius: 5px;
  145. margin: 0 5px;
  146. cursor: pointer;
  147. }
  148. .page-info {
  149. font-weight: bold;
  150. }
  151. /* 背景色 */
  152. page {
  153. background-color: #F0F4F7;
  154. }
  155. /* 搜索框 */
  156. .search {
  157. display: flex;
  158. align-items: center;
  159. justify-content: center;
  160. height: 120rpx;
  161. background-color: #fff;
  162. /* border:1px solid black; */
  163. margin-bottom: 5%;
  164. }
  165. .search .search_in {
  166. display: flex;
  167. align-items: center;
  168. justify-content: space-between;
  169. padding: 0 20rpx;
  170. box-sizing: border-box;
  171. height: 70rpx;
  172. width: 90%;
  173. background-color: #F0F4F7;
  174. border-radius: 10rpx;
  175. }
  176. .search_in image {
  177. height: 45rpx;
  178. width: 50rpx;
  179. margin-right: 20rpx;
  180. /* border:1px solid black; */
  181. }
  182. .search input {
  183. /* border:1px solid black; */
  184. width: 100%;
  185. }
  186. /* 列表 */
  187. .all {
  188. margin-bottom: 20%;
  189. border: 1px solid #F0F4F7;
  190. }
  191. .item_all {
  192. /* border: 1px solid black; */
  193. margin-bottom: 3%;
  194. display: flex;
  195. flex-direction: column;
  196. align-items: center;
  197. justify-content: center;
  198. width: 100%;
  199. }
  200. .position {
  201. display: flex;
  202. flex-direction: column;
  203. justify-content: center;
  204. height: 160rpx;
  205. width: 95%;
  206. border-radius: 20rpx;
  207. background-color: #fff;
  208. box-shadow: 4rpx 4rpx 4rpx gainsboro;
  209. }
  210. .vv_1 {
  211. margin-left: 5%;
  212. word-break: break-all;
  213. }
  214. /* 选中之后的样式设置 */
  215. .checked_parameter {
  216. background: #74bfe7;
  217. color: #fff;
  218. }
  219. .footer button {
  220. width: 100%;
  221. }
  222. /* 标题信息 */
  223. .center {
  224. display: flex;
  225. flex-direction: column;
  226. align-items: center;
  227. justify-content: center;
  228. width: 100%;
  229. margin-bottom: 3%;
  230. }
  231. .pages_name {
  232. /* border: 1px solid black; */
  233. width: 95%;
  234. display: flex;
  235. align-items: center;
  236. }
  237. .li {
  238. /* border: 1px solid black; */
  239. width: 30rpx;
  240. height: 30rpx;
  241. border-radius: 100%;
  242. background-color: #74bfe7;
  243. }
  244. .li2 {
  245. /* border: 1px solid black; */
  246. font-size: 110%;
  247. margin-left: 2%;
  248. }
  249. /* 悬浮按钮 */
  250. .img {
  251. float: right;
  252. position: fixed;
  253. bottom: 15%;
  254. right: 2%;
  255. width: 100rpx;
  256. height: 100rpx;
  257. }
  258. /* 确认按钮 */
  259. .button_sure {
  260. bottom: 0rpx;
  261. position: fixed;
  262. width: 100%;
  263. height: 8%;
  264. background: #74bfe7;
  265. color: white;
  266. font-size: 105%;
  267. display: flex;
  268. align-items: center;
  269. justify-content: center;
  270. }
  271. </style>

后端:

  1. //查询员工详细信息
  2. public function select_employee()
  3. {
  4. $like_employee_num = input('post.like_employee_num','');//模糊查询的条件
  5. $page = input('post.page', 1); // 获取当前页数,默认为第一页
  6. $pageSize = input('post.pageSize', 10); // 获取每页展示的数据条数,默认为10条
  7. $start = ($page - 1) * $pageSize; // 计算查询的起始位置
  8. //计算总页数
  9. $count = Db::table('hr_employees')->where('employee_num', 'like', '%' . $like_employee_num . '%')->count(); // 查询符合条件的总记录数
  10. $data['total'] = $count; // 将总记录数返回给前端
  11. //查询数据
  12. $data['all_info'] = db::table('hr_employees')->where(['employee_num'=>['like', '%' . $like_employee_num . '%']])->limit($start, $pageSize)->select();
  13. //处理拼接数据和单选所需数据
  14. for($i=0;$i<count($data['all_info']);$i++){
  15. $data['all_info'][$i]['num_name'] = $data['all_info'][$i]['employee_num'] . '-' . $data['all_info'][$i]['employee_name'];
  16. $data['all_info'][$i]['checked'] = false;
  17. }
  18. //返回值给前端
  19. echo json_encode($data);
  20. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/627885
推荐阅读
相关标签
  

闽ICP备14008679号