Element-UI+Vue模式使用總結(jié)
項目框架
Element-ui+Vue+jQuery+Bootstrap+Echarts。
嵌入vue使用的是<script>,沒有使用vue-cli,請自行將<template>內(nèi)代碼貼入html,<style>內(nèi)代碼貼入樣式表。
checkbox全選和全不選
<template>
<el-form-item label="地電阻率選項:">
<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單項
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)聽checkbox是否全部選擇
"searchItem.eid": function() {
if (this.searchItem.eid.length == this.eidList.length) {
this.eidAll = true
} else {
this.eidAll = false
}
}
}
});
</script>
表頭固定,表身滾動
方案①:el-table,卡死,據(jù)說和vue版本有關(guān)系,但是升級了仍然卡死,拋棄。
方案②: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來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>
表格滾動無限加載
可以用插件,但為了輕量就自己寫吧,此處用jQuery。
<script>
var app = new Vue({
el: '#app',
mounted: function() {
// 監(jiān)聽滾動
this.handleScrollLoad();
},
data: {
// 加載完全部數(shù)據(jù),更換查詢條件時請先初始化為false
loadAll: false,
// 頁碼,更換查詢條件時請先初始化為1
offset: 1,
// 表格數(shù)據(jù),更換查詢條件時請先清空
tableData: []
},
methods: {
// 處理滾動加載
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遞增
// 因為我們后端定義的offset其實是page,代表第幾頁,而不是真正意義上的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) {
// 請求正常,判斷是否加載完全部
if (res.data.rows.length === 0) {
$this.loadAll = true;
return;
}
for (let i of res.data.rows) {
$this.tableData.push(i);
}
} else {
// 請求錯誤
alert('請求錯誤,錯誤碼:' + res.status);
}
}, e => {
this.loading = false;
throw new Error('請求失?。? + e);
})
}
}
});
</script>
多個echarts
既然使用了vue,嵌入echarts最好的方式當(dāng)然是組件,將echarts封裝成組件,再通過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: []
}
});
/*****************************曲線實例****************************/
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>
后記
使用這個框架做項目斷斷續(xù)續(xù)也做了很久了,一直都沒有特意去總結(jié),導(dǎo)致每次都要翻從前的代碼,回憶良久,例如el-checkbox,不同于其他表單項,它的label才是真正的value,每次都要重新查閱文檔+回憶,其實是很費時的。
總結(jié)項目套路是很有必要的,我覺得隨著工作時間增長,一個人是進(jìn)步,還是重復(fù)工作,和會不會總結(jié)有本質(zhì)聯(lián)系。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue自定義指令實現(xiàn)彈窗拖拽四邊拉伸及對角線拉伸效果
小編最近在做一個vue前端項目,需要實現(xiàn)彈窗的拖拽,四邊拉伸及對角線拉伸,以及彈窗邊界處理功能,本文通過實例代碼給大家分享我的實現(xiàn)過程及遇到問題解決方法,感興趣的朋友一起看看吧2021-08-08
Vue全局事件總線$bus安裝與應(yīng)用小結(jié)
這篇文章主要介紹了Vue全局事件總線$bus安裝與應(yīng)用,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09
利用Vue+intro.js實現(xiàn)頁面新手引導(dǎo)流程功能
在同學(xué)們使用某些網(wǎng)站的新版本頁面的時候,經(jīng)常會出現(xiàn)一個類似于新手引導(dǎo)一樣的效果,來幫助同學(xué)們更好的熟悉新版本頁面的功能和使用,這篇文章主要給大家介紹了關(guān)于如何利用Vue+intro.js實現(xiàn)頁面新手引導(dǎo)流程功能的相關(guān)資料,需要的朋友可以參考下2023-11-11
手把手教你寫一個vue全局注冊的Toast的實現(xiàn)
本文主要介紹了手把手教你寫一個vue全局注冊的Toast的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
vue中render函數(shù)和h函數(shù)以及jsx的使用方式
這篇文章主要介紹了vue中render函數(shù)和h函數(shù)以及jsx的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue render渲染時間戳轉(zhuǎn)時間,時間轉(zhuǎn)時間戳及渲染進(jìn)度條效果
這篇文章主要介紹了Vue render渲染時間戳轉(zhuǎn)時間,時間轉(zhuǎn)時間戳及渲染進(jìn)度條效果,通過實例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07

