Dynamic Management of Search Items β
Demonstrates all programmatic management methods, including adding, removing, and modifying search items, suitable for scenarios requiring dynamic adjustment of search conditions based on business logic.
Dynamic Management Demo β
Dynamic Management Methods β
Adding Search Items β
Use the appendItem
method to dynamically add new search items:
// Add a single search item
const addSearchItem = () => {
searchRef.value?.appendItem({
label: 'New Field',
prop: 'new_field',
render: 'input',
props: {
placeholder: 'Please enter new field value'
}
})
}
2
3
4
5
6
7
8
9
10
11
Removing Search Items β
Use the removeItem
method to remove specified search items:
// Remove specified search item
const removeSearchItem = (prop: string) => {
searchRef.value?.removeItem(prop)
}
2
3
4
Batch Setting Search Items β
Use the setItems
method to set all search items at once:
// Batch update search items
const updateAllItems = () => {
const newItems = [
{ label: 'Username', prop: 'username', render: 'input' },
{ label: 'Status', prop: 'status', render: 'select', options: statusOptions }
]
searchRef.value?.setItems(newItems)
}
2
3
4
5
6
7
8
Retrieving Search Item Information β
Use various retrieval methods to query current search item configurations:
// Get all search items
const allItems = searchRef.value?.getItems()
// Get specific search item
const userItem = searchRef.value?.getItemByProp('username')
// Check if search item exists
const hasUserItem = !!searchRef.value?.getItemByProp('username')
2
3
4
5
6
7
8
Usage Scenarios β
1. Permission Control β
Dynamically display different search conditions based on user permissions:
// Admin sees all search items, regular users see partial items
if (userRole === 'admin') {
searchRef.value?.appendItem({
label: 'Creator',
prop: 'creator',
render: 'select',
options: userOptions
})
}
2
3
4
5
6
7
8
9
2. Business Scenario Switching β
Switch between different search condition combinations based on business scenarios:
// Switch business scenario
const switchScenario = (scenario: string) => {
let items = []
switch (scenario) {
case 'order':
items = orderSearchItems
break
case 'user':
items = userSearchItems
break
case 'product':
items = productSearchItems
break
}
searchRef.value?.setItems(items)
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3. Conditional Dependencies β
Implement dependencies between search conditions:
// When selecting a category, dynamically add subcategory search item
const onCategoryChange = (categoryId: string) => {
if (categoryId) {
searchRef.value?.appendItem({
label: 'Subcategory',
prop: 'subcategory',
render: 'select',
options: getSubcategories(categoryId)
})
} else {
searchRef.value?.removeItem('subcategory')
}
}
2
3
4
5
6
7
8
9
10
11
12
13
Key Features β
- π§ Full CRUD operation support
- π Real-time search item management
- π Comprehensive query and retrieval methods
- π― Support for complex business logic
- β‘ High-performance dynamic update mechanism
Advanced Usage β
Search Item Template System β
Create reusable search item templates:
// Define search item templates
const templates = {
userSearch: [
{ label: 'Username', prop: 'username', render: 'input' },
{ label: 'Email', prop: 'email', render: 'input' },
{ label: 'Status', prop: 'status', render: 'select', options: statusOptions }
],
orderSearch: [
{ label: 'Order No.', prop: 'order_no', render: 'input' },
{ label: 'Order Status', prop: 'order_status', render: 'select', options: orderStatusOptions },
{ label: 'Order Time', prop: 'created_at', render: 'date-picker' }
]
}
// Apply template
const applyTemplate = (templateName: string) => {
const template = templates[templateName]
if (template) {
searchRef.value?.setItems([...template])
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Dynamic Validation Rules β
Add validation rules dynamically based on search items:
const addItemWithValidation = (item: any) => {
// Add validation rules for certain fields
if (item.prop === 'email') {
item.rules = [
{ required: true, message: 'Email cannot be empty', trigger: 'blur' },
{ type: 'email', message: 'Invalid email format', trigger: 'blur' }
]
}
searchRef.value?.appendItem(item)
}
2
3
4
5
6
7
8
9
10
11
Best Practices β
1. State Management β
Recommended to use state management libraries (like Pinia) for managing complex search item states:
// Use Pinia store to manage search items
const useSearchStore = defineStore('search', () => {
const searchItems = ref([])
const addItem = (item: any) => {
searchItems.value.push(item)
searchRef.value?.setItems(searchItems.value)
}
const removeItem = (prop: string) => {
searchItems.value = searchItems.value.filter(item => item.prop !== prop)
searchRef.value?.setItems(searchItems.value)
}
return { searchItems, addItem, removeItem }
})
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2. Performance Optimization β
Use debounce technique when frequently updating search items dynamically:
import { debounce } from 'lodash-es'
// Debounced item update
const updateItemsDebounced = debounce((items: any[]) => {
searchRef.value?.setItems(items)
}, 300)
2
3
4
5
6
3. Data Persistence β
Save dynamically created search item configurations to local storage:
// Save search configuration
const saveSearchConfig = () => {
const items = searchRef.value?.getItems()
localStorage.setItem('searchConfig', JSON.stringify(items))
}
// Restore search configuration
const restoreSearchConfig = () => {
const savedConfig = localStorage.getItem('searchConfig')
if (savedConfig) {
const items = JSON.parse(savedConfig)
searchRef.value?.setItems(items)
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
Related Links β
- Method Demonstration - Learn detailed usage of all exposed methods
- Custom Actions - Learn about custom action buttons
- Advanced Search - Learn implementation of complex search scenarios