Viral
Viral

Reputation: 320

Find Duplicate Records in HTML Table

I am using DataTable pulgin and had question about adding duplicate row.

When user add a record to (HTML) table I want to check if that record already exists in Table (on client side).

For example:

       Column A
Row 1  ABC

Now if user try to add "ABC", I want to throw error.

Can anyone provide pointer how to achieve this using jQuery or Datatables?

Upvotes: 2

Views: 5731

Answers (3)

Pierre
Pierre

Reputation: 19071

function findInTable(str, tableID){
    $('#' + tableID + ' tr').each(function(){
        $(this).children('td').each(function(){
            if ( $(this).html() == str ){
                alert('found');
                return false;
            }
        });
    }); 
}

findInTable('ABC', 'mytable'); // <table id="mytable">...</table>

Upvotes: 2

Adithya Surampudi
Adithya Surampudi

Reputation: 4454

There is a hacky way to do this for smaller tables. Convert the rows into strings, and put them in an associative array, works best for single column tables, and there are ways to work with multiple columns

Hence lets say you insert ABC

if (tableData["ABC"] != undefined) tableData["ABC"] = 1;
else alert("Duplicate");

Also if loop should take care of adding the row to the UI

Upvotes: 0

Vinit
Vinit

Reputation: 1825

This should solve your problem. Tweak this

<script type="text/javascript">
<!--
function cellContent() {
  var content=document.getElementsByTagName('td');
for(c=0;c<content.length;c++) {
   alert ('td cell number '+(c+1)+' contains...\n ' +content[c].innerHTML);
  }
 }
onload=cellContent;
//-->
</script>

Upvotes: 0

Related Questions