render ellipsis and return data without using substring

render ellipsis and return data without using substring

katzmoyekatzmoye Posts: 8Questions: 2Answers: 0

I have something like this:

      "targets": findWithAttr(columnArr, "comment_text"),
        "render": function( data, type, row ) {
                 return '<div class="my_text">' + data + '</div>';
      }

and i want to render an ellipsis on the displayed data (text). If I use the substr method:

return type === 'display' && data.length > 150 ? '<div class="my_text">' + data.substr(0, 150) + '...' + '</div>' : '<div class="my_text">' + data + '</div>';

it returns truncated text which I cannot use.

I'd prefer to use the ellipsis plugin ($.fn.dataTable.render.ellipsis(150, true)) - is it possible?

Answers

  • colincolin Posts: 15,236Questions: 1Answers: 2,598

    Hi @katzmoye ,

    This example here shows the Eliipses plugin in action. Hope that does the trick,

    Cheers,

    Colin

  • katzmoyekatzmoye Posts: 8Questions: 2Answers: 0

    That is super elegant, and renders great, but unfortunately doesn't do what I need it to do which is, ultimately, send the entire text string to 'my_text' which has a function that matches the data string against another text. In other words, i'd love for it to display the rendered ellipsis but the full data string needs to go to 'my_text'.

    Hope that makes sense.

  • colincolin Posts: 15,236Questions: 1Answers: 2,598

    Hi @katzmoye ,

    I don't quite follow, but this example here shows how you can use cell().render() to get the full string. If that's no good, could you explain a little more please,

    Cheers,

    Colin

  • katzmoyekatzmoye Posts: 8Questions: 2Answers: 0
    edited September 2018

    Sorry, I'm still very new and I very much appreciate the help!

    I have a button on the page (not in the DT) that, when clicked, looks at each row in the datatable and compares the value in returned data to a larger text. If it finds a match between the datatable value and the larger text it highlights the matching text within the larger text.

    Some of the datatables data are very long text in a tight space, so I'd like to render with the ellipsis. However, using
    return type === 'display' && data.length > 150 ? '<div class="my_text">' + data.substr(0, 150) + '...' + '</div>' : '<div class="my_text">' + data + '</div>';
    will return the truncated text with the three dots as the data, and no match will be found in the larger text using the function(s).

    Ideally, the text in the datatables would display as truncated + "..." but would return the entire text. Your example above is great for display but when i console log data it returns nothing.

    I hope this makes more sense!

  • LesFLesF Posts: 16Questions: 6Answers: 1

    If you make the div containing the text a fixed size you could use CSS, as in: text-overflow: ellipsis;

    The full data will be retained but the display will be truncated.
    See https://www.w3schools.com/cssref/css3_pr_text-overflow.asp

  • colincolin Posts: 15,236Questions: 1Answers: 2,598

    Hi @katzmoye ,

    Yep, if you look at this example here, this will do what you want. cell().render() returns the original text, so you would use that for your comparison,

    Cheers,

    Colin

This discussion has been closed.