일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- js
- 회고
- 리액트
- 정재남
- LeetCode
- 타임어택
- 파이썬
- 2주 프로젝트
- 백준
- programmers
- 렛츠기릿 자바스크립트
- 코어 자바스크립트
- 타입스크립트
- 코드스테이츠
- 4주 프로젝트
- 자바스크립트
- 프로그래머스
- SQL 고득점 Kit
- python
- 손에 익히며 배우는 네트워크 첫걸음
- 리트코드
- Async
- til
- 제로초
- codestates
- 알고리즘
- 리덕스
- 타입스크립트 올인원
- 토익
- javascript
Archives
- Today
- Total
Jerry
Add custom css to lightning-datatable checkbox 본문
반응형
Orders for Requirements
1. Add checked status to checkbox according to specific condition.
2. Add visibility css to checked checkbox using custom style.
HTML
<template>
<lightning-datatable
data={data}
columns={columns}
key-field="Id"
onrowselection={getSelectedName}
selected-rows={preSelectedRows}
>
</lightning-datatable>
</template>
JavaScript
import { LightningElement } from 'lwc';
const columns = [
{ label: 'Opportunity name', fieldName: 'OpportunityName', type: 'text',
cellAttributes: {
class: 'slds-checkbox_faux.flag flag',
},
},
{
label: 'Confidence',
fieldName: 'Confidence',
type: 'percent',
cellAttributes: {
iconName: { fieldName: 'TrendIcon' },
iconPosition: 'right',
},
},
{
label: 'Amount',
fieldName: 'Amount',
type: 'currency',
typeAttributes: { currencyCode: 'EUR', step: '0.001' },
},
{ label: 'Contact Email', fieldName: 'Contact', type: 'email' },
{ label: 'Contact Phone', fieldName: 'Phone', type: 'phone' },
];
const data = [
{
Id: 'a',
OpportunityName: 'Cloudhub',
Confidence: 0.2,
Amount: 25000,
Contact: 'jrogers@cloudhub.com',
Phone: '2352235235',
TrendIcon: 'utility:down',
flag: '-',
},
{
Id: 'b',
OpportunityName: 'Quip',
Confidence: 0.78,
Amount: 740000,
Contact: 'quipy@quip.com',
Phone: '2352235235',
TrendIcon: 'utility:up',
flag: '*',
},
];
const setCustomStyle = (style, id) => {
let styleElement = document.createElement("style");
styleElement.setAttribute("id", id);
styleElement.innerHTML = style;
document.body.appendChild(styleElement);
console.log("setCustomStyle ===> execution");
}
const removeCustomStyle = (id) => {
const target = document.querySelector("style#" + id);
if(target) target.remove();
console.log("removeCustomStyle ===> execution");
}
const customStyle = {
style : `
// .slds-checkbox_faux {
// border: 1px solid red !important;
// }
// .slds-checkbox_faux.flag, .flag {
// border: 1px solid yellow !important;
// }
// .slds-th__action.slds-th__action_form.slds-cell-fixed.flag {
// border: 1px solid green !important;
// }
.slds-checkbox [type=checkbox]:checked+.slds-checkbox__label {
visibility: hidden;
}
`,
id: `dataTable`
}
export default class dataTable extends LightningElement {
data = data;
columns = columns;
preSelectedRows = [];
connectedCallback() {
this.data.forEach((el, idx) => {
if (el.flag === '-') {
console.log("el ===> ", el);
this.preSelectedRows.push(el.Id);
console.log("class (0) ===> ", this.columns[idx]);
console.log("class (1) ===> ", this.columns[idx].cellAttributes);
console.log("class (1) ===> ", this.columns[idx].cellAttributes?.class);
if (this.columns[idx].cellAttributes?.class !== undefined) {
let target = this.template.querySelectorAll('.slds-th__action.slds-th__action_form.slds-cell-fixed');
console.log("check ===> target (1) ", target);
// this.columns[idx].cellttributes = {};
// this.columns[idx].cellAttributes.class = [];
// this.columns[idx].cellAttributes.class.push(this.columns[idx].cellAttributes.class + 'flag');
console.log("check ===> this (2)", this.columns[idx].cellAttributes);
} else {
console.log("check ===> this (1) ", this.columns[idx].cellAttributes);
this.columns[idx].cellAttributes.class = ['1'];
console.log("check ===> this (2)", this.columns[idx].cellAttributes);
}
console.log("class (2) ===> ", this.columns[idx].cellAttributes);
}
})
setCustomStyle(customStyle.style, customStyle.id)
}
disconnectedCallback() {
removeCustomStyle(customStyle.id)
}
getSelectedName(event) {
console.log("getSelectedName (event) ===> ", JSON.parse(JSON.stringify(event)));
console.log("getSelectedName (event.detail) ===> ", JSON.parse(JSON.stringify(event.detail)));
console.log("getSelectedName (event.detail.selectedRows) ===> ", JSON.parse(JSON.stringify(event.detail.selectedRows)));
const selectedRows = event.detail.selectedRows;
// Display that fieldName of the selected rows
for (let i = 0; i < selectedRows.length; i++) {
console.log('You selected: ' + selectedRows[i].OpportunityName);
}
}
}
반응형
'Salesforce > Aura + LWC' 카테고리의 다른 글
template tag(템플릿 태그) (0) | 2022.06.07 |
---|