给出一定标准的隐藏行(慢)

时间:2010-07-08 13:50:16

标签: jquery performance select html-table hide

如果其中的输入符合某个条件,我试图隐藏在html表中的tr。 标准由下拉列表的选定值定义。 我是这样做的:

$(function () {

  $('body').find('#p_Selection').live('change', function () {

    var type = $('body').find('#p_Selection').attr('value');
    var tableRow = $('.goods').find('.detail-child tr');

    tableRow.each(function (index) {

      var Record_LidExpected = $('input[id$=Record[' + index + ']_LidExpected]').attr('value');
      var Record_LidObtained = $('input[id$=Record[' + index + ']_LidObtained]').attr('value');
      var Record_QuantityExpected = $('input[id$=Record[' + index + ']_QuantityExpected]').attr('value');
      var Record_QuantityObtained = $('input[id$=Record[' + index + ']_QuantityObtained]').attr('value');

      if (type == "1") {

        if (Record_LidExpected != Record_LidObtained) {
          $(this).hide();
        }
        else {
          if (Record_QuantityExpected != Record_QuantityObtained) {
            $(this).hide();
          }
        }
      }
      else {
        if (type == "2") {
          if (Record_LidExpected == Record_LidObtained) {
            $(this).hide();
          }
          else {
            if (Record_QuantityExpected == Record_QuantityObtained) {
              $(this).hide();
            }
          }
        }
        else {
          if (type == "0") {
            $(this).show();
          }
        }
      }
    });
  });
});​

这个脚本在我的aspx页面内变得非常慢,但由于它太重而无法完成。 关于如何加快速度的任何建议?

2 个答案:

答案 0 :(得分:1)

绩效优化的关键点

  • 做几个选择(不要选择所有onchange
  • 保存不会改变的选择
  • 选择元素时尽可能具体

额外奖励:学会使用else if因为你的分支变得更加清晰。

以下是代码:

$(function () {

  // pre-select things that won't change
  var $sel = $('#p_Selection'); 
  var tableRow = $('.goods .detail-child tr');

  //
  // ONCHANGE EVENT 
  // keep in mind that everything in it will be very costly
  //
  $sel.live('change', function () {

    var type = $sel.attr('value');

    tableRow.each(function (index) {

      // this just makes the code shorter
      var record = '#Record'+index;

      // simple ID selector is enough, since its unique
      var Record_LidExpected =  $(record+'_LidExpected').attr('value');
      var Record_LidObtained =  $(record+'_LidObtained').attr('value');
      var Record_QuantityExpected = $(record+'_QuantityExpected').attr('value');
      var Record_QuantityObtained = $(record+'_QuantityObtained').attr('value');

      if (type == "1") {
        if (Record_LidExpected != Record_LidObtained) {
          $(this).hide();
        } else if (Record_QuantityExpected != Record_QuantityObtained) {
          $(this).hide();
        }
      } else if (type == "2") {
        if (Record_LidExpected == Record_LidObtained) {
          $(this).hide();
        } else if (Record_QuantityExpected == Record_QuantityObtained) {
          $(this).hide();
        }
      } else if (type == "0") {
        $(this).show();
      }
    });
  });
});

答案 1 :(得分:0)

在我的改写中你会注意到一些事情。

  1. 我省略了$('body').find()因为它没有比直接选择#p_Selection更有优势。
  2. 使用input函数选择$()值时,我添加this作为第二个参数。这基本上告诉jQuery在this中查找输入(在这种情况下是指每个循环中的当前tr。这里的优点是jQuery不需要搜索整个DOM该特定输入,仅在当前tr标记内。
  3. 我刚刚使用switch语句和一些||运算符清理了if / else逻辑。
  4. 我认为现在应该跑得更快。

    <script type="text/javascript">
      $(function() {
    
        $('#p_Selection').live('change', function() {
          var type = $(this).val();
    
          var Record_LidExpected, 
              Record_LidObtained, 
              Record_QuantityExpected, 
              Record_QuantityObtained;
    
          $('.goods .detail-child tr').each(function(index) {
    
            Record_LidExpected = $('input[id$=Record['+index+']_LidExpected]', this).val();
            Record_LidObtained = $('input[id$=Record[' + index + ']_LidObtained]', this).val();
            Record_QuantityExpected = $('input[id$=Record[' + index + ']_QuantityExpected]', this).val();
            Record_QuantityObtained = $('input[id$=Record[' + index + ']_QuantityObtained]', this).val();
    
            switch(type) {
              case "1" :
                if (Record_LidExpected != Record_LidObtained || Record_QuantityExpected != Record_QuantityObtained) {
                  $(this).hide();
                }
              break;
              case "2" :
                if (Record_LidExpected == Record_LidObtained || Record_QuantityExpected == Record_QuantityObtained) {
                  $(this).hide();
                }
              break;
    
              case "0":
                $(this).show();
              break;
            }
          });
        });
      });
    </script>