Update lưu trạng thái cho chức năng sắp xếp nhận xét theo thời gian cho Threaded Comments

23.1.24 | 8 nhận xét | lượt xem
Từ bài viết của tác giả hung1001 tại bài viết này và mình chỉ update thêm tính năng lưu trạng thái sắp xếp vào localStorage.
Mình sẽ hướng dẫn lại từ đầu nhé!

Thêm bộ sắp xếp vào vị trí mong muốn

<div class='sort-comment'>
        <span class='sort-title'>Sort by</span>
        <div class='sort-sel'>
           <select class='sort-option'>
              <option selected='selected' value='newest'>Newest</option>
              <option value='oldest'>Oldest</option>
           </select>
        </div>
</div>

Giao diện

.sort-comment{text-align:right}
.sort-comment .sort-title{font-size:14px;display:inline-block;margin-right:3px}
.sort-comment .sort-sel{display:inline-block;position:relative}
.sort-comment .sort-sel::after,.sort-comment .sort-sel::before{position:absolute;content:"";right:7px;border-style:solid;border-width:4px;width:0;height:0}
.sort-comment .sort-sel::after{top:2px;border-color:transparent transparent #333}
.sort-comment .sort-sel::before{bottom:2px;border-color:#333 transparent transparent}
.sort-comment .sort-sel .sort-option{cursor:pointer;background:#fff;border:1px solid #aaa;padding:2px 20px 2px 7px;color:#656565;font-size:14px;overflow:hidden;-o-text-overflow:ellipsis;text-overflow:ellipsis;white-space:nowrap;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;-moz-appearance:window;-webkit-appearance:none;outline:0}

JS để tiện ích hoạt động

Đoạn JS bên dưới mình đã update để có thêm tính năng lưu trạng thái
$.fn.reverseChildren = function() {
    return this.each(function() {
        var $this = $(this);
        $this.children().each(function() {
            $this.prepend(this);
        });
    });
};
function reverseCommentsOrder() {
    $(".comments-content ol").reverseChildren();
}
$(document).ready(function() {
    if (localStorage.getItem("commentsOrder") === "reversed") {
        reverseCommentsOrder();
    }
});
$(".sort-option").on("change", function() {
    reverseCommentsOrder();
    localStorage.setItem("commentsOrder", "reversed");
});
Lưu ý: Template của bạn phải có thư viện jQuery mới hoạt động
Chúc anh em một ngày vui! 😁
Bạn đang xem bài viết "Update lưu trạng thái cho chức năng sắp xếp nhận xét theo thời gian cho Threaded Comments" tại chuyên mục: Blogspot , Javascript

    8

    nhận xét
    Cũ nhất ⇅
    1. Anh Trai Nắng

      hồi xưa mình đọc mấy bài của ông Hưng chả biết gì hết, nay hiểu ra thấy toàn nhiều cái hay ho ghê á! 😊

      REPLY DELETE
      1. A Đê Min

        Bác đấy nhiều cái đỉnh, tiếc là giờ chắc bận công việc nên không chơi nữa 😥

        REPLY DELETE
        1. Anh Trai Nắng

          cái theme của ông mà phát triển làm thương mại cũng ngon lắm

          REPLY DELETE
          1. A Đê Min

            Theme này nhìn nhàm chán, nội dung lại nhạt mà bác. Mình chú trọng vào tốc độ load trang thôi 😁
            Làm thương mại như mọi người theme phải màu mè, nội dung đa dạng

            DELETE
          2. Anh Trai Nắng

            ý nói cái theme ông Hưng ý 😁

            DELETE
          3. A Đê Min

            Theme của bác ấy cũng bình thường nhưng nội dung thì đỉnh 😁

            DELETE
          4. YMG

            Nhận xét phân 4 cấp nhìn khá xấu trên màn hình kích thước nhỏ (320x568, 375x667)

            DELETE
          5. A Đê Min

            Cảm ơn bác YMG đã góp ý! Nhờ vậy em đã chỉnh sửa cho hiển thị phần nhận xét ở màn hình kích thước nhỏ cho hợp lý hơn 😁

            DELETE

    Chèn hình ảnh: Chỉ cần dán link hình ảnh - Upload ảnh

    Chèn code: [pre]Code đã mã hóa [/pre]

    Chèn emoji: Nhấn tổ hợp phím “Windows + . (dấu chấm)”

    Chèn link: Click để chèn link