开发者

jquery .each function to check another selector and delete duplicates

开发者 https://www.devze.com 2023-04-04 01:15 出处:网络
I have a jquery datatable that contains name and count data. <tr> <th>Name</th> <th class=\"count\">Count</th>

I have a jquery datatable that contains name and count data.

                <tr>
                    <th>Name</th>
                    <th class="count">Count</th>
                </tr>
              </thead>
            <tbody>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>               
              <td class= "name"><%# Eval("Name") %></td>
              <td class= "count"><%# Eval("count") %></td>             
            </tr>

The issue i am facing is trying to retrieve data that matches a certain criteria based on 2 selectors. For instance, how would i write a jquery function to retrieve names that are "john" and have a count of 4? I am able to retrieve positions where all names that are john as follows:

 $('td').each(function () {        
         var aPos = oTable.fnGetPosition(this);
         var aData = oTable.fnGetData(aPos[0]);
         var retrievedEntry = aData[aPos[1]];

         if (retrieved开发者_开发问答Entry == "john") {
             alert($(this).html() + "found at position " + aPos);
         }
 });

I tried alert($(this).find(".count").filter(":contains("1")")); as a test but keep getting null values.

Also once the data is retrieved, how would i delete all the duplicates and jus leave one record? Your help is much appreciated.


You can get the cells that contain "john", and then filter that result to only contain the ones that have "4" in the next element:

$('td.name:contains(john)').filter(function(){ return $(this).next().text() == '4'; })

Note: The :contains selector makes a partial match, so it would also match for example "johnny". If that is a problem, you would put that check in the filter also:

$('td.name').filter(function(){ return $(this).text() == 'john' && $(this).next().text() == '4'; })


1) You are using double-quotes inside and outside of the selector. Consider: ':contains("1")'

2) You need to specify selectors one-by-one without spaces

'.count:contains("1")'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号