跳过 v-for vuejs2 的第一个结果
Skip first result from v-for vuejs2
我正在使用 laravel 5.5 以及 vuejs2 和 lodash 项目。我想跳过结果中的第一个数据,如下图所示。这是我的 vuejs2 代码。
new Vue({
el:'#users',
data:{
message:'',
ok:false,
noresult:false,
arrayresults: [{id:'' ,username: '',useremail: '',userphone:'',}],
},
methods:{
searchData: _.debounce(function(){
if(this.message != '')
{
this.noresult = false;
this.arrayresults = [{id:'' ,username: '',useremail: '',userphone:'',}],
$.ajax({
type:'POST',
url: path+'usersearch',
data: {data:this.message},
success:(data) => {
if(data.length >= 1)
{
for(i = 0;i<data.length;i++)
{
this.arrayresults.push({id:data[i]['id'],username:data[i]['user_name'],useremail:data[i]['user_email'],userphone:data[i]['user_phone']})
}
this.ok = true;
}
else
{
this.ok = false;
this.noresult = true;
}
},
error:function()
{
console.log("error");
}
});
}
else
{
this.ok = false;
this.arrayresults = [{id:'' ,username: '',useremail: '',userphone:'',}];
}
},1000)
}
});
这是我的 laravel blade 代码:
<div v-if="ok" id='search-result' v-cloak>
<table class='table table-responsive thead-text' border='5'>
<thead>
<tr class='success'>
<td>{{trans('language.user_name')}}</td>
<td>{{trans('language.user_phone')}}</td>
<td>{{trans('language.user_email')}}</td>
<td>{{trans('language.settings')}}</td>
</tr>
</thead>
<tbody>
<tr v-for='(arrayresult ,key ,id) in arrayresults' class='warning'>
<td>@{{arrayresult.username}}</td>
<td>@{{arrayresult.userphone}}</td>
<td>@{{arrayresult.useremail}}</td>
<td class='hidden-print'>
<a v-bind:href="'/{{$path}}/users/' + arrayresult.id" class='btn btn-success'>{{trans('language.show')}}</a>
@can('users.update')<a v-bind:href="'/{{$path}}/users/' + arrayresult.id + '/edit'" class='btn btn-info'>{{trans('language.edit')}}</a>@endcan
</td>
</tr>
</tbody>
</table>
</div>
到目前为止一切正常,除了当我像这样设置数组时第一个值看起来为空而没有结果:
this.arrayresults = [{id:'' ,username: '',useremail: '',userphone:'',}],
结果是这样的:
我想从搜索中删除第一个空值。
改变
this.arrayresults = [{id:'' ,username: '',useremail: '',userphone:'',}]
至
this.arrayresults = []
使用 v-for
+ v-if
(参见 guide)
When they exist on the same node, v-for has a higher priority than v-if. That means the v-if will be run on each iteration of the loop separately. This can be useful when you want to render nodes for only some items, like below:
更新:
添加了更好的方法
示例:
new Vue({
el: '#app',
computed: {
filteredArray() {
return this.array.filter(item => !!item.firstName);
},
},
data: {
array: [
{
firstName: '',
lastName: '',
age: 0
},
{
firstName: 'Dirk',
lastName: 'Doe',
age: 41
},
{
firstName: 'Julia',
lastName: 'Doe',
age: 25
}
]
}
})
<script src="https://unpkg.com/vue@2.6.11/dist/vue.js"></script>
<div id="app">
<li v-for="user in filteredArray">
{{ user.firstName }} - age: {{user.age}}
</li>
<pre>old array = {{ array }}</pre>
</div>
Voldymyr 写了什么
<li v-for="(value, index) in array" v-if="value.firstName">
{{ value.firstName }} - index: {{index}}
</li>
似乎可以排除名字为空或为空的数据。
但是为了避免第一个记录(这就是你问的),我能想到的最好的方法是,
<li v-for="(value, index) in array" v-if="index">
{{ value.firstName }} - index: {{index}}
<li>
第一条记录的索引将为0,v-if(index)
将为false
。
我正在使用 laravel 5.5 以及 vuejs2 和 lodash 项目。我想跳过结果中的第一个数据,如下图所示。这是我的 vuejs2 代码。
new Vue({
el:'#users',
data:{
message:'',
ok:false,
noresult:false,
arrayresults: [{id:'' ,username: '',useremail: '',userphone:'',}],
},
methods:{
searchData: _.debounce(function(){
if(this.message != '')
{
this.noresult = false;
this.arrayresults = [{id:'' ,username: '',useremail: '',userphone:'',}],
$.ajax({
type:'POST',
url: path+'usersearch',
data: {data:this.message},
success:(data) => {
if(data.length >= 1)
{
for(i = 0;i<data.length;i++)
{
this.arrayresults.push({id:data[i]['id'],username:data[i]['user_name'],useremail:data[i]['user_email'],userphone:data[i]['user_phone']})
}
this.ok = true;
}
else
{
this.ok = false;
this.noresult = true;
}
},
error:function()
{
console.log("error");
}
});
}
else
{
this.ok = false;
this.arrayresults = [{id:'' ,username: '',useremail: '',userphone:'',}];
}
},1000)
}
});
这是我的 laravel blade 代码:
<div v-if="ok" id='search-result' v-cloak>
<table class='table table-responsive thead-text' border='5'>
<thead>
<tr class='success'>
<td>{{trans('language.user_name')}}</td>
<td>{{trans('language.user_phone')}}</td>
<td>{{trans('language.user_email')}}</td>
<td>{{trans('language.settings')}}</td>
</tr>
</thead>
<tbody>
<tr v-for='(arrayresult ,key ,id) in arrayresults' class='warning'>
<td>@{{arrayresult.username}}</td>
<td>@{{arrayresult.userphone}}</td>
<td>@{{arrayresult.useremail}}</td>
<td class='hidden-print'>
<a v-bind:href="'/{{$path}}/users/' + arrayresult.id" class='btn btn-success'>{{trans('language.show')}}</a>
@can('users.update')<a v-bind:href="'/{{$path}}/users/' + arrayresult.id + '/edit'" class='btn btn-info'>{{trans('language.edit')}}</a>@endcan
</td>
</tr>
</tbody>
</table>
</div>
到目前为止一切正常,除了当我像这样设置数组时第一个值看起来为空而没有结果:
this.arrayresults = [{id:'' ,username: '',useremail: '',userphone:'',}],
结果是这样的:
我想从搜索中删除第一个空值。
改变
this.arrayresults = [{id:'' ,username: '',useremail: '',userphone:'',}]
至
this.arrayresults = []
使用 v-for
+ v-if
(参见 guide)
When they exist on the same node, v-for has a higher priority than v-if. That means the v-if will be run on each iteration of the loop separately. This can be useful when you want to render nodes for only some items, like below:
更新: 添加了更好的方法
示例:
new Vue({
el: '#app',
computed: {
filteredArray() {
return this.array.filter(item => !!item.firstName);
},
},
data: {
array: [
{
firstName: '',
lastName: '',
age: 0
},
{
firstName: 'Dirk',
lastName: 'Doe',
age: 41
},
{
firstName: 'Julia',
lastName: 'Doe',
age: 25
}
]
}
})
<script src="https://unpkg.com/vue@2.6.11/dist/vue.js"></script>
<div id="app">
<li v-for="user in filteredArray">
{{ user.firstName }} - age: {{user.age}}
</li>
<pre>old array = {{ array }}</pre>
</div>
Voldymyr 写了什么
<li v-for="(value, index) in array" v-if="value.firstName">
{{ value.firstName }} - index: {{index}}
</li>
似乎可以排除名字为空或为空的数据。 但是为了避免第一个记录(这就是你问的),我能想到的最好的方法是,
<li v-for="(value, index) in array" v-if="index">
{{ value.firstName }} - index: {{index}}
<li>
第一条记录的索引将为0,v-if(index)
将为false
。