Jovs
Jovs

Reputation: 892

How to check if value exist in datatable

Is there a way to find a specific value inside of existing datatable? After finding it I want to like put the information of row in a single var for example its like selected then getting the data like this

$('#datatable').DataTable().rows({selected:true}).data();

now I want it to work like searching if I input two and the datatable has that value on specific column It will give me the true in if else, Then I want it to store that value in a var in javascript.

I also know how to get all the data in datatable. I just don't know how to get the data of a single row using if else. Thanks for the help!

EDIT:

Here's what I want to happen

The user will input a data > The System will check if that data is existing in datatable > If not existing error will occur > If existing, Store the whole row in var e.g: var data = //the whole row data

Upvotes: 3

Views: 11568

Answers (1)

User863
User863

Reputation: 20039

Try using {search: 'applied'} selector-modifier

var table = $('#example').DataTable()

console.log(table.search('Tokyo').row({search: 'applied'}).data())
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />


<table id="example" class="display" style="width:100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Numero</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>155555</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
    </tr>
    <tr>
      <td>Garrett Winters</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>63</td>
      <td>2011/07/25</td>
      <td>$170,750</td>
    </tr>
    <tr>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>
      <td>1</td>
      <td>2009/01/12</td>
      <td>$86,000</td>
    </tr>
  </tbody>
</table>

Upvotes: 2

Related Questions