简体   繁体   English

搜索过滤器不适用于IE 8

[英]Search filter not working on IE 8

I'm using http://codepen.io/anon/pen/gKrln have copy the code and mix it up with my table data. 我正在使用http://codepen.io/anon/pen/gKrln复制代码并将其与我的表数据混合。 It works on firefox and chrome. 它适用于Firefox和Chrome。

I have change: 我有零钱:

 document.getElementsByClassName('light-table-filter2');

TO inputs = $(".light-table-filter"); TO inputs = $(".light-table-filter");

but im not sure how i would change document.getElementsByClassName(_input.getAttribute('data-table')); 但我不确定我将如何更改document.getElementsByClassName(_input.getAttribute('data-table')); to make it work on IE8? 使它在IE8上正常工作?

with the changes I have made only work on firefox and chrome , how can I get it to work on IE8? 通过所做的更改,我只能在firefox和chrome上使用,如何在IE8上使用它?

var LightTableFilter;

LightTableFilter = (function() {
  var _filter, _input, _onInputEvent;
  _input = null;
  _onInputEvent = (function(_this) {
    return function(e) {
      var row, table, tables, tbody, _i, _j, _k, _len, _len1, _len2, _ref, _ref1;
      _input = e.target;

      tables = document.getElementsByClassName(_input.getAttribute('data-table'));
      for (_i = 0, _len = tables.length; _i < _len; _i++) {
        table = tables[_i];
        _ref = table.tBodies;
        for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
          tbody = _ref[_j];
          _ref1 = tbody.rows;
          for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
            row = _ref1[_k];
            _filter(row);
          }
        }
      }
    };
  })(this);
  _filter = function(row) {
    var len, n, text, val, vals, _i, _len;
    text = row.textContent.toLowerCase();
    vals = _input.value.toLowerCase().split(' ');
    len = vals.length;
    n = 0;
    for (_i = 0, _len = vals.length; _i < _len; _i++) {
      val = vals[_i];
      if (text.indexOf(val) !== -1) {
        n++;
      }
    }
    return row.style.display = n === len ? 'table-row' : 'none';
  };
  return {
    init: function() {
      var input, inputs, _i, _len, _results;
      inputs = $(".light-table-filter");

      _results = [];
      for (_i = 0, _len = inputs.length; _i < _len; _i++) {
        input = inputs[_i];
        _results.push(input.oninput = _onInputEvent);
      }
      return _results;
    }
  };
})();

Here is the body : 这是身体:

<section class="container">

    <input type="search" class="light-table-filter" data-table="order-table"  />

    <table  class="order-table"  >
      <thead><th>Name</th><th>Department</th><th>Ext:</th><th>Email</th><th>Title Name</th><th>Cell Phone</th></thead>
    <tbody>
    <cfoutput query="Branch"  >
    <tr>

        <td >#emp_namefirst#  </td> 
    <td >#dept_name#</td>       
        <td >#emp_ext#</div></td>
        <td >#EMP_EMAIL#  </td>

    </tr>
    </cfoutput>
    </tbody>
    </table>

</section>

When you look to support a modern script in a legacy browser, you'll definitely have a few areas in which you'll need to make some adjustments. 当您希望在旧版浏览器中支持现代脚本时,肯定会在某些方面需要进行一些调整。 As you've properly identified, getElementsByClassName is not going to be available in Internet Explorer 8 - you therefore made a wise decision to go with jQuery , which can provide comparable features in legacy environments. 如您getElementsByClassNamegetElementsByClassName在Internet Explorer 8中将不可用-因此,您明智地选择了jQuery ,它可以在旧版环境中提供可比的功能。

Other things you'll be giving up are Array.prototype.forEach , addEventListener , and textContent . 您将放弃的其他东西是Array.prototype.forEachaddEventListenertextContent Fortunately, these can all be replaced with jQuery.fn.each , jQuery.fn.on , and jQuery.fn.text respectively. 幸运的是,这些都可以分别用jQuery.fn.eachjQuery.fn.onjQuery.fn.text替换。 You also don't have access to an input event either until IE9. IE9之前,您也无权访问input事件 We'll use the proprietary propertychange event in as a fallback to the input event. 我们将使用专有的propertychange事件作为input事件的后备。

Making these changes, we see a greatly reduced script: 进行这些更改后,我们看到了一个大大减少的脚本:

(function () {

    "use strict";

    var LightTableFilter = (function () {

        var _input, _row;

        function _onInputEvent ( event ) {
            _input = $( this );
            $( "tr:gt(0)", "." + _input.attr( "data-table" ) ).each( _filter );
        }

        function _filter () {
            _row = $( this );
            _row.toggle( _row.text().indexOf( _input.val() ) > -1 );
        }

        return {
            init: function () {
                $( ".light-table-filter" ).on( "input propertychange", _onInputEvent );
            }
        };

    })();

    $( LightTableFilter.init );

})();

Be sure that you use a 1.x version of jQuery, as the 2.x branch is intended for Internet Explorer 9 and up. 确保使用1.x版本的jQuery,因为2.x分支适用于Internet Explorer 9及更高版本。 You can test the above script online at: http://jsfiddle.net/md18wvy2/15/ . 您可以在以下网址在线测试上述脚本: http : //jsfiddle.net/md18wvy2/15/

If the above structure isn't necessary needing to be preserved, you could collapse it down even further to something resembling the following: 如果不需要保留上述结构,则可以将其折叠成类似于以下内容的形式:

(function ( $ ) {

    "use strict";

    // By passing our init function into $, it will be caused when the DOM is ready
    $(function init () {
        // Call our filter function on every input event of any .light-table-filter
        $( ".light-table-filter" ).on( "input propertychange", function filter ( event ) {
            // Find every tr in the corresponding table, and loop over them
            $( "tr", "." + $( this ).attr( "data-table" ) ).each(function toggle () {
                var row = $( this );
                // Show/Hide current tr based on presence of a substring
                row.toggle( row.text().indexOf( event.target.value ) > -1 );
            });
        });
    });

})( jQuery );

You can test this online at http://jsfiddle.net/md18wvy2/14/ . 您可以在http://jsfiddle.net/md18wvy2/14/在线进行测试。

The above approaches both use case- sensitive filtering. 上面的方法都使用区分大小写的过滤。 If you'd like to make the filtering case- insensitive you would need to follow the original script's direction and normalize the output before checking for substrings: 如果要使过滤不区分大小写 ,则需要在检查子字符串之前遵循原始脚本的方向并对输出进行规范化:

_row.text().toLowerCase().indexOf( _input.val().toLowerCase() )
var myAttribute = $(_input).attr('data-table');
var myElements = $('.' + myAttribute);

Or simply 或者简单地

var myElements = $('.' + $(_input).attr('data-table'));

If you choose to go down the jQuery route 如果您选择沿着jQuery路线走

var className = $(_input).attr('data-table');
tables = $('.' + className);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM