implemented simpleSearch for Employees/Index.

This commit is contained in:
Sockenklaus
2021-11-07 15:37:08 +01:00
parent e018db9e0a
commit a9d0f109a9
6 changed files with 128 additions and 22 deletions

View File

@@ -0,0 +1,64 @@
<template>
<div class="input-group">
<input
type="text"
class="form-control"
v-model="searchQuery"
placeholder="Suche nach..."
@keyup="search"
>
<button aria-expanded="false" data-bs-toggle="dropdown" data-bs-auto-close="outside" type="button" class="btn btn-outline-secondary dropdown-toggle">
Suche in...
</button>
<ul class="dropdown-menu">
<li v-for="(item, index) in unionColumns" :key="item">
<label class="dropdown-item form-check">
<input type="checkbox" :value="item" :id="'checkBox'+item" v-model="checkedColumns" class="form-check-input">
{{item}}
</label>
</li>
</ul>
</div>
{{ unionColumns }}
</template>
<script setup lang="ts">
import { ref, computed} from 'vue';
import { storeToRefs } from 'pinia'
import { useEmployees } from '@/stores/employees';
import { union } from 'lodash';
const searchQuery = ref('')
const checkedColumns = ref([])
const store = useEmployees()
const { columns, simple_search } = storeToRefs(store)
const queryString = computed(() => {
return searchQuery.value + '(' + checkedColumns.value.join(',') + ')'
})
const unionColumns = computed(() => {
return union(columns.value, checkedColumns.value)
})
async function search() {
simple_search.value = queryString.value
await store.fetchFromApi()
}
</script>
<style scoped>
.form-check {
padding-left: 20px;
}
.form-check input {
margin-left: -0.5rem;
margin-right: 0.5rem;
}
</style>

View File

@@ -2,17 +2,15 @@ import { defineStore } from 'pinia'
import { useUser } from '@/stores/user'
import axios from '@/axios'
import emplJSON from '../sample-data/employees.json'
const user = useUser()
export const useEmployees = defineStore('employees', {
state: () => {
return {
/**
* @type {}[]
* @type any[]
*/
rows: Array<Object>(),
rows: Array<any>(),
meta: {
current_page: NaN,
@@ -22,7 +20,12 @@ export const useEmployees = defineStore('employees', {
total: NaN
},
columns: Array<string>()
columns: Array<string>(),
limit: 10,
page: 1,
sort_by: '',
simple_search: ''
}
},
@@ -33,28 +36,29 @@ export const useEmployees = defineStore('employees', {
* @param: {string} sortBy - QueryString: sort_by=asc(col1),desc(col2),...
* @param: {string} simpleSearch - QueryString: simple_search=query(col1,col2,...)
**/
async fetchFromApi(limit?: number, page?:number, sort_by?: string, simple_search?: string) {
async fetchFromApi() {
try {
const data : any= (await axios.get(
const data : any = (await axios.get(
'/employees',
{
params: {
limit,
page,
sort_by,
simple_search
limit: this.limit,
page: this.page,
sort_by: this.sort_by,
simple_search: this.simple_search
},
headers: user.header
}
)).data
Object.assign(this.meta, data?.meta)
Object.assign(this.rows, data?.data)
Object.assign(this.meta, data.meta)
this.rows = data.data.map(e => e)
this.columns = this.fetchColumns()
}
catch(err) {
console.log(err)
}
},
@@ -63,6 +67,10 @@ export const useEmployees = defineStore('employees', {
return Object.keys(this.rows[0])
}
return []
},
watch(){
}
}
})

View File

@@ -1,5 +1,4 @@
<template>
<form class="text-start">
<VProfileControls class="mb-5" :isActive="editEmployee || createUser" @save="onUpdateEmployee" @toggleEdit="onToggleEdit" />

View File

@@ -1,6 +1,8 @@
<template>
<table class="table table-hover">
<EmployeesSimpleSearch />
<table class="table table-hover mt-3">
<thead>
<tr>
<th
@@ -26,7 +28,8 @@
<script setup lang="ts">
import { onMounted, reactive, watch, computed } from 'vue'
import EmployeesSimpleSearch from '@/components/Employees/EmployeesSimpleSearch.vue';
import { onMounted, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { storeToRefs } from 'pinia'
import { useEmployees } from '@/stores/employees'
@@ -40,12 +43,8 @@ const sort_by = reactive({
column: 'id'
})
watch(sort_by, () => {
store.fetchFromApi(undefined, undefined, (sort_by.asc ? "asc(" : "desc(")+sort_by.column+")")
})
onMounted(() => {
store.fetchFromApi(undefined, undefined, (sort_by.asc ? "asc(" : "desc(")+sort_by.column+")")
store.fetchFromApi()
})
function colIsSelected(col : string) {
@@ -57,6 +56,8 @@ function onSortBy(col : string) {
else sort_by.asc = !sort_by.asc
sort_by.column = col
store.sort_by = (sort_by.asc ? 'asc(' : 'desc(') + sort_by.column + ')'
store.fetchFromApi()
}
const iconSort = computed(() => {
@@ -66,6 +67,13 @@ const iconSort = computed(() => {
</script>
<style scoped>
table {
/* table-layout: fixed; */
}
table thead tr:first-child th:first-child {
width: 51px;
}
table th {
-ms-user-select: none;
@@ -75,6 +83,7 @@ table th {
-khtml-user-select: none;
user-select: none;
cursor: pointer;
}
tr {