Element-UI+Vue模式使用總結(jié)
項(xiàng)目框架
Element-ui+Vue+jQuery+Bootstrap+Echarts。
嵌入vue使用的是<script>,沒有使用vue-cli,請自行將<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)聽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ù)說和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來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)無限加載
可以用插件,但為了輕量就自己寫吧,此處用jQuery。
<script>
var app = new Vue({
el: '#app',
mounted: function() {
// 監(jiān)聽滾動(dòng)
this.handleScrollLoad();
},
data: {
// 加載完全部數(shù)據(jù),更換查詢條件時(shí)請先初始化為false
loadAll: false,
// 頁碼,更換查詢條件時(shí)請先初始化為1
offset: 1,
// 表格數(shù)據(jù),更換查詢條件時(shí)請先清空
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,代表第幾頁,而不是真正意義上的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 {
// 請求錯(cuò)誤
alert('請求錯(cuò)誤,錯(cuò)誤碼:' + res.status);
}
}, e => {
this.loading = false;
throw new Error('請求失?。? + e);
})
}
}
});
</script>
多個(gè)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: []
}
});
/*****************************曲線實(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ù)也做了很久了,一直都沒有特意去總結(jié),導(dǎo)致每次都要翻從前的代碼,回憶良久,例如el-checkbox,不同于其他表單項(xiàng),它的label才是真正的value,每次都要重新查閱文檔+回憶,其實(shí)是很費(fèi)時(shí)的。
總結(jié)項(xiàng)目套路是很有必要的,我覺得隨著工作時(shí)間增長,一個(gè)人是進(jìn)步,還是重復(fù)工作,和會(huì)不會(huì)總結(jié)有本質(zhì)聯(lián)系。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一文掌握Pinia使用及數(shù)據(jù)持久化存儲(chǔ)超詳細(xì)教程
這篇文章主要介紹了Pinia安裝使用及數(shù)據(jù)持久化存儲(chǔ)的超詳細(xì)教程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
vue?當(dāng)中組件之間共享數(shù)據(jù)的實(shí)現(xiàn)方式
這篇文章主要介紹了vue?當(dāng)中組件之間共享數(shù)據(jù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
分析 Vue 中的 computed 和 watch 的區(qū)別
這篇文章分析 Vue 的 computed 和 watch 的區(qū)別,computed 用來監(jiān)控自己定義的變量,頁面上可直接使用。watch 是監(jiān)測 Vue 實(shí)例上的數(shù)據(jù)變動(dòng),通俗地講,就是檢測 data 內(nèi)聲明的數(shù)據(jù),需要的朋友可以參考一下2021-09-09
vue通過vue-lazyload實(shí)現(xiàn)圖片懶加載的代碼詳解
這篇文章主要給大家介紹了vue通過vue-lazyload實(shí)現(xiàn)圖片懶加載,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02
antd?Vue實(shí)現(xiàn)Login登錄頁面布局案例詳解?附帶驗(yàn)證碼驗(yàn)證功能
這篇文章主要介紹了antd?Vue實(shí)現(xiàn)Login登錄頁面布局案例詳解附帶驗(yàn)證碼驗(yàn)證功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
Vue3進(jìn)階主題Composition API使用詳解
這篇文章主要為大家介紹了Vue3進(jìn)階主題Composition API使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
用Vue.js在瀏覽器中實(shí)現(xiàn)裁剪圖像功能
在本教程中,我們將探討如何在瀏覽器中使用 JavaScript 庫來操作圖片,為服務(wù)器上的存儲(chǔ)做準(zhǔn)備,并在 Web 程序中使用。我們將使用 Vue.js 而不是原生 JavaScript來完成此操作,需要的朋友可以參考下2019-06-06
Vue-element-admin平臺(tái)側(cè)邊欄收縮控制問題
這篇文章主要介紹了Vue-element-admin平臺(tái)側(cè)邊欄收縮控制問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

