0

I'm using this code in order to add a filter field to all my tables. This filter is added to my navbar using JavaScript.

  // Table filter.
  // Source: https://codepen.io/chriscoyier/pen/tIuBL
  (function(document) {
    'use strict';

    var LightTableFilter = (function(Arr) {

      var _input;

      function _onInputEvent(e) {
        _input = e.target;
        var tables = document.getElementsByClassName(_input.getAttribute('data-table'));
        Arr.forEach.call(tables, function(table) {
          Arr.forEach.call(table.tBodies, function(tbody) {
            Arr.forEach.call(tbody.rows, _filter);
          });
        });
      }

      function _filter(row) {
        var text = row.textContent.toLowerCase(), val = _input.value.toLowerCase();
        row.style.display = text.indexOf(val) === -1 ? 'none' : 'table-row';
      }

      return {
        init: function() {
          var inputs = document.getElementsByClassName('light-table-filter');
          Arr.forEach.call(inputs, function(input) {
            input.oninput = _onInputEvent;
          });
        }
      };
    })(Array.prototype);

    document.addEventListener('readystatechange', function() {
      if (document.readyState === 'complete') {
        LightTableFilter.init();
      }
    });

  })(document);

The code itself works perfectly.

However, PhpStorm adds a warning to document.readyState saying that it's an unresolved variable.

Is PhpStorm missing some information? I downloaded and installed it yesterday, so it should be the most recent version.


I fixed it by replacing the following code:

document.addEventListener('readystatechange', function() {
  if (document.readyState === 'complete') {
    LightTableFilter.init();
  }
});

With this:

  document.addEventListener("DOMContentLoaded", function (event) {
      LightTableFilter.init();
  })
LazyOne
  • 137,403
  • 37
  • 338
  • 342
RubbelDieKatz
  • 902
  • 1
  • 11
  • 26

1 Answers1

1

Well, why do you think that PhpStorm will automatically know, that your document variable is the browsers instance of document? You're right, readyState is a property of document inside a browser, but for PhpStrom it's unresolved because it's not initialized.

eisbehr
  • 11,374
  • 7
  • 30
  • 56
  • Do you think [this](https://stackoverflow.com/a/800010/6203984) could be used as an alternative? – RubbelDieKatz Jul 21 '17 at 07:28
  • 1
    You mean `DOMContentLoaded`? Of course, this can be used too. @RubbelDieKatz – eisbehr Jul 21 '17 at 07:34
  • 1
    But to be clear, there in no error in general in your code. It's just a warning in PhpStorm. You could suppress this warning with `// noinspection JSUnresolvedVariable` too, for example, or just ignore it. @RubbelDieKatz – eisbehr Jul 21 '17 at 07:36
  • The solution I just added to my question is two lines shorter and works in most browsers, so I'd rather use that one. Also, I don't need to suppress any inspections. – RubbelDieKatz Jul 21 '17 at 07:38