Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

javascript - Jquery delete almost same table rows

I have table in html.... I need to: if i have two or more almost identical rows (first check with uppercase and without spaces and other symbold... just numbers and letters), i must delete all, and keep one, but only such, where i have some value in td... Take look at example:

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

i must keep only such table:

Bosch | 1234s | details
ATE | 1215 | details

Just if there are same (with uppercase and without spaces and other symbold... just numbers and letters) then delete all same rows, and keep such, where in third row i have details...

I'm not good in javascript.... Please help me (just do not minus, or close)... I googled some sortings... But how to delete almost duplicates didn't know....

also now i have tablesorter:

$(document).ready(function() { 
        $(".sortable") 
        .tablesorter({sortList: [[4,0]], widgets: ['zebra']});
    }); 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Starting for this answer and using your table.

<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>

And the jQuery code is

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;
            }
        }
    });

An functional example is here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...