12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- Component({
- properties: {
- text:{
- type:String,
- observer:'_changeText'
- },
- key:{
- type:String,
- value:'',
- observer:'_changeKey'
- }
- },
- data: {
- highlightList:[],//处理后的数据
- },
- methods: {
- _changeText(e) {
- // if (e.indexOf(this.properties.key) > -1) {
- this._filterHighlight(e,this.properties.key);
- // }
- },
- // 非空过滤
- _changeKey(e) {
- this._filterHighlight(this.properties.text,e);
- },
- /**
- * 关键字高亮处理
- * @param { String } text - 文本
- * @param { String } key - 关键字
- */
- _filterHighlight(text, key) {
- let textList = text.split("");
- let keyList = key.split("");
- let list = [];
- for (let i = 0; i < textList.length; i++) {
- let obj = {
- deep: false,
- val: textList[i]
- }
- list.push(obj);
- };
- for (let k = 0; k < keyList.length; k++) {
- list.forEach(item => {
- if (item.val === keyList[k]) {
- item.deep = true;
- }
- })
- }
- this.setData({
- highlightList: list
- })
- }
- }
- })
|