王士厅

fix: 搜索页-联想词展示问题

... ... @@ -114,6 +114,7 @@ export struct SearchComponent {
console.log(TAG, JSON.stringify(err))
})
}
// console.log(TAG, 'this.relatedSearchContentsData', JSON.stringify(this.relatedSearchContentsData))
}
getSearchHint() {
... ...
import { SearchRelatedItem } from '../../viewmodel/SearchRelatedItem'
import { SearchShowRed, titleInitRes, textItem } from '../../utils/searchShowRed';
const TAG = "SearchRelatedComponent"
... ... @@ -12,6 +13,23 @@ export struct SearchRelatedComponent {
@Prop searchText: string
@Prop percent:number = 1
// 正则过滤检索词
createCaseInsensitiveRegex(str: string, pattern: string) {
let regex = new RegExp(pattern, 'gi');
return str.replace(regex, (match) => `<em>${match}</em>`);
}
checkForPattern(str: string, pattern: string) {
let highlightedStr = this.createCaseInsensitiveRegex(str, pattern);
return highlightedStr;
}
titleInit(str: string, pattern: string) {
const title = this.checkForPattern(str, pattern)
const titleInitRes:titleInitRes = SearchShowRed.titleInit(title)
return titleInitRes
}
build() {
Column() {
List() {
... ... @@ -28,11 +46,29 @@ export struct SearchRelatedComponent {
Text(){
ForEach(item.data_arr,(item:string)=>{
if (this.titleInit(item, this.searchText).titleMarked) {
ForEach(this.titleInit(item, this.searchText).textArr, (textItem: textItem) => {
if (textItem.isRed) {
Span(textItem.content)
.fontColor($r('app.color.color_ED2800'))
.fontSize(`${this.calcHeight(31)}lpx`)
.fontWeight(400)
.lineHeight(`${this.calcHeight(50)}lpx`)
} else {
Span(textItem.content)
.fontColor($r('app.color.color_000000'))
.fontSize(`${this.calcHeight(31)}lpx`)
.fontWeight(400)
.lineHeight(`${this.calcHeight(50)}lpx`)
}
})
} else {
Span(item)
.fontColor(item===this.searchText?$r('app.color.color_ED2800'):$r('app.color.color_000000'))
.fontColor($r('app.color.color_000000'))
.fontSize(`${this.calcHeight(31)}lpx`)
.fontWeight(400)
.lineHeight(`${this.calcHeight(50)}lpx`)
}
})
}
.maxLines(1)
... ...