Jquery删除几乎相同的表行

时间:2013-01-31 19:18:08

标签: javascript jquery tablesorter

我在html中有表.... 我需要:如果我有两个或更多几乎相同的行(首先检查大写,没有空格和其他symbold ...只是数字和字母),我必须删除所有,并保留一个,但只有这样,我有一些td中的值... 看看例子:

Bosch | 1234s | details
BOSCH | 1234S | 
ATE | 1215 | details

我必须只保留这样的表格:

Bosch | 1234s | details
ATE | 1215 | details

如果有相同的(有大写但没有空格和其他符号......只是数字和字母)然后删除所有相同的行,并保持这样,在第三行我有详细信息......

我不擅长javascript ....请帮助我(只是不减,或关闭)...我google了一些排序...但是如何删除几乎重复没有不知道......

现在我也有了tableorter:

$(document).ready(function() { 
        $(".sortable") 
        .tablesorter({sortList: [[4,0]], widgets: ['zebra']});
    }); 

1 个答案:

答案 0 :(得分:0)

开始this answer并使用您的表格。

<table cellpadding="0" cellspasing="0" class="sortable zebra tablesorter tablesorter-default" id="articles-table">
              <thead>
                <tr class="tablesorter-headerRow">
                  <th data-column="0" class="tablesorter-header"><div class="tablesorter-header-inner">Производитель<i class="tablesorter-icon"></i></div></th>
                  <th data-column="1" class="tablesorter-header"><div class="tablesorter-header-inner">Артикул<i class="tablesorter-icon"></i></div></th>
                  <th data-column="2" class="tablesorter-header"><div class="tablesorter-header-inner">Описание<i class="tablesorter-icon"></i></div></th>
                  <th data-column="3" class="tablesorter-header"><div class="tablesorter-header-inner"><i class="tablesorter-icon"></i></div></th>
                  <th class="prcol  tablesorter-header tablesorter-headerSortDown" data-column="4"><div class="tablesorter-header-inner">Цена<i class="tablesorter-icon"></i></div></th>
                  <th data-column="5" class="tablesorter-header"><div class="tablesorter-header-inner">Наличие<i class="tablesorter-icon"></i></div></th>
                  <th data-column="6" class="tablesorter-header"><div class="tablesorter-header-inner">Сроки поставки<i class="tablesorter-icon"></i></div></th>
                  <th data-column="7" class="tablesorter-header"><div class="tablesorter-header-inner"><i class="tablesorter-icon"></i></div></th>
                </tr>
              </thead>
              <tbody class="analogs_art">


             <tr class="zebra-stripe zebra-grey zebra2 even">
                  <td>
                    <h5>
                      Knecht
                    </h5>
                  </td>
                  <td>
                    <h4>
                      OC90
                    </h4>
                  </td>
                  <td style="max-width: 300px;">
                    (07786171)Фильтр масл OPEL
                    (07786171)Фильтр масл OPEL
                  </td>
                  <td>
                  </td>
                  <td>
                    6124
                  </td>
                  <td>
                    20
                  </td>
                  <td>
                    0-1
                  </td>
                  <td id="to_cart_and_qnt_td">


                      <form accept-charset="UTF-8" action="/line_items" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="0jlRLSkDKi6eM5SSL+3uRB0w2G141KUcJKuy4Bg2YNo="/></div>
                          <input id="ART" name="ART" type="hidden" value="y1M1ZtdXgnMe6pOjjuXWPqhYR41SmQRKzlDOQ1uPwuru+AamPGpcOwOFFylbRDu5M1XUawCnukqNDQCuIr0JPA==--vNGx6/lYKRrpD5NGTJALcQ=="/>

                          <input id="price" name="price" type="hidden" value="TodzvfOZCbK5T9+RLE1IQg==--qq8o30c5rTX91F8W42wdsQ=="/>
                          <input id="distributor_id" name="distributor_id" type="hidden" value="6"/>
                          <input id="waittime" name="waittime" type="hidden" value="1"/>
                          <input class="qnt_to_cart2" id="OC90" name="cart_quantity" pattern="0|[1-9]\d{0,2}" required="required" type="text" value="1"/>
                          <input class="orange-button to-cart_no" id="OC90" name="commit" type="submit" value="В корзину"/>
                    </form>
                  </td>
                </tr><tr class="zebra-stripe zebra-grey zebra2 odd">
                  <td>
                    <h5>
                      KNECHT

                    </h5>
                  </td>
                  <td>
                    <h4>
                      OC 90
                    </h4>
                  </td>
                  <td style="max-width: 300px;">
                    Масляный фильтр
                  </td>
                  <td>
                    <a href="/articles/1248525/details" class="details-link">подробнее</a>
                    <a href="/articles/1248525" class="analogs-link">аналоги</a>
                  </td>
                  <td>
                    6124
                  </td>
                  <td>
                    20
                  </td>
                  <td>
                    0-1
                  </td>
                  <td id="to_cart_and_qnt_td">
                    <form accept-charset="UTF-8" action="/line_items" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="0jlRLSkDKi6eM5SSL+3uRB0w2G141KUcJKuy4Bg2YNo="></div>
                            <input id="ART" name="ART" type="hidden" value="9x6mdLbW2aMaHqFK32kZwHWnWA7n0GBp/jXS8KeECsTS+78ytG0SQzFpVODplws5iHm6B6SkZzi3ej2+zpW8iQ==--RNoUUgcZu7MLU8BHJ4ynSQ==">
                            <input id="price" name="price" type="hidden" value="VqatEYzkPEf3P9Y/jNY9Zg==--Iw5nQDqjK9vvZX/RecD1eA==">
                            <input id="distributor_id" name="distributor_id" type="hidden" value="6">
                            <input id="waittime" name="waittime" type="hidden" value="1">
                            <input class="qnt_to_cart" id="cart_quantity" name="cart_quantity" pattern="0|[1-9]\d{0,2}" required="required" type="text" value="1">
                            <input class="orange-button to-cart" name="commit" type="submit" value="В корзину">
                    </form>
                  </td>
                </tr><tr class="zebra-stripe zebra-grey zebra1 even">
                  <td>
                    <h5>
                      Knecht
                    </h5>
                  </td>
                  <td>
                    <h4>
                      OC90
                    </h4>
                  </td>
                  <td style="max-width: 300px;">
                    Фильтр масляный OPEL/GM/DAEWOO
                    Фильтр масляный OPEL/GM/DAEWOO
                  </td>
                  <td>
                  </td>
                  <td>
                    6142
                  </td>
                  <td>
                    1000
                  </td>
                  <td>
                    3
                  </td>
                  <td id="to_cart_and_qnt_td">


                    <form accept-charset="UTF-8" action="/line_items" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="0jlRLSkDKi6eM5SSL+3uRB0w2G141KUcJKuy4Bg2YNo="></div>
                            <input id="ART" name="ART" type="hidden" value="NqgWSvcdv0FEQkjcJZp98gIYe20Fjol8Bn31tB/CjWnTFVsa3J3hmldO1RSqS/pNUcVZ5dh0wcP7YeZxPAkQc4DEPKy7EVuTpMw3oQw58T0=--/XCn0hP3w0mglJ1vTXSjHw==">

                            <input id="price" name="price" type="hidden" value="mFPLSHh7AdEMpQJiZHFKNg==--hfJQGWhjBoD6mNAetXZ3Xw==">
                            <input id="distributor_id" name="distributor_id" type="hidden" value="6">
                            <input id="waittime" name="waittime" type="hidden" value="3">
                            <input class="qnt_to_cart2" id="OC90" name="cart_quantity" pattern="0|[1-9]\d{0,2}" required="required" type="text" value="1">
                            <input class="orange-button to-cart_no" id="OC90" name="commit" type="submit" value="В корзину">
                    </form>
                  </td>
                </tr></tbody>
            </table>

jQuery代码是

var seen = {};
    $('table tr').each(function() {
        var tr = $(this).clone();
        if(tr.not(':input')){
            if(tr.not('a.details-link')){
                tr.find("td:eq(7)").remove();
                tr.find("td:eq(3)").remove(); 
               // tr.find("td:eq(2)").remove();
                var txt = tr.text().toLowerCase().replace(/\s+/g, '');;        
                if (seen[txt])
                    $(this).remove();
                else
                    seen[txt] = true;
            }
        }
    });

功能示例是here