简体   繁体   English

为什么CSS样式无法在javascript中申请匹配行?

[英]Why css styles are not applying for matching rows in javascript?

If records are matching with searchtext need to hightlight that entire row but not applying that css style. 如果记录与searchtext匹配,则需要突出显示整个行,但不应用该CSS样式。

My javascript function 我的JavaScript函数

  $(function () {
    grid = $('#tblsearchresult tbody');
    // handle search fields key up event
    $('#search-term').keyup(function (e) {
      text = $(this).val().trim(); // grab search term
      if (text.length > 1) {
        grid.find('tr:has(td)').css({ background: "" });
        grid.find('tr').show();
        // iterate through all grid rows
        grid.find('tr').each(function (i) {
          // check to see if search term matches ApplicationName column
          if ($(this).find('td:first-child').text().toUpperCase().match(text.toUpperCase()))
              $(this).addClass('result');
          // $(this).css({ background: "#A4D3EE" });
          // check to see if search term matches RoleName column
          if ($(this).find("td:eq(1)").text().toUpperCase().match(text.toUpperCase()))
              $(this).addClass('result');
        });
      }
      else {
        grid.find('tr:has(td)').css({ background: "" });
        grid.find('tr').show();
      } // if no matching name is found, show all rows
    });
  });
  $('table').tablesorter();

My CSS : 我的CSS:

  table.tablesorter tbody td.result {
    background: #A4D3EE;
  }
  table.tablesorter {
    font-family:arial;
    color: rgb(51, 51, 51);
    margin:10px 0pt 15px;
    font-size: 10pt;
    width: 100%;
    text-align: left;
  }
  table.tablesorter thead tr th, table.tablesorter tfoot tr th {
    background-color: #8dbdd8;
    border: 1px solid #FFF;
    font-size: 10pt;
    padding: 5px;
  }
  table.tablesorter thead tr .header:not(.nosort) {
    background-image: url('/sorter/bg.gif');  
    background-repeat: no-repeat;
    background-position: center right;
    cursor: pointer;
  }
  table.tablesorter tbody td {
    background-color: rgb(239, 243, 251);
    padding: 5px;
    border: solid 1px #e8eef4;

    vertical-align: top;
  }
  table.tablesorter tbody tr.odd td {
    background-color:#F0F0F6;
  }
  table.tablesorter thead tr .headerSortUp:not(.nosort) {
    background-image: url('/sorter/asc.gif'); 
  }
  table.tablesorter thead tr .headerSortDown:not(.nosort) {
    background-image: url('/sorter/desc.gif'); 
  }
  table.tablesorter thead tr .headerSortDown, table.tablesorter thead tr .headerSortUp {
    background-color: #8dbdd8;
  }

UI Design : 用户界面设计:

<table id="tblsearchresult" class="tablesorter">
  <thead> 
    <tr>
    </tr>
  </thead>
  <tbody></tbody>
</table>

Table Data : 表格数据:

applicationame   role
application1     appadministrator
app              developer
application2      tester

if i given 'app'as search text need to highlight secondrow only .highlightling firstrow also because 'app' is there in role of firstrow..exact match should be highlight on every rows.please tell me. 如果我给'app'作为搜索文本,则仅需突出显示第二行。突出显示第一行也是因为'app'充当firstrow的角色。精确匹配应在每一行上突出显示。请告诉我。

Please check my code i need to highlight the matching record row .if searchtext matching with table column data need to highlight the entire row.but not applying css in above code. 请检查我的代码,我需要突出显示匹配的记录行。如果与表列数据匹配的searchtext需要突出显示整个行。但不要在上述代码中应用CSS。

It seems like you are not applying the result class to the correct element. 似乎您没有将结果类应用于正确的元素。 In your CSS, the following line defines the result class for TDs: 在您的CSS中,以下行定义了TD的结果类:

table.tablesorter tbody td.result table.tablesorter tbody td.result

But in your javascript, this line will apply it to the table row: 但是在您的javascript中,这一行会将其应用于表格行:

$(this).addClass('result'); $(this).addClass('result');

So by changing this line to 因此,将这一行更改为

$(this).children('td').addClass('result'); $(this).children('td')。addClass('result');

you should be okay. 你应该没事的

Update: Based on your feedback, I've created an example on jsFiddle for you: http://jsfiddle.net/kUxNj/4/ 更新:根据您的反馈,我在jsFiddle上为您创建了一个示例: http : //jsfiddle.net/kUxNj/4/

  // check to see if search term matches ApplicationName column if ($(this).find('td:first-child').text().toUpperCase() === text.toUpperCase()) $(this).children('td').addClass('result'); // check to see if search term matches RoleName column if ($(this).find("td:eq(1)").text().toUpperCase() === text.toUpperCase()) $(this).children('td').addClass('result'); 

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

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