• 微信小程序实现删除功能


     1. 前端

    项目列表展示是使用的wx:for遍历

    每个项目展示有3个模块

    1. project-title

    2. project-content

    3. project-foot

     全部代码如下

    1. <t-sticky>
    2. <view class="search">
    3. <t-search model:value="{{conditions.keyword}}" placeholder="输入 户名 手机号 省份 城市 查询" center bind:change="inputKeyword" bind:clear="clearKeyword" bind:submit="doSearch" />
    4. view>
    5. <t-dropdown-menu>
    6. <t-dropdown-item label="选择状态" optionsColumns="2" options="{{workflows}}" value="{{conditions.selectedWorkflows}}" bindchange="selectWorkflow" bindconfirm="confirmSelectWorkflow" default-value="{{conditions.selectedWorkflows}}" multiple />
    7. <t-dropdown-item label="选择城市" bindchange="selectCity" bindconfirm="confirmSelectCity" optionsColumns="3" options="{{cities}}" value="{{conditions.selectedCities}}" default-value="{{conditions.selectedCities}}" multiple />
    8. <t-dropdown-item label="选择用户" bindchange="selectUser" bindconfirm="confirmSelectUser" wx:if="{{user.admin}}" optionsColumns="3" options="{{users}}" value="{{conditions.selectedUsers}}" default-value="{{conditions.selectedUsers}}" multiple />
    9. t-dropdown-menu>
    10. t-sticky>
    11. <list-rack length="{{records.length}}" loading="{{loading}}" list-end="{{dataEnd}}">
    12. <block wx:for="{{records}}" wx:key="id">
    13. <view class="project-title">
    14. <view class="left">#{{item.serialNo}}view>
    15. <view class="right" bind:tap="onAction" data-id="{{item.id}}">
    16. <t-icon name="ellipsis" size="48rpx" />
    17. view>
    18. view>
    19. <view class="project-content">
    20. <view class="basic-info" bind:tap="view">
    21. <view class="left">
    22. <view>
    23. <text class="name" data-id="{{item.id}}">{{item.name}}text>
    24. <text class="phone">{{item.phone}}text>
    25. view>
    26. <view class="city">{{item.province}}-{{item.city}}view>
    27. view>
    28. <view class="right">
    29. <t-tag class="margin-16" theme="success" variant="light" size="large">
    30. {{item.workflow}}
    31. t-tag>
    32. view>
    33. view>
    34. <view class="address">
    35. {{item.address}}
    36. view>
    37. view>
    38. <view class="project-foot">
    39. <view class="left">业务员:{{item.userName}} 工程人员: {{item.builderName}}view>
    40. <view class="right">{{item.createAt}}view>
    41. view>
    42. block>
    43. list-rack>
    44. <t-action-sheet id="t-action-sheet" bind:selected="handleAction" />
    45. <t-fab icon="add" bind:click="addProject" text="项目">t-fab>

     点击删除的功能是定义在

    project-title

    中 bind:tap绑定了onAction(e)方法,点击时触发 ,自定义属性data-id绑定了当前项目的id值

    js页面如下:

    全部js代码,找一下onAction(e)方法

    1. import Project from '../../../api/project'
    2. import ActionSheet, { ActionSheetTheme } from '../../../miniprogram_npm/tdesign-miniprogram/action-sheet/index';
    3. import I18n from '../../../i18n/index'
    4. import PageList from '../../../behaviors/page-list'
    5. Page({
    6. behaviors: [I18n, PageList],
    7. /**
    8. * 页面的初始数据
    9. */
    10. data: {
    11. workflows: [],
    12. cities: [],
    13. users: [],
    14. conditions: {
    15. keyword: null,
    16. selectedWorkflows: [],
    17. selectedCities: [],
    18. selectedUsers: [],
    19. pageSize: 20,
    20. currentPage: 1
    21. },
    22. project: [],
    23. user: {},
    24. true: true
    25. },
    26. /**
    27. * 生命周期函数--监听页面加载
    28. */
    29. onLoad(options) {
    30. console.log("project index onload:。。。。")
    31. let self = this
    32. //城市列表数据从Project归属地中查询
    33. Project.findAllCities().then((res) => {
    34. console.log("获取到城市有:" + res.data)
    35. let cs = res.data.map(city => {
    36. return {
    37. value: city,
    38. label: city
    39. }
    40. })
    41. self.setData({
    42. cities: cs
    43. })
    44. })
    45. //用户列表数据从Project所有userId中查询
    46. Project.findAllUsers().then((res) => {
    47. console.log("获取到的user有:" + res.data)
    48. let rcds = res.data.map((rd) => {
    49. return {
    50. value: rd.id,
    51. label: rd.name
    52. }
    53. })
    54. self.setData({
    55. users: rcds
    56. })
    57. })
    58. //设置审核状态
    59. this.setData({
    60. //新的/审核信息不完整(newly),待审核(apply),审核通过(approved)/挂起(pending);
    61. //施工中/整改(building), 施工验收(builded-check)验收通过(check-passed),项目完成(finish)
    62. workflows: [{
    63. value: 'newly',
    64. label: this.$t('projectWorkflowNewly')
    65. },
    66. {
    67. value: 'apply',
    68. label: this.$t('projectWorkflowApply')
    69. },
    70. {
    71. value: 'approved',
    72. label: this.$t('projectWorkflowApproved')
    73. },
    74. {
    75. value: 'building',
    76. label: this.$t('projectWorkflowBuilding')
    77. },
    78. {
    79. value: 'builded-check',
    80. label: this.$t('projectWorkflowBuildedCheck')
    81. },
    82. {
    83. value: 'check-passed',
    84. label: this.$t('projectWorkflowCheckPassed')
    85. },
    86. {
    87. value: 'finish',
    88. label: this.$t('projectWorkflowFinish')
    89. },
    90. {
    91. value: 'pending',
    92. label: this.$t('projectWorkflowPending')
    93. }
    94. ]
    95. });
    96. this.setData({
    97. user: getApp().globalData.user
    98. })
    99. this.resetData();
    100. //console.log('====', Permit.canAccess(this.data.user, 'project'))
    101. },
    102. injectEntity() {
    103. return Project
    104. },
    105. onTabsChange(tabVal) {
    106. console.log(tabVal.detail.value)
    107. },
    108. addProject() {
    109. wx.redirectTo({
    110. url: '/pages/project/new/firstStep/firstStep',
    111. })
    112. },
    113. selectWorkflow(e) {
    114. console.log(e)
    115. this.setData({
    116. "conditions.selectedWorkflows": e.detail.value
    117. })
    118. },
    119. selectCity(e) {
    120. console.log(e)
    121. this.setData({
    122. "conditions.selectedCities": e.detail.value
    123. })
    124. },
    125. selectUser(e) {
    126. console.log(e)
    127. this.setData({
    128. "conditions.selectedUsers": e.detail.value
    129. })
    130. },
    131. inputKeyword(e) {
    132. this.setData({
    133. 'conditions.keyword': e.detail.value
    134. })
    135. },
    136. clearKeyword(e) {
    137. this.setData({
    138. 'conditions.keyword': null
    139. })
    140. },
    141. onAction(e) {
    142. this.setData({ selectedRecord: this.pickRecord(e.currentTarget.dataset.id) })
    143. let menuItems = [
    144. // {label: '编辑', action: 'edit'},
    145. { label: '删除', action: 'delete' }
    146. ];
    147. ActionSheet.show({
    148. theme: ActionSheetTheme.List,
    149. selector: '#t-action-sheet',
    150. context: this,
    151. items: menuItems,
    152. });
    153. },
    154. handleAction(e) {
    155. switch (e.detail.selected.action) {
    156. case 'edit':
    157. this.edit()
    158. break;
    159. case 'delete':
    160. this.delete()
    161. break;
    162. }
    163. },
    164. delete() {
    165. console.log(this.data.selectedRecord.id)
    166. },
    167. edit() {
    168. },
    169. confirmSelectWorkflow() {
    170. this.resetData()
    171. },
    172. confirmSelectCity() {
    173. this.resetData()
    174. },
    175. confirmSelectUser() {
    176. this.resetData()
    177. }
    178. })

     其他导入的这些js文件:(都是定义好的,直接拿来用!)

    这个miniprogram_npm文件夹中的所有代码是tdesign模版中自带的,具体看这篇文章

    微信小程序项目开发Day1【项目搭建】_素心如月桠的博客-CSDN博客

    js代码我上传了

    【免费】微信小程序通用的js代码资源-CSDN文库

    百度网盘:

    链接:https://pan.baidu.com/s/1tRtjBBjbb4Z2PIpVZijg9A?pwd=zes8 
    提取码:zes8

    回到onAction(e)方法里面

     调用逻辑如下箭头所示

    最后调用的是  handleAction(e) 

     

     

     这个id是哪来的?一定要自己前端定义好,不是直接就能哪的

    自定义属性就是data-开头后面跟自定义的名字,这里我就写的id:data-id

     取值的时候,

    点击这个view标签,绑定的onAction(e)

    id值就在参数e里面

    this.setData({ selectedRecord: this.pickRecord(e.currentTarget.dataset.id) })

    setData到selectRecord可以打印到控制台如下:

     

     selectRecord定义在page-list.js里面,import导入进来就相对于把代码直接复制到当前页面,所以直接用就行

    接下来实现删除方法

    springboot后端delete的Controller写好

    回顾一下重点就这一块和

     

    1. onAction(e) {
    2. this.setData({ selectedRecord: this.pickRecord(e.currentTarget.dataset.id) })
    3. console.log("当前selectedRecord的id值:" + this.data.selectedRecord.id)
    4. let menuItems = [
    5. // {label: '编辑', action: 'edit'},
    6. { label: '删除', action: 'delete' }
    7. ];
    8. ActionSheet.show({
    9. theme: ActionSheetTheme.List,
    10. selector: '#t-action-sheet',
    11. context: this,
    12. items: menuItems,
    13. });
    14. },
    15. handleAction(e) {
    16. switch (e.detail.selected.action) {
    17. case 'edit':
    18. this.edit()
    19. break;
    20. case 'delete':
    21. this.delete()
    22. break;
    23. }
    24. },
    25. delete() {
    26. console.log(this.data.selectedRecord.id)
    27. Project.delete(this.data.selectedRecord.id).then((res) => {
    28. console.log(res)
    29. if (res.data.code == 200) {
    30. this.resetData();
    31. }
    32. })
    33. },

    删除代码如下:

    1. delete() {
    2. console.log(this.data.selectedRecord.id)
    3. Project.delete(this.data.selectedRecord.id).then((res) => {
    4. console.log(res)
    5. if (res.data.code == 200) {
    6. this.resetData();
    7. }
    8. })
    9. },

     

    实现成功:

     

  • 相关阅读:
    物理机服务器应该注意的事
    C++异步调用方法
    2022华为机试真题 C++ 实现【最大股票收益】
    C++牛客知识点
    【C++/STL】:list容器的深度剖析及模拟实现
    基于Node.js API开发:Error [ERR_HTTP_HEADERS_SENT]
    docker使用前的配置
    AutoCAD Electrical 2022—源箭头和目标箭头
    释放机器人潜力,INDEMIND深耕底层技术
    算法小讲堂之B树和B+树(浅谈)|考研笔记
  • 原文地址:https://blog.csdn.net/m0_47010003/article/details/133031199