Searchable Table
This recipe demonstrates how to implement a real-time searchable table using pocket-vue.
Example Code
html
<div v-scope="{
query: '',
users: [
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' },
{ id: 3, name: 'Charlie', email: 'charlie@example.com' }
],
get filteredUsers() {
return this.users.filter(user =>
user.name.toLowerCase().includes(this.query.toLowerCase()) ||
user.email.toLowerCase().includes(this.query.toLowerCase())
);
}
}">
<div class="search-container">
<input v-model="query" placeholder="Search by name or email...">
<table v-if="filteredUsers.length">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr v-for="user in filteredUsers" :key="user.id">
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
</tr>
</tbody>
</table>
<p v-else>No users found matching "{{ query }}".</p>
</div>
</div>How it works
- Reactive State: We define a
querystring and an array ofusersin our scope. - Computed Property: We use the
filteredUsersgetter to automatically calculate the filtered list of users whenever thequeryorusersdata changes. - Two-way Binding: We use
v-model="query"to bind the search input to thequeryproperty. - List Rendering: We use
v-for="user in filteredUsers"to render only the matched users in the table. - Conditional Rendering: We use
v-ifto show the table if any users match the search, and a fallback message if no matches are found. - Optimized Rendering: By using
:key="user.id", we ensure that pocket-vue efficiently updates only the necessary parts of the DOM when the filtered results change.
