gpt4 book ai didi

javascript - 隐藏表行直到搜索(filterBy)完成

转载 作者:搜寻专家 更新时间:2023-10-30 22:48:32 24 4
gpt4 key购买 nike

我正在使用 Vue.js(目前为 v.1.x,将很快更新),我想要一个包含一些数据的表行表。我正在使用 Vue.js 组件通过 filterBy 过滤表行,但我希望在完成任何搜索/过滤之前隐藏表行。

有人可以帮我解决这个问题吗?我应该在某处放置某种事件监听器吗?放在哪里最好?

这是一个 JSFiddle:https://jsfiddle.net/m7sgaron/863/

Vue.component('demo-grid', {
template: '#grid-template',
props: {
data: Array,
columns: Array,
filterKey: String
},
data: function () {
var sortOrders = {}
this.columns.forEach(function (key) {
sortOrders[key] = 1
})
return {
sortKey: '',
sortOrders: sortOrders
}
},
methods: {
sortBy: function (key) {
this.sortKey = key
this.sortOrders[key] = this.sortOrders[key] * -1
}
}
})

// bootstrap the demo
var demo = new Vue({
el: '#demo',
data: {
searchQuery: '',
gridColumns: ['name', 'power'],
gridData: [
{ name: 'Chuck Norris', power: Infinity },
{ name: 'Bruce Lee', power: 9000 },
{ name: 'Jackie Chan', power: 7000 },
{ name: 'Jet Li', power: 8000 }
]
}
})
body {
font-family: Helvetica Neue, Arial, sans-serif;
font-size: 14px;
color: #444;
}

table {
border: 2px solid #42b983;
border-radius: 3px;
background-color: #fff;
}

th {
background-color: #42b983;
color: rgba(255,255,255,0.66);
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-user-select: none;
}

td {
background-color: #f9f9f9;
}

th, td {
min-width: 120px;
padding: 10px 20px;
}

th.active {
color: #fff;
}

th.active .arrow {
opacity: 1;
}

.arrow {
display: inline-block;
vertical-align: middle;
width: 0;
height: 0;
margin-left: 5px;
opacity: 0.66;
}

.arrow.asc {
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-bottom: 4px solid #fff;
}

.arrow.dsc {
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 4px solid #fff;
}

#search {
margin-bottom: 10px;
}
<script src="https://cdn.jsdelivr.net/vue/1.0.24/vue.js"></script>
<!-- component template -->
<script type="text/x-template" id="grid-template">
<table>
<tbody>
<tr v-for="
entry in data
| filterBy filterKey
| orderBy sortKey sortOrders[sortKey]">
<td v-for="key in columns">
{{entry[key]}}
</td>
</tr>
</tbody>
</table>
</script>

<!-- demo root element -->
<div id="demo">
<form id="search">
Search <input name="query" v-model="searchQuery">
</form>
<demo-grid
:data="gridData"
:columns="gridColumns"
:filter-key="searchQuery">
</demo-grid>
</div>

最佳答案

最简单的方法是在 tr 上使用条件指令,例如 v-showv-if。表达式将检查 code>searchQuery 有一些长度。

<script type="text/x-template" id="grid-template">
<table>
<tbody>
<tr v-show="filterKey.length" v-for="
entry in data
| filterBy filterKey
| orderBy sortKey sortOrders[sortKey]">
<td v-for="key in columns">
{{entry[key]}}
</td>
</tr>
</tbody>
</table>
</script>

工作演示:https://jsfiddle.net/zp0m0om6/2/

关于javascript - 隐藏表行直到搜索(filterBy)完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44203034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com