vue基于兩個(gè)計(jì)算屬性實(shí)現(xiàn)選中和全選功能示例
本文實(shí)例講述了vue基于兩個(gè)計(jì)算屬性實(shí)現(xiàn)選中和全選功能。分享給大家供大家參考,具體如下:
還是選中和全選功能,用兩個(gè)計(jì)算屬性來實(shí)現(xiàn),別人的代碼,思維確實(shí)不一樣。學(xué)習(xí)了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>chabaoo.cn vue選中和全選</title>
<style>
table, td, th{
border:1px solid #ebebeb;
border-collapse:collapse;
text-align: center;
}
table {
width:500px;
}
</style>
<script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
</head>
<body>
<div class="app">
<table>
<thead>
<tr>
<th style="line-height: 38px">選中數(shù)
{{checkedCount}}<input type="checkbox" v-model="allchecked" style="zoom:200%;vertical-align: middle">
</th>
<th>name</th>
<th>age</th>
</tr>
</thead>
<tbody>
<tr v-for="item in list">
<td><input type="checkbox" v-model="item.checked" style="zoom:200%"></td>
<td>{{item.name}}</td>
<td>{{item.age}}</td>
</tr>
</tbody>
</table>
</div>
<script>
var list=[
{
name:'小明',
age: 23,
checked: false
},
{
name:'小紅',
age: 2,
checked: true
},
{
name:'小藍(lán)',
age: 23,
checked: true
},
{
name:'小bai',
age: 40,
checked: true
},
{
name:'王小二',
age: 18,
checked: false
}
]
new Vue({
el: '.app',
data: {
list
},
computed: {
allchecked: {
// getter
get: function () {
return this.list.length == this.checkedCount
},
// setter
set: function (val) {
//val就是點(diǎn)擊之后,全選按鈕的v-model值(狀態(tài)),勾上后就是val的值就是true。未勾上就是false
console.log(val)
this.list.forEach(item => {
item.checked = val
})
}
},
checkedCount: {
// getter
get: function () {
var i = 0
this.list.forEach(item => {
if (item.checked === true) i++
})
return i
}
}
}
})
</script>
</body>
</html>
使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼,可得如下運(yùn)行效果:

希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
- Vue computed 計(jì)算屬性代碼實(shí)例
- vue計(jì)算屬性+vue中class與style綁定(推薦)
- Vue學(xué)習(xí)筆記之計(jì)算屬性與偵聽器用法
- vue計(jì)算屬性無法監(jiān)聽到數(shù)組內(nèi)部變化的解決方案
- Vue.js計(jì)算機(jī)屬性computed和methods方法詳解
- vue計(jì)算屬性computed的使用方法示例
- vue計(jì)算屬性get和set用法示例
- vue計(jì)算屬性computed、事件、監(jiān)聽器watch的使用講解
- Vue中使用方法、計(jì)算屬性或觀察者的方法實(shí)例詳解
- vue中的計(jì)算屬性實(shí)例詳解
- Vue條件循環(huán)判斷+計(jì)算屬性+綁定樣式v-bind的實(shí)例
- 詳解Vue之計(jì)算屬性
相關(guān)文章
Vue3新屬性之css中使用v-bind的方法(v-bind?in?css)
這篇文章主要介紹了Vue3新屬性css中使用v-bind(v-bind?in?css)的方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
解決vue+webpack項(xiàng)目接口跨域出現(xiàn)的問題
這篇文章主要介紹了解決vue+webpack項(xiàng)目接口跨域出現(xiàn)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
讓axios發(fā)送表單請(qǐng)求形式的鍵值對(duì)post數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇讓axios發(fā)送表單請(qǐng)求形式的鍵值對(duì)post數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
關(guān)于Vue?CLI3中啟動(dòng)cli服務(wù)參數(shù)說明
這篇文章主要介紹了關(guān)于Vue?CLI3中啟動(dòng)cli服務(wù)參數(shù)說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue結(jié)合Element-Plus封裝遞歸組件實(shí)現(xiàn)目錄示例
本文主要介紹了Vue結(jié)合Element-Plus封裝遞歸組件實(shí)現(xiàn)目錄示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue在mounted中window.onresize不生效問題及解決
這篇文章主要介紹了vue中在mounted中window.onresize不生效問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04

