Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Colspan breaks ordering of cells using data attribute #1708

Closed
daichihoang opened this issue Jan 22, 2020 · 4 comments
Closed

Colspan breaks ordering of cells using data attribute #1708

daichihoang opened this issue Jan 22, 2020 · 4 comments

Comments

@daichihoang
Copy link

Hello,

JS Fiddle demo: https://jsfiddle.net/daihoang/5os6uypj/5/

I have a simple table where the first column has a mixture of cells with dates and cells with text.

To sort this, I have added a data attribute to each cell and using the custom parser to sort by this attribute. In the example above, it's simply "data-row", starting at 0 for the first row

However, I am running into an issue where the cells using "colspan" are not being sorted according to their data attribute (data-rows 4, 7 and 10)

If i remove the colspan, it works as expected.

I read https://mottie.github.io/tablesorter/docs/example-colspan.html and I don't think I am missing anything, it's a fairly simple implementation without any widgets.

Can anyone help?

@Mottie
Copy link
Owner

Mottie commented Jan 22, 2020

Hi @daichihoang!

Maybe the static row widget will fulfill your need?

@daichihoang
Copy link
Author

Hi @daichihoang!

Maybe the static row widget will fulfill your need?

Hi @Mottie thanks for such a quick reply.

Unfortunately those text cells still need to be ordered, they are "events" that occured in between the 2 dates that they sit between.

Can you let me know if I tried to do it the right way by using data attributes?

@Mottie Mottie added the Bug label Mar 3, 2020
@Mottie
Copy link
Owner

Mottie commented Mar 3, 2020

Ahh, you're right, there is a small bug in the code where it wasn't saving the parsed cell value. I'll have it fixed and available in the next update.

The following code & demo won't work until the update is available - demo

Switch to use the built-in data-text attribute that is automatically extracted, and include a date in the same format as the other first column cells

<td data-text="08-Dec-19">This Race</td>

And use this modified parser

var months = [
  "jan",
  "feb",
  "mar",
  "apr",
  "may",
  "jun",
  "jul",
  "aug",
  "sep",
  "oct",
  "nov",
  "dec"
];

$.tablesorter.addParser({
  id: "customDate",
  format: function(str) {
    if (str) {
      var parts = str.split("-");
      var month = $.inArray(parts[1].toLowerCase(), months) + 1;
      var date = new Date(month + "/" + parts[0] + "/" + parts[2]);
      return date instanceof Date && isFinite(date) ? date.getTime() : "";
    }
    return str;
  },
  type: "numeric"
});

$("#myTable").tablesorter({
  textAttribute: "data-text", // default value
  duplicateSpan: false,
  headers: {
    0: { sorter: "customDate" }
  }
});

@Mottie Mottie closed this as completed in 07d7c71 Mar 3, 2020
@daichihoang
Copy link
Author

@Mottie Thankyou very much for resolving this! I really appreciate your time and plugin. Thankyou!!

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants