19626
2023-10-07 988f9735377909b6310301e582c15804e004783f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * @Description: 
 * @version: 
 * @Author: chen jing wu
 * @Date: 2023-05-25 13:59:44
 * @LastEditors: chen jing wu
 * @LastEditTime: 2023-09-27 15:30:05
 */
import { LightningElement, track, api } from 'lwc';
import getRecordsByName from '@salesforce/apex/lexLookupController.getRecordsByName'
 
export default class LexLookupLwc extends LightningElement {
    @api
    objectname;
    @api
    queryBrand;
    @track
    recordsList;
    @track
    selectedValue = "";
    error;
    @track
    recordselected = false;
    @api
    iconname;
    @api
    initValue;
    disabled = false;
    //Method to query data after typing search term
    onKeyChange(event) {
        this.selectedValue = event.target.value;
        if(this.queryBrand == undefined){
            this.queryBrand = '';
        }
        getRecordsByName({objectName : this.objectname, searchFor : this.selectedValue, queryBrand : this.queryBrand})
            .then(result => {
                this.recordsList = result;
            })
            .catch(error => {
                //exception handling
                this.error = error;
            })
        
    }
    connectedCallback(){
        
        if(this.initValue != '' && this.initValue != undefined && this.initValue != null){
            this.recordselected = true;
            this.selectedValue = this.initValue;
        }
    }
   
    
    @api
    letDisabledTrue(){
        this.recordselected = false;
        this.disabled = true;
        this.selectedValue = "";
        this.recordsList = undefined;
    }
    @api
    letDisabledFalse(){
        this.recordselected = false;
        this.disabled = false;
        this.selectedValue = "";
        this.recordsList = undefined;
    }
    //Method to clear search list and show selected value.
    @api
    clearSelection() {
        console.log('qwer');
        this.recordselected = false;
        this.selectedValue = "";
        this.recordsList = undefined;
        const customEvent = new CustomEvent('buttonclick', {
            bubbles: true,  // 使事件冒泡
            composed: true  // 使事件可以跨shadow boundary传播
        });
        this.dispatchEvent(customEvent);
    }
 
    //Method to pass selected record to parent component.
    setSelectedValue(event) {
        this.selectedValue = event.target.dataset.itemname;
        this.recordselected = true;
        this.recordsList = undefined;
        event.preventDefault();
        const selectedEvent = new CustomEvent('selected', {
            detail: {
                Name : this.selectedValue,
                Id : event.target.dataset.itemid,
                ObjectName : this.objectname
            } 
        });
        this.dispatchEvent(selectedEvent);
    }
    clear(){
        this.recordsList = undefined;
    }
}