答案 0 :(得分:2)
这里有一个选项,它使用简单的列表来满足您的需求,这似乎更适合您的情况;
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<table class="table">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
<button onclick="myPrintFunction()">Click me</button>
<script>
function myPrintFunction() {
let divToPrint = document.getElementsByClassName("table")[0];
if (typeof divToPrint !== 'undefined') {
let newWin = window.open();
newWin.document.write('<h1>PRINT FRIENDLY</h1>');
newWin.document.write(divToPrint.outerHTML);
newWin.document.close();
newWin.focus();
setTimeout(()=>{
newWin.print();
newWin.close();
}, 1000);
} else {
alert('NO EVENTS TO PRINT!');
}
}
</script>
</body>
</html>
new Vue({
el: "#app",
data: {
arr: ['1','2','3','4'],
removed: ""
},
methods: {
splice (item, index) {
this.removed = item
this.arr.splice(index, 1)
}
}
})
ul {
border: 1px solid black;
overflow-y: scroll;
height: 55px;
width: 50px;
padding: 0px;
}
li {
list-style-type: none;
}
答案 1 :(得分:2)
另一种解决方案是使用watch
属性来监视selected
项目并将其从数组中删除:
new Vue({
el: "#app",
data: {
arr: ['1', '2', '3', '4'],
selected: ""
},
methods: {
},
watch: {
selected(v) {
if (this.selected !== "") {
this.arr.splice(this.arr.indexOf(v), 1)
}
this.selected=""
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<select v-model="selected">
<option v-for="item in arr">{{ item }}</option>
</select>
<div>
答案 2 :(得分:1)
如果您有一个默认值为null的默认选项,则可以通过重新分配this.selected = null
然后拼接选项数组来工作:
new Vue({
el: "#app",
data() {
return {
arr: ['1', '2', '3', '4'],
selected: null
}
},
methods: {
removeElement(e) {
this.selected = null
this.arr.splice(this.arr.indexOf(e.target.value), 1)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<select v-model="selected" @change="removeElement">
<option :value="null" disabled>-- Select --</option>
<option v-for="item in arr">{{ item }}</option>
</select>
</div>