Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

widgets do not take parsers into consideration #1547

Closed
dmarra opened this issue May 17, 2018 · 4 comments
Closed

widgets do not take parsers into consideration #1547

dmarra opened this issue May 17, 2018 · 4 comments

Comments

@dmarra
Copy link

dmarra commented May 17, 2018

In my current project, there are a LOT of tables. Each one contains form input fields in some of the columns.
In order to extract the text out of them, I add a parser once in a js file (textExtract option isn't sufficient since it is per column, and I would have to write that code over and over for each table since the columns are not guaranteed to be the same in each one). this fixes all my sorter problems universally.

I ALSO use the output widget on most of these tables. What I am unfortunately forced to do is duplicate the code I use for the parser and place it in "output_formatContent" option (otherwise the fields with inputs have no output data in them). This means I have to maintain the same code in two places.

It would be nice if the "parsed" option wasn't just filter widget specific. OR, if we could define that widgets should use different parsers. something like:

.tablesorter({
    widgets: [
        "output": [
            "parsers": [
                "myCustomParser"
            ]
        ]
});

Just SOME way to be able to apply a parser to a widget

@Mottie
Copy link
Owner

Mottie commented May 17, 2018

Hi @dmarra!

The textExtraction function can be set for the entire table. Within the function you can test the cellIndex to determine what column is being processed:

var myTextExtraction = function(node, table, cellIndex) {
  return cellIndex === 3
    ? // input in 4th column
      $(node).find("input").val()
    : $(node).find("selector").text();
};
$(function() {
  $("#myTable").tablesorter({
    textExtraction: myTextExtraction
  });
});

As for the output widget... what if I set up the output_formatContent option to also provide the parsed data saved in the cache? That would have already been processed by the column parser.

If that won't work, you can use getParserById within the output_formatContent and use it to format the data.$cell.text():

output_formatContent : function( c, wo, data ) {
  var column = data.$cell[0].cellIndex;
  if (column === 3) {
    return $.tablesorter.getParserById("myCustomParser").format(data.$cell.text());
  }
}

@dmarra
Copy link
Author

dmarra commented May 18, 2018

Oohhhh. I had no idea you could define just a callback for textExtraction; I don't think the documentation demonstrates that ability. That is good to know. It would work for me, if the output widget also respects the output of the callback.

As far as output_formatContent, it would definitely go a long way if you could provide the parsed content to it as well. then, all I need to do is make a small callback to return the parsed value. that would be awesome!

I am not sure I can get by with just getParserById(), because each column uses a different parser (defined with data- tags). It could work if I can get the name of the parser for that particular field though....

@Mottie
Copy link
Owner

Mottie commented May 19, 2018

The master branch has been updated. Use output_formatContent as follows:

output_formatContent : function(config, widgetOptions, data) {
	// data.isHeader (boolean) = true if processing a header cell
	// data.$cell = jQuery object of the cell currently being processed
	// data.content = processed cell content (spaces trimmed, quotes added/replaced, etc)
	// data.columnIndex = column in which the cell is contained
	// data.parsed = cell content parsed by the associated column parser
	return data.parsed;
},

It'll be available in the next release.

@Mottie
Copy link
Owner

Mottie commented Jul 10, 2018

This was added in v2.30.5; v2.30.7 was just released.

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

No branches or pull requests

2 participants