imballinst / react-bs-datatable

Bootstrap datatable without jQuery. Features include: filter, sort, pagination, checkbox, and control customization.
https://imballinst.github.io/react-bs-datatable
MIT License
59 stars 20 forks source link

HTML code crash filtering #8

Closed Styvak closed 7 years ago

Styvak commented 7 years ago

I have a property with an html button to link to and other url, on the header filterable is false but when I use the search I have an error : Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

It is possible to display an html object with the database ?

Thanks

imballinst commented 7 years ago

Hello!

It should be possible, according to https://github.com/Imballinst/react-bs-datatable/blob/master/src/index.js#L179

          if (React.isValidElement(columnValue)) {
            // If columnValue is React element
            columnValue = this.getLastChildren(columnValue);
          } else if (typeof columnValue === 'number') {
            // If columnValue is a number
            columnValue = columnValue.toString();
          }

          // Lowercase columnValue
          columnValue = columnValue.toLowerCase();

          // If filterable and columnValue contains filterText
          if (this.isPropFilterable(elementProps[i]) &&
              _includes(columnValue, this.state.filterText.toLowerCase())) {
            isElementIncluded = true;
          }

When you have an HTML object, the table will get the last children of it, say:

<a href="#someLink">
  <span className="span-class">
      <b>
         Text
      </b>
  </span>
</a>

In this case, the .getLastChildren() should return the "Text" string, just like the example on https://imballinst.github.io/react-bs-datatable/. Would you like to tell me the HTML object you are trying to display so I can try reproduce it? Thanks.

Styvak commented 7 years ago

It works thanks ! I just have to add a text at last children

imballinst commented 7 years ago

You're welcome! My pleasure to help :) Thanks for reporting this, too, I realized some points that I missed when I made the filterData() function (will fix next version):

  1. I should have used .isPropFilterable() at the beginning of the loop instead of at the end, so your case when filterable is set to false already but still outputs an error won't happen
  2. I should have handled the case when the HTML Object doesn't contain any string