14.07 Filter - Highlight the Search Query on Matching Tasks

This commit is contained in:
Fredrick W Warren 2021-02-11 11:53:32 -08:00
parent d3af477f86
commit 15199bbb42

View File

@ -13,8 +13,9 @@
<q-item-section> <q-item-section>
<q-item-label <q-item-label
:class="{ 'text-strikethrough' : task.completed }"> :class="{ 'text-strikethrough' : task.completed }"
{{ task.name }} v-html="$options.filters.searchHighLight(task.name, search)"
>
</q-item-label> </q-item-label>
</q-item-section> </q-item-section>
@ -71,7 +72,7 @@
</template> </template>
<script> <script>
import { mapActions } from 'vuex' import { mapState, mapActions } from 'vuex'
import editTask from 'components/Tasks/Modals/EditTask.vue' import editTask from 'components/Tasks/Modals/EditTask.vue'
import { date } from 'quasar' import { date } from 'quasar'
const { formatDate } = date const { formatDate } = date
@ -90,8 +91,33 @@ export default {
filters: { filters: {
niceDate (value) { niceDate (value) {
return formatDate(value, 'MMM D') return formatDate(value, 'MMM D')
},
searchHighLight (value, search) {
if (search) {
var output = ''
var ms = search.toLocaleLowerCase()
var ml = search.length
var str = value
while (str.length) {
const rts = str.toLocaleLowerCase()
var pos = rts.indexOf(ms)
if (pos === -1) {
output += str
str = ''
} else {
output += str.substr(0, pos) + '<span class="bg-yellow-6">' + str.substr(pos, ml) + '</span>'
str = str.substr(pos + ml)
}
}
return output
}
return value
} }
}, },
computed: {
...mapState('tasks', ['search'])
},
methods: { methods: {
...mapActions('tasks', ['updateTask', 'deleteTask']), ...mapActions('tasks', ['updateTask', 'deleteTask']),
promptToDelete (id) { promptToDelete (id) {