One way to achieve this would be to store the style attribute of that element to a variable and check if is display or block and based on that you can add class.
Something like this:
const display = document.getElementById["invoiceTable"].style.display; // Inline style
or, if the display property is an inherited property:
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
and then add your classes based on display:
display === 'block' ? element.classList.add["block"] : element.classList.add["other"];
Xin chào tất cả các bạn đã quay trở lại với series học VueJS với Laravel của mình, ở bài trước mình đã hướng dẫn các bạn về Wacher trong VueJS, ở trong bài này chúng ta sẽ cùng đến với Conditional rendering, đây là những thứ mà mình tin chắc các bạn sẽ dùng rất nhiều trong công việc.
Ở bài này cách sử dụng của các directive điều kiện này khá là giống với những gì các bạn đã từng được học trong các ngôn ngữ lập trình.
Ở bài này chúng ta tạo ra một file mới tên là ConditionalRendering.vue
ở trong folder components
nhé, sau đó chúng ta khai báo nó ở trong file
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
0 và nhớ thêm nó vào file const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
1 nhé. Nội dung như bên dưới, các bạn chạy thử xem nhé: [nhớ luôn chạy const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
2 và const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
3 nhé mọi người]
This is block 1
This is block 2
export default {
}
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
4
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
Ở ví dụ trên nếu như chúng ta muốn chỉ render ra từng block ở những điều kiện nhất định, khi đó chúng ta có thể sử dụng
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
4 để quản lý việc render. Cùng xem ví dụ sau:
This is block 1
This is block 2
Button
export default {
data[] {
return {
isActive: true
}
},
methods: {
toggleActive[] {
if[this.isActive == true] {
this.isActive = false
}
else {
this.isActive = true
}
}
}
}
Ở đây block 1 chỉ được render khi
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
6 có giá trị const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
7 và ngược lại thì block 2 sẽ được render. Chúng ta tạo một const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
8 để toggle giá trị của const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
6 bằng cách mỗi lần click const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
8 thì sẽ gọi đến hàm display === 'block' ? element.classList.add["block"] : element.classList.add["other"];
1. Các bạn f5 lại trình duyệt và thử kết quả nhé. Các bạn bật cả Vue devtool nên để xem giá trị của isActive thay đổi như thế nào mỗi lần click nhé.display === 'block' ? element.classList.add["block"] : element.classList.add["other"];
2, display === 'block' ? element.classList.add["block"] : element.classList.add["other"];
3
display === 'block' ? element.classList.add["block"] : element.classList.add["other"];
display === 'block' ? element.classList.add["block"] : element.classList.add["other"];
Với ví dụ trên luồng xử lý của chúng ta có performance chưa tốt, vì mỗi lần component này render, đến block 1 nó sẽ kiểm tra giá trị
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
6, sau đó đến block 2 nó lại kiểm tra giá trị const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
6 một lần nữa. Thay vì phải kiểm tra 2 lần như thế chúng ta có thể sử dụng display === 'block' ? element.classList.add["block"] : element.classList.add["other"];
2 để Vue bỏ qua kiểm tra biểu thức còn lại nếu như một trong 2 biểu thức đã đúng, được mô tả như trong đoạn code sau:
This is block 1
This is block 2
Button
export default {
data[] {
return {
isActive: true
}
}
}
Ở đây mình sử lại code một chút cho gọn, không dùng đến methods để thay đổi giá trị của
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
6 nữa mà chúng ta sẽ để trực tiếp khi sự kiện display === 'block' ? element.classList.add["block"] : element.classList.add["other"];
8 được gọi thì thay đổi giá trị của const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
6 trái ngược với giá trị hiện tại.Các bạn load lại trình duyệt và thử xem kết quả nhé, ta nhận được điều tương tự.
Tiếp theo khi chúng ta có nhiều block muốn render theo từng điều kiện cụ thể thì đó là lúc chúng ta nghĩ đến
display === 'block' ? element.classList.add["block"] : element.classList.add["other"];
3
This is block 1
This is block 2
This is block 3
Button 1
Button 2
Button 3
export default {
data[] {
return {
currentBlock: 1
}
},
methods: {
showBlock[number] {
this.currentBlock = number
}
}
}
Cách sử dụng khá đơn giản, các bạn có thể tự xem và giải thích, nếu không hiểu thì comment bên dưới nhé.
This is block 1
This is block 2
export default {
}
1
This is block 1
This is block 2
export default {
}
Quay trở lại ví dụ ở phần
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
4, để có thể hiện thị 1 trong 2 block tùy theo giá trị của const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
6, chúng ta cũng có thể sử dụng cách khác đó là dùng
This is block 1
This is block 2
export default {
}
1:
This is block 1
This is block 2
Button
export default {
data[] {
return {
isActive: true
}
}
}
Cùng load lại trình duyệt và các bạn sẽ thấy kết quả tương tự như dùng
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
4 mỗi khi click vào button. Đến đây các bạn sẽ tự hỏi: "giời ơi vậy thì cuối cùng v-if hay là v-show, tôi nên dùng cái nào?" . Chúng ta cùng đào sâu hơn một chút về chúng nhé.*Đối với v-if: sử dụng ví dụ phần
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
4Nếu chúng ta mở inspect HTML lên [f12 trên win, hoặc chuột phải và chọn
This is block 1
This is block 2
export default {
}
7], mỗi lần click vào button thì thẻ
This is block 1
This is block 2
export default {
}
8 chứa block cũ sẽ được thay thế nội dung của block mới.*Đối với v-show: sử dụng ví dụ ngay bên trên
Khi inspect các bạn có thể thấy ngay là Vue đã render sẵn ra luôn 2 element chính là 2 block của chúng ta, và tùy với giá trị của
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
6 mà block đó có
This is block 1
This is block 2
Button
export default {
data[] {
return {
isActive: true
}
},
methods: {
toggleActive[] {
if[this.isActive == true] {
this.isActive = false
}
else {
this.isActive = true
}
}
}
}
0 là
This is block 1
This is block 2
Button
export default {
data[] {
return {
isActive: true
}
},
methods: {
toggleActive[] {
if[this.isActive == true] {
this.isActive = false
}
else {
this.isActive = true
}
}
}
}
1 hay không.Điều rút ra ở đây là:
4 không render ngay ra toàn bộ các element mà tùy vào giá trị kiểm tra, còn vớiconst display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
1 sẽ render ra ngay từ đầu và chỉ quyết định cóThis is block 1 This is block 2 export default { }
4 nó hay khôngThis is block 1 This is block 2 Button export default { data[] { return { isActive: true } }, methods: { toggleActive[] { if[this.isActive == true] { this.isActive = false } else { this.isActive = true } } } }
- Từ kết quả trên chúng ta thấy rằng: nếu nội dung trong block mà ít, và trong quá trình sử dụng mà user sẽ tác động đến block nhiều thì dùng
1 sẽ cho tốc độ tốt hơn, còn nếu nội dung trong block mà nhiều cùng với nhiều xử lý hoặc nội dung của block ít thay đổi trong suốt vòng đời của component thì khi đó không nên dùngThis is block 1 This is block 2 export default { }
1 vì lúc đó trang của chúng ta sẽ nặng làm giảm performance và ta cần nghĩ đến dùngThis is block 1 This is block 2 export default { }
4const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
Kết luận
Qua bài này mong rằng các bạn sẽ biết cách sử dụng
const display = window.getComputedStyle[document.getElementById["invoiceTable"], null].display;
4, display === 'block' ? element.classList.add["block"] : element.classList.add["other"];
2, display === 'block' ? element.classList.add["block"] : element.classList.add["other"];
3 và
This is block 1
This is block 2
export default {
}
1 trong quá trình phát triển ứng dụng các bạn sẽ phải dùng đến nó rất nhiều. Đồng thời hiểu và sử dụng linh hoạt các
This is block 1
This is block 2
Button
export default {
data[] {
return {
isActive: true
}
}
}
2 này để có một ứng dụng tốt.Ở bài tiếp theo chúng ta sẽ cùng tìm hiểu về
This is block 1
This is block 2
Button
export default {
data[] {
return {
isActive: true
}
}
}
3 nhé.Cám ơn các bạn đã theo dõi, nếu có gì thắc mắc các bạn để lại comment cho mình nhé ^^!