swipe-action.nvue 7.9 KB

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