binxie
2024-01-18 b1d36ea3e6653e59bd767aa192c688ee0d9d4c58
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * @Description: 
 * @version: 
 * @Author: chen jing wu
 * @Date: 2023-05-25 13:59:44
 * @LastEditors: chen jing wu
 * @LastEditTime: 2023-11-11 20:06:45
 */
import { LightningElement, track, api } from 'lwc';
import getRecordsByName from '@salesforce/apex/lexLookupController.getRecordsByName'
import { RefreshEvent } from 'lightning/refresh';
export default class LexLookupLwc extends LightningElement {
    @api
    objectname;
    @api
    queryBrand;
    @track
    recordsList;
    @track
    selectedValue = "";
    error;
    @track
    recordselected = false;
    @api
    iconname;
    @api
    initValue = "";
    initValuetemp = '';
    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;
        }else{
            this.recordselected = false;
        }
    }
    renderedCallback(){
    }
   
    
    @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.initValuetemp = this.selectedValue;
        this.selectedValue = "";
        this.initValue = "";
        this.recordsList = undefined;
        const customEvent = new CustomEvent('buttonclick', {
            bubbles: true,  // 使事件冒泡
            composed: true  // 使事件可以跨shadow boundary传播
        });
        this.dispatchEvent(customEvent);
    }
    @api
    refresh(){
        console.log('you============================');
        console.log(this.initValue);
        console.log(this.selectedValue);
        console.log('me=====================');
        // if(this.initValue == '' && this.selectedValue == '' && this.initValuetemp != ''){
        //     // this.recordselected = true;
        //     this.initValue = this.initValuetemp;
        //     // this.selectedValue = this.initValue;
        //     return;
        // }
        if(this.initValue != undefined && this.initValue != null && this.initValue != ''){
            // this.recordselected = false;
            this.recordselected = true;
            this.selectedValue = this.initValue;
        }else{
            this.selectedValue = '';
            this.recordselected = false;
 
        }
           
    }
    dontClose(event){
        event.preventDefault();
    }
 
    //Method to pass selected record to parent component.
    setSelectedValue(event) {
        this.selectedValue = event.target.dataset.itemname;
        this.initValue = 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;
    }
}