swipe-action.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <view class="container">
  3. <uni-card is-full :is-shadow="false">
  4. <text class="uni-h6">通过滑动触发选项的容器,容器内可放置列表等组件,通过左右滑动来触发一些操作。</text>
  5. </uni-card>
  6. <uni-section
  7. title="基本用法"
  8. type="line"
  9. ></uni-section>
  10. <uni-swipe-action>
  11. <uni-swipe-action-item
  12. :left-options="options2"
  13. :threshold="0"
  14. :right-options="options1"
  15. @click="bindClick"
  16. >
  17. <view class="content-box">
  18. <text class="content-text">使用数据填充</text>
  19. </view>
  20. </uni-swipe-action-item>
  21. <uni-swipe-action-item @click="bindClick">
  22. <template v-slot:left>
  23. <view class="slot-button">
  24. <text
  25. class="slot-button-text"
  26. @click="bindClick({position:'left',content:{text:'置顶'}})"
  27. >置顶</text>
  28. </view>
  29. </template>
  30. <view class="content-box">
  31. <text class="content-text">使用插槽</text>
  32. </view>
  33. <template v-slot:right>
  34. <view class="slot-button"><text class="slot-button-text">删除</text></view>
  35. </template>
  36. </uni-swipe-action-item>
  37. <uni-swipe-action-item
  38. :right-options="options1"
  39. @click="bindClick"
  40. >
  41. <template v-slot:left>
  42. <view class="slot-button"><text
  43. class="slot-button-text"
  44. @click="bindClick({position:'left',content:{text:'置顶'}})"
  45. >置顶</text></view>
  46. </template>
  47. <view class="content-box">
  48. <text class="content-text">混合使用</text>
  49. </view>
  50. </uni-swipe-action-item>
  51. </uni-swipe-action>
  52. <uni-section
  53. title="禁止滑动"
  54. type="line"
  55. ></uni-section>
  56. <uni-swipe-action>
  57. <uni-swipe-action-item :disabled="true">
  58. <view class="content-box">
  59. <text class="content-text">禁止左右滚动</text>
  60. </view>
  61. </uni-swipe-action-item>
  62. </uni-swipe-action>
  63. <uni-section
  64. title="使用变量控制开关"
  65. type="line"
  66. ></uni-section>
  67. <view class="example-body">
  68. <view
  69. class="button"
  70. @click="setOpened"
  71. >
  72. <text class="button-text">当前状态:{{ isOpened }}</text>
  73. </view>
  74. </view>
  75. <uni-swipe-action>
  76. <uni-swipe-action-item
  77. :left-options="options2"
  78. :right-options="options2"
  79. :show="isOpened"
  80. :auto-close="false"
  81. @change="change"
  82. @click="bindClick"
  83. >
  84. <view class="content-box">
  85. <text class="content-text">使用变量控制SwipeAction的开启状态</text>
  86. </view>
  87. </uni-swipe-action-item>
  88. </uni-swipe-action>
  89. <uni-section
  90. title="swipe-action 列表"
  91. type="line"
  92. ></uni-section>
  93. <uni-swipe-action>
  94. <uni-swipe-action-item
  95. v-for="(item, index) in swipeList"
  96. :right-options="item.options"
  97. :key="item.id"
  98. @change="swipeChange($event, index)"
  99. @click="swipeClick($event, index)"
  100. >
  101. <view class="content-box">
  102. <text class="content-text">{{ item.content }}</text>
  103. </view>
  104. </uni-swipe-action-item>
  105. </uni-swipe-action>
  106. </view>
  107. </template>
  108. <script>
  109. export default {
  110. components: {},
  111. data() {
  112. return {
  113. show: false,
  114. isOpened: 'none',
  115. options1: [{
  116. text: '取消置顶'
  117. }],
  118. options2: [{
  119. text: '取消',
  120. style: {
  121. backgroundColor: '#007aff'
  122. }
  123. },
  124. {
  125. text: '确认',
  126. style: {
  127. backgroundColor: '#F56C6C'
  128. }
  129. }
  130. ],
  131. swipeList: []
  132. };
  133. },
  134. onReady() {
  135. // 模拟延迟赋值
  136. setTimeout(() => {
  137. this.isOpened = 'right';
  138. this.swipeList = [{
  139. options: [{
  140. text: '添加',
  141. style: {
  142. backgroundColor: '#F56C6C'
  143. }
  144. }],
  145. id: 0,
  146. content: 'item1'
  147. },
  148. {
  149. id: 1,
  150. options: [{
  151. text: '置顶'
  152. },
  153. {
  154. text: '删除',
  155. style: {
  156. backgroundColor: 'rgb(255,58,49)'
  157. }
  158. }
  159. ],
  160. content: 'item2'
  161. },
  162. {
  163. id: 2,
  164. options: [{
  165. text: '置顶'
  166. },
  167. {
  168. text: '标记为已读',
  169. style: {
  170. backgroundColor: 'rgb(254,156,1)'
  171. }
  172. },
  173. {
  174. text: '删除',
  175. style: {
  176. backgroundColor: 'rgb(255,58,49)'
  177. }
  178. }
  179. ],
  180. content: 'item3'
  181. }
  182. ]
  183. }, 1000);
  184. },
  185. methods: {
  186. bindClick(e) {
  187. uni.showToast({
  188. title: `点击了${e.position === 'left' ? '左侧' : '右侧'} ${e.content.text}按钮`,
  189. icon: 'none'
  190. });
  191. },
  192. setOpened() {
  193. if (this.isOpened === 'none') {
  194. this.isOpened = 'left';
  195. return;
  196. }
  197. if (this.isOpened === 'left') {
  198. this.isOpened = 'right';
  199. return;
  200. }
  201. if (this.isOpened === 'right') {
  202. this.isOpened = 'none';
  203. return;
  204. }
  205. },
  206. change(e) {
  207. this.isOpened = e;
  208. console.log('返回:', e);
  209. },
  210. swipeChange(e, index) {
  211. console.log('返回:', e);
  212. console.log('当前索引:', index);
  213. },
  214. swipeClick(e, index) {
  215. let {
  216. content
  217. } = e;
  218. if (content.text === '删除') {
  219. uni.showModal({
  220. title: '提示',
  221. content: '是否删除',
  222. success: res => {
  223. if (res.confirm) {
  224. this.swipeList.splice(index, 1);
  225. } else if (res.cancel) {
  226. console.log('用户点击取消');
  227. }
  228. }
  229. });
  230. } else if (content.text === '添加') {
  231. if (this.swipeList.length < 10) {
  232. this.swipeList.push({
  233. id: new Date().getTime(),
  234. options: [{
  235. text: '置顶'
  236. },
  237. {
  238. text: '标记为已读',
  239. style: {
  240. backgroundColor: 'rgb(254,156,1)'
  241. }
  242. },
  243. {
  244. text: '删除',
  245. style: {
  246. backgroundColor: 'rgb(255,58,49)'
  247. }
  248. }
  249. ],
  250. content: '新增' + new Date().getTime()
  251. });
  252. uni.showToast({
  253. title: `添加了一条数据`,
  254. icon: 'none'
  255. });
  256. } else {
  257. uni.showToast({
  258. title: `最多添加十条数据`,
  259. icon: 'none'
  260. });
  261. }
  262. } else {
  263. uni.showToast({
  264. title: `点击了${e.content.text}按钮`,
  265. icon: 'none'
  266. });
  267. }
  268. }
  269. }
  270. };
  271. </script>
  272. <style lang="scss">
  273. .content-box {
  274. flex: 1;
  275. /* #ifdef APP-NVUE */
  276. justify-content: center;
  277. /* #endif */
  278. height: 44px;
  279. line-height: 44px;
  280. padding: 0 15px;
  281. position: relative;
  282. background-color: #fff;
  283. border-bottom-color: #f5f5f5;
  284. border-bottom-width: 1px;
  285. border-bottom-style: solid;
  286. }
  287. .content-text {
  288. font-size: 15px;
  289. }
  290. .example-body {
  291. /* #ifndef APP-NVUE */
  292. display: flex;
  293. /* #endif */
  294. flex-direction: row;
  295. justify-content: center;
  296. padding: 10px 0;
  297. background-color: #fff;
  298. }
  299. .button {
  300. border-color: #e5e5e5;
  301. border-style: solid;
  302. border-width: 1px;
  303. padding: 4px 8px;
  304. border-radius: 4px;
  305. }
  306. .button-text {
  307. font-size: 15px;
  308. }
  309. .slot-button {
  310. /* #ifndef APP-NVUE */
  311. display: flex;
  312. height: 100%;
  313. /* #endif */
  314. flex: 1;
  315. flex-direction: row;
  316. justify-content: center;
  317. align-items: center;
  318. padding: 0 20px;
  319. background-color: #ff5a5f;
  320. }
  321. .slot-button-text {
  322. color: #ffffff;
  323. font-size: 14px;
  324. }
  325. </style>