Datatable- How to custom sort one column by defferent values every times from rendered html element

Let’s assume the cell of the Datatable are rendered like this:

      columnDefs: [
            {
                targets: 0,
                type: 'percent',
                render: function (data, type, full, meta) {
                    let html = '<span class="sort-value">' + data + '</span>';
                    html += "(<span class='sort-percent'>" + full.percent + "</span>%)";
                    return html;
                }
            }
      }
  • At this time, by clicking on the header, I want to sort in the ASC direction for the first sorting using the value with the sort-value class name of the rendered HTML element.

  • The second sort is the same, but the DESC direction by the value with the sort-value class name.

  • For the third sort, I want to sort in the ASC direction by the value with the sort-percent class name.

  • For the fourth sort, I want to sort in the DESC direction by the value with the sort-percent class name.

Then I want to repeat this sequence over and over again.

So I created the logic for this and introduced the following two variables:

var previously_sorted_index = 0;
var sort_state = 0;         // 0: default, 1: sort-value ASC, 2: sort-value DESC, 3: percentage ASC, 4: percentage DESC

And whenever the header is clicked and sort is performed, the sort_state value is updated and this works well. For better understanding, here is my code:

$('#table thead th').on('click', function() {
    var colIdx = $(this).index();

    if(previously_sorted_index == colIdx) {
        sort_state ++;
        if(sort_state == 5)
            sort_state = 1;
    } else {
        previously_sorted_index = colIdx;
        sort_state = 1;
    }
});

The problem is that I customized the sort using datatable’s sort plugin, but it doesn’t work as expected.
It simply performs a string sort using the HTML elements of the cell.

Here is my custom code

$.extend($.fn.dataTableExt.oSort, {
        "percent-pre": function (a) {
            let div = document.createElement('div');
            div.innerHTML = a;
            let sort_val_el = div.getElementsByClassName("sort-value");
            let sort_percent_el = div.getElementsByClassName("sort-percent");

            let sort_val = parseFloat(sort_val_el.length > 0 ? sort_val_el[0].innerHTML : 0);
            let percentage_val = parseFloat(sort_percent_el.length > 0 ? sort_percent_el[0].innerHTML : 0);

            return [sort_val, percentage_val];
        },
      
        "percent-asc": function (a, b) {
            if(sort_state == 1) {
                return a[0] > b[0] ? 1 : -1;
            } else if(sort_state == 3) {                        // percentage ASC/DESC
                return a[1] > b[1] ? 1 : -1;
            }
            return 0;
        },
      
        "percent-desc": function (a, b) {
            if (sort_state == 2) {                              // sort-value ASC/DESC
                return b[0] > a[0] ? 1 : -1;
            } else if(sort_state == 4) {                        // percentage ASC/DESC
                return b[1] > a[1] ? 1: -1;
            }
            return 0;
        }
    });

I debugged it, and the value of sort_state is updated exactly as expected.

When percent-pre returns a single value rather than an array as in my code (for example, only the percent value), the sort works exactly based on that value.

If return an array and sorting by the value of any element of the array according to the sort_state value does not work.

Is there something wrong with my code?

Thank you!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật