highlight.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. Component({
  2. properties: {
  3. text:{
  4. type:String,
  5. observer:'_changeText'
  6. },
  7. key:{
  8. type:String,
  9. value:'',
  10. observer:'_changeKey'
  11. }
  12. },
  13. data: {
  14. highlightList:[],//处理后的数据
  15. },
  16. methods: {
  17. _changeText(e) {
  18. // if (e.indexOf(this.properties.key) > -1) {
  19. this._filterHighlight(e,this.properties.key);
  20. // }
  21. },
  22. // 非空过滤
  23. _changeKey(e) {
  24. this._filterHighlight(this.properties.text,e);
  25. },
  26. /**
  27. * 关键字高亮处理
  28. * @param { String } text - 文本
  29. * @param { String } key - 关键字
  30. */
  31. _filterHighlight(text, key) {
  32. let textList = text.split("");
  33. let keyList = key.split("");
  34. let list = [];
  35. for (let i = 0; i < textList.length; i++) {
  36. let obj = {
  37. deep: false,
  38. val: textList[i]
  39. }
  40. list.push(obj);
  41. };
  42. for (let k = 0; k < keyList.length; k++) {
  43. list.forEach(item => {
  44. if (item.val === keyList[k]) {
  45. item.deep = true;
  46. }
  47. })
  48. }
  49. this.setData({
  50. highlightList: list
  51. })
  52. }
  53. }
  54. })