Element-UI+Vue模式使用總結(jié)
項(xiàng)目框架
Element-ui+Vue+jQuery+Bootstrap+Echarts。
嵌入vue使用的是<script>,沒(méi)有使用vue-cli,請(qǐng)自行將<template>內(nèi)代碼貼入html,<style>內(nèi)代碼貼入樣式表。
checkbox全選和全不選
<template> <el-form-item label="地電阻率選項(xiàng):"> <el-checkbox class="search_item" v-model="eidAll" @change="handleEidAllChange">全選</el-checkbox> <el-checkbox-group v-model="searchItem.eid"> <el-checkbox class="search_item" v-for="item of eidList" :label="item.value">{{ item.name }}</el-checkbox> </el-checkbox-group> </el-form-item> </template> <script> var app = new Vue({ el: '#app', data: { // 全選變量 eidAll: false // checkbox單項(xiàng) searchItem: { eid: [], }, // checkbox數(shù)據(jù)循環(huán) eidList: [{ name: '缺數(shù)', value: 'DZ1' // ... }] }, methods: { // 處理全選 handleEidAllChange() { if (this.eidAll) { this.searchItem.eid = []; var arr = []; for (let i in this.eidList) { arr.push(this.eidList[i].value); } this.searchItem.eid = arr; } else { this.searchItem.eid = []; } }, }, watch: { // 監(jiān)聽(tīng)checkbox是否全部選擇 "searchItem.eid": function() { if (this.searchItem.eid.length == this.eidList.length) { this.eidAll = true } else { this.eidAll = false } } } }); </script>
表頭固定,表身滾動(dòng)
方案①:el-table,卡死,據(jù)說(shuō)和vue版本有關(guān)系,但是升級(jí)了仍然卡死,拋棄。
方案②:table,要設(shè)置display:table; table-layout: fixed;,布局有局限性。
方案③:div+el-col模擬table。
<template> <div class="table"> <div class="thead"> <div class="tr"> <el-row> <el-col v-for="item of tableHeadList" :span="item.col"> <div class="th"> {{ item.text }} </div> </el-col> </el-row> </div> </div> <div class="tbody"> <div class="tr" v-for="(item, index) of tableData"> <el-row> <el-col v-for="bodyItem of tableBodyList" :span="bodyItem.col"> <div class="td"> {{ item[bodyItem.field] }} </div> </el-col> </el-row> </div> </div> </div> </template> <style> .table .tbody { width: 100%; height: 278px; overflow-y: scroll; } </style> <script> var app = new Vue({ el: '#app', data: { // th數(shù)據(jù)循環(huán) tableHeadList: [{ // 根據(jù)type來(lái)v-if th的標(biāo)題內(nèi)容,根據(jù)需求放文本或checkbox type: 'text', // 每格占用柵格,element-ui總柵格數(shù)是24 col: '1', // th標(biāo)題 text: 'ID' }], // td數(shù)據(jù)循環(huán) tableBodyList: [{ type: 'text', col: '1', // 接口返回字段 field: 'id' }], // 表格數(shù)據(jù) tableData: [...] } }); </script>
表格滾動(dòng)無(wú)限加載
可以用插件,但為了輕量就自己寫(xiě)吧,此處用jQuery。
<script> var app = new Vue({ el: '#app', mounted: function() { // 監(jiān)聽(tīng)滾動(dòng) this.handleScrollLoad(); }, data: { // 加載完全部數(shù)據(jù),更換查詢條件時(shí)請(qǐng)先初始化為false loadAll: false, // 頁(yè)碼,更換查詢條件時(shí)請(qǐng)先初始化為1 offset: 1, // 表格數(shù)據(jù),更換查詢條件時(shí)請(qǐng)先清空 tableData: [] }, methods: { // 處理滾動(dòng)加載 handleScrollLoad() { var $this = this var nScrollHight = 0; var nScrollTop = 0; var nDivHight = $(".table .tbody").height(); $(".table .tbody").scroll(function() { if ($this.loadAll) { // 全部加載完不進(jìn)行操作 return; } nScrollHight = $(this)[0].scrollHeight; nScrollTop = $(this)[0].scrollTop; if (nScrollTop + nDivHight >= nScrollHight) { // 滑到底部,offset遞增 // 因?yàn)槲覀兒蠖硕x的offset其實(shí)是page,代表第幾頁(yè),而不是真正意義上的offset // 有需要的人可以轉(zhuǎn)為$this.offset += $this.limit; $this.offset += 1; $this.searchData() } }); }, // 查詢表格數(shù)據(jù) searchData() { ... var $this = this axios.get(str) .then(res => { if (res.status === 200) { // 請(qǐng)求正常,判斷是否加載完全部 if (res.data.rows.length === 0) { $this.loadAll = true; return; } for (let i of res.data.rows) { $this.tableData.push(i); } } else { // 請(qǐng)求錯(cuò)誤 alert('請(qǐng)求錯(cuò)誤,錯(cuò)誤碼:' + res.status); } }, e => { this.loading = false; throw new Error('請(qǐng)求失?。? + e); }) } } }); </script>
多個(gè)echarts
既然使用了vue,嵌入echarts最好的方式當(dāng)然是組件,將echarts封裝成組件,再通過(guò)v-for循環(huán),每次數(shù)據(jù)更新再setOption。
<template> <div class="echarts_box"> <charts v-for="(item, index) of echartsData" :item="item"></charts> </div> </template> <script> var app = new Vue({ el: '#app', data: { // 曲線數(shù)據(jù) echartsData: [] } }); /*****************************曲線實(shí)例****************************/ Vue.component('charts', { props: { item: Object }, methods: { // 初始化曲線 initChart() { this['echart' + (this.item.id)] = echarts.init(document.getElementById('echart' + this.item.id)); this.setChart(); }, setChart() { var $this = this let option = { ... }; this['echart' + this.item.id].setOption(option); } }, mounted() { this.initChart(); }, watch: { item: { handler: function () { this.setChart(); }, deep: true } }, template: `<div class="echart_item" :id="'echart'+item.id" style="height:260px;"></div>` }); </script>
后記
使用這個(gè)框架做項(xiàng)目斷斷續(xù)續(xù)也做了很久了,一直都沒(méi)有特意去總結(jié),導(dǎo)致每次都要翻從前的代碼,回憶良久,例如el-checkbox,不同于其他表單項(xiàng),它的label才是真正的value,每次都要重新查閱文檔+回憶,其實(shí)是很費(fèi)時(shí)的。
總結(jié)項(xiàng)目套路是很有必要的,我覺(jué)得隨著工作時(shí)間增長(zhǎng),一個(gè)人是進(jìn)步,還是重復(fù)工作,和會(huì)不會(huì)總結(jié)有本質(zhì)聯(lián)系。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:JS實(shí)現(xiàn)導(dǎo)航欄樓層特效
欄 目:JavaScript
下一篇:Angular8 實(shí)現(xiàn)table表格表頭固定效果
本文標(biāo)題:Element-UI+Vue模式使用總結(jié)
本文地址:http://mengdiqiu.com.cn/a1/JavaScript/9326.html
您可能感興趣的文章
- 04-02包含javascript舍的詞條
- 01-10使用webpack/gulp構(gòu)建TypeScript項(xiàng)目的方法示例
- 01-10使用JS來(lái)動(dòng)態(tài)操作css的幾種方法
- 01-10在Vue項(xiàng)目中使用Typescript的實(shí)現(xiàn)
- 01-10Vue中使用Lodop插件實(shí)現(xiàn)打印功能的簡(jiǎn)單方法
- 01-10echarts實(shí)現(xiàn)折線圖的拖拽效果
- 01-10JS數(shù)據(jù)類型STRING使用實(shí)例解析
- 01-10node使用request請(qǐng)求的方法
- 01-10angularjs模態(tài)框的使用代碼實(shí)例
- 01-10使用JavaScript計(jì)算前一天和后一天的思路詳解


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02javascript點(diǎn)線,點(diǎn)線的代碼
- 04-02javascript潛力,javascript強(qiáng)大嗎
- 04-02javascript替換字符串,js字符串的替換
- 04-02javascript移出,js 移入移出
- 04-02包含javascript舍的詞條
- 04-02javascript并行,深入理解并行編程 豆瓣
- 04-02javascript匿名,js匿名方法
- 04-02javascript警報(bào),JavaScript警告
- 04-02javascript遮蓋,JavaScript遮蓋PC端頁(yè)面
- 04-02javascript前身,javascript的前身
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)