自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從微信小程序到鴻蒙JS開發(fā)【04】-list組件

開發(fā)
文章由鴻蒙社區(qū)產出,想要了解更多內容請前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術社區(qū)https://harmonyos.51cto.com/#zz

[[381248]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區(qū)

https://harmonyos.51cto.com/#zz

1、可滾動區(qū)域

在許多場景中,頁面會有一塊區(qū)域是可滾動的,比如這樣一個簡單的每日新聞模塊:

從微信小程序到鴻蒙js開發(fā)【04】——list組件
從微信小程序到鴻蒙js開發(fā)【04】——list組件

上面的新聞類型是一塊可橫向滾動的區(qū)域,下方新聞列表是一塊可豎向滾動的區(qū)域。在微信小程序中,使用scroll-view組件即可實現。那么在鴻蒙js組件中,想要實現可滾動的區(qū)域,則是使用list組件。list僅支持豎向滾動,橫向滾動要用tabs,將在下篇博客講解。

2、list + list-item

這里以本地新聞模塊為例,數據請求自天行數據接口(https://www.tianapi.com/apiview/154)。

從微信小程序到鴻蒙js開發(fā)【04】——list組件

上方為一個搜索框,下方是新聞列表。搜索框給了固定高度,那么怎樣讓新聞列表能夠占滿屏幕剩余部分呢?只需將父容器設置flex布局,list設置flex: 1即可。list下直接放list-item,在總高度超出list的高度后,即可上下滾動。

hml:

  1. <!-- 本地新聞 --> 
  2.  <div> 
  3.      <div class="searchView"
  4.          <image src="{{ searchIcon }}"></image> 
  5.          <input placeholder="搜你想看的"></input> 
  6.      </div> 
  7.      <list class="localView"
  8.          <block for="{{ localNews }}"
  9.              <list-item class="newsItem"
  10.                  <div class="newsContent"
  11.                      <text> 
  12.                          {{ $item.title }} 
  13.                      </text> 
  14.                      <div class="newsDesc"
  15.                          <text> 
  16.                              {{ $item.source }} 
  17.                          </text> 
  18.                          <text> 
  19.                              {{ $item.ctime }} 
  20.                          </text> 
  21.                      </div> 
  22.                  </div> 
  23.              </list-item> 
  24.          </block> 
  25.      </list> 
  26.  </div> 
  27.  <!-- 本地新聞end --> 

css:

  1. /*本地新聞*/ 
  2. .searchView { 
  3.     width: 100%; 
  4.     height: 140px; 
  5.     background-color: #f0f0f0; 
  6.     display: flex; 
  7.     align-items: center; 
  8. .searchView>image { 
  9.     margin: 0 40px 0 40px; 
  10.     height: 60px; 
  11.     width: 60px; 
  12. .searchView>input { 
  13.     margin-right: 40px; 
  14. .localView { 
  15.     width: 100%; 
  16.     flex: 1; 
  17.     display: flex; 
  18.     flex-direction: column
  19. .localContent { 
  20.     margin-left: 20px; 
  21. .newsItem { 
  22.     width: 100%; 
  23.     height: 240px; 
  24.     border-bottom: 1px solid #bbbbbb; 
  25.     display: flex; 
  26.     align-items: center; 
  27. .newsContent { 
  28.     display: flex; 
  29.     flex-direction: column
  30.     margin-right: 20px; 
  31.     margin-left: 20px; 
  32. .newsContent>text { 
  33.     margin-top: 20px; 
  34.     height: 140px; 
  35.     font-size: 34px; 
  36.     color: #333333; 
  37. .newsDesc { 
  38.     height: 60px; 
  39.     line-height: 60px; 
  40.     display: flex; 
  41.     justify-content: space-between
  42. .newsDesc>text { 
  43.     font-size: 28px; 
  44.     color: #777777; 

 js:

  1. searchLocalNews() { 
  2.      let url = 'http://api.tianapi.com/areanews/index?key=xxxx&areaname=江蘇'
  3.      if (this.searchWord) { 
  4.          url = url + '&word' + this.searchWord; 
  5.      } 
  6.      fetch.fetch({ 
  7.          url: url, 
  8.          responseType: 'json'
  9.          success: res => { 
  10.              let data = JSON.parse(res.data); 
  11.              this.localNews = data.newslist; 
  12.          } 
  13.      }) 
  14.  }, 

 新聞列表可滾動,且不會影響搜索框的位置。


3、list + list-item-group + list-item

list組件的子元素還可以是list-item-group,顧名思義應是分組列表項,list-item作為list-item-group的子元素。隨便寫一點看一看:

  1. <div> 
  2.         <list class="manageList"
  3.             <list-item-group class="list-item-group"
  4.                 <list-item class="list-item"
  5.                     <text> 
  6.                         <span>分組1 子項1</span> 
  7.                     </text> 
  8.                 </list-item> 
  9.                 <list-item class="list-item"
  10.                     <text> 
  11.                         <span>分組1 子項2</span> 
  12.                     </text> 
  13.                 </list-item> 
  14.                 <list-item class="list-item"
  15.                     <text> 
  16.                         <span>分組1 子項3</span> 
  17.                     </text> 
  18.                 </list-item> 
  19.             </list-item-group
  20.             <list-item-group class="list-item-group"
  21.                 <list-item class="list-item"
  22.                     <text> 
  23.                         <span>分組2 子項1</span> 
  24.                     </text> 
  25.                 </list-item> 
  26.                 <list-item class="list-item"
  27.                     <text> 
  28.                         <span>分組2 子項2</span> 
  29.                     </text> 
  30.                 </list-item> 
  31.                 <list-item class="list-item"
  32.                     <text> 
  33.                         <span>分組2 子項3</span> 
  34.                     </text> 
  35.                 </list-item> 
  36.             </list-item-group
  37.         </list> 
  38.     </div> 

  1. .manageList{ 
  2.     height: 100%; 
  3.     width: 100%; 
  4. .list-item-group
  5.     width: 100%; 
  6.     height: 450px; 
  7. .list-item{ 
  8.     width: 100%; 
  9.     height: 150px; 
  10.     display: flex; 
  11.     justify-content: center; 
  12.     align-items: center; 
  13.     border-bottom: 1px solid gray; 
  14. .list-item>text{ 
  15.     line-height: 100px; 

  

 

可以看出,list-item-group是可折疊的列表分組,且默認是折疊的。點擊右側小箭頭可展開列表,如果list-item-group給了高度,則折疊和展開后這一塊區(qū)域的高度不變。在折疊時,展示第一個list-item的內容。

那么如果每一個list-item-group中l(wèi)ist-item數目不固定,在展開后的布局就會很難看。如下:


其實不定義list-item-group的高度即可,折疊高度為list-item的高度,展開后高度自適應增長,超出list高度可以滾動,功能還是很強大的。更改css后的效果如下:


 

這種分組的列表,可以制作一個簡單的后臺管理系統(tǒng)菜單界面。這里我將菜單數據文件、圖片文件放入nginx服務器的目錄中,再通過內網穿透訪問資源。注意數據的格式,list-item-group和list-item之間存在父級標簽關系,故數據中也應存在父級關系。list-item-group展示的內容是其下第一個list-item,這里用一個兩重for循環(huán)實現:



manage.json:

  1.     "manageList": [ 
  2.         { 
  3.             "name""組織管理"
  4.             "icon""http://milkytea.free.idcfengye.com/images/christools/icon/org.png"
  5.             "subList": [ 
  6.                 { 
  7.                     "name""查詢組織"
  8.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/search.png" 
  9.                 }, 
  10.                 { 
  11.                     "name""添加組織"
  12.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/add.png" 
  13.                 }, 
  14.                 { 
  15.                     "name""刪除組織"
  16.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/delete.png" 
  17.                 } 
  18.             ] 
  19.         }, 
  20.         { 
  21.             "name""人員管理"
  22.             "icon""http://milkytea.free.idcfengye.com/images/christools/icon/person.png"
  23.             "subList": [ 
  24.                 { 
  25.                     "name""查詢人員"
  26.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/search.png" 
  27.                 }, 
  28.                 { 
  29.                     "name""添加人員"
  30.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/add.png" 
  31.                 }, 
  32.                 { 
  33.                     "name""批量導入人員"
  34.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/add.png" 
  35.                 }, 
  36.                 { 
  37.                     "name""刪除人員"
  38.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/delete.png" 
  39.                 }, 
  40.                 { 
  41.                     "name""修改人員"
  42.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/update.png" 
  43.                 } 
  44.             ] 
  45.         }, 
  46.         { 
  47.             "name""卡片管理"
  48.             "icon""http://milkytea.free.idcfengye.com/images/christools/icon/card.png"
  49.             "subList": [ 
  50.                 { 
  51.                     "name""開卡"
  52.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/add.png" 
  53.                 }, 
  54.                 { 
  55.                     "name""退卡"
  56.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/delete.png" 
  57.                 } 
  58.             ] 
  59.         } 
  60.     ] 

 hml:

  1. <!-- 管理 --> 
  2.      <div> 
  3.          <list class="manageList"
  4.              <block for="{{ manageList }}"
  5.                  <list-item-group class="list-item-group"
  6.                      <list-item class="list-item"
  7.                          <image src="{{ $item.icon }}"></image> 
  8.                          <text>{{ $item.name }}</text> 
  9.                      </list-item> 
  10.                      <block for="{{ (index, value) in $item.subList }}"
  11.                          <list-item class="list-item"
  12.                              <image src="{{ value.icon }}"></image> 
  13.                              <text>{{ value.name }}</text> 
  14.                          </list-item> 
  15.                      </block> 
  16.                  </list-item-group
  17.              </block> 
  18.          </list> 
  19.      </div> 
  20.      <!-- 管理end --> 

js:

  1. getManageList() { 
  2.        let url = "http://milkytea.free.idcfengye.com/text/manage.json"
  3.        fetch.fetch({ 
  4.            url: url, 
  5.            responseType: 'json'
  6.            success: res => { 
  7.                let data = JSON.parse(res.data); 
  8.                this.manageList = data.manageList; 
  9.            } 
  10.        }) 
  11.    } 

 想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區(qū)

https://harmonyos.51cto.com/#zz

 

責任編輯:jianghua 來源: 鴻蒙社區(qū)
相關推薦

2021-02-23 12:25:26

鴻蒙HarmonyOS應用開發(fā)

2021-02-23 09:52:42

鴻蒙HarmonyOS應用開發(fā)

2021-03-02 09:29:29

鴻蒙HarmonyOS應用開發(fā)

2021-02-25 15:13:08

鴻蒙HarmonyOS應用開發(fā)

2021-02-23 12:23:57

鴻蒙HarmonyOS應用開發(fā)

2021-02-21 11:09:18

鴻蒙HarmonyOS應用開發(fā)

2021-02-22 14:56:55

鴻蒙HarmonyOS應用開發(fā)

2021-02-25 10:01:19

鴻蒙HarmonyOS應用開發(fā)

2021-02-04 13:49:41

鴻蒙HarmonyOS應用開發(fā)

2021-02-05 09:46:16

鴻蒙HarmonyOSjs開發(fā)

2021-02-07 09:17:24

鴻蒙HarmonyOS應用開發(fā)

2021-02-24 09:36:03

鴻蒙CSS應用開發(fā)

2016-09-27 16:38:24

JavaScript微信Web

2016-11-04 10:49:48

微信小程序

2017-05-08 15:03:07

微信小程序開發(fā)實戰(zhàn)

2016-09-28 18:10:59

微信程序MINA

2016-09-27 20:36:23

微信HttpWeb

2016-11-04 10:30:17

微信小程序

2018-09-11 10:32:07

云開發(fā)小程序開發(fā)者

2023-01-05 09:01:05

UI組件庫微信
點贊
收藏

51CTO技術棧公眾號