jQuery datatables select rows with specific data from dataset

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


jQuery datatables select rows with specific data from dataset



I'm using jQuery DataTables and looking for a way to use a button to select rows from the entire dataset that contain a specific value (in this case "foo").



This is the script I'm using to populate my table:


<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/pdfmake-0.1.18/dt-1.10.12/b-1.2.2/b-html5-1.2.2/b-print-1.2.2/r-2.1.0/se-1.2.0/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/pdfmake-0.1.18/dt-1.10.12/b-1.2.2/b-html5-1.2.2/b-print-1.2.2/r-2.1.0/se-1.2.0/datatables.min.js"></script>

var oTable = $('#table').DataTable({
'ajax': {
url: 'script-to-return-json-row-data.php',
type: "POST",
dataSrc: function ( data ) {
return data;
},
'columns': [
{
"data": "name",
"render": function ( data, type, row ) {
return data;
}
]

}
});



The sample dataset receive from script-to-return-json-row-data.php looks something like this:


script-to-return-json-row-data.php



[
["name":"a-name-i-want-to-select","specific-value":"foo"],
["name":"a-name-i-dont-want-to-select","specific-value":"bar"]
]


[
["name":"a-name-i-want-to-select","specific-value":"foo"],
["name":"a-name-i-dont-want-to-select","specific-value":"bar"]
]



In the past I've been able to use the script below to select the rows that contain a specific class


$('#select-specific-values-button').click(function(e){
oTable.rows( {search:'applied'} ).every(function(rowIdx, tableLoop, rowLoop){
if($(this.node()).hasClass('class-name')){
$(this.node()).addClass('selected');
}
});
});



However I'm wondering if there's a way to modify the code above to only select rows where the row data specific-value is equal to foo. Any ideas on how I can do this?


specific-value


foo



I know the code below wont work but this should give a good idea what I'm trying to accomplish:


$('#select-specific-values-button').click(function(e){
oTable.rows( {search:'applied'} ).every(function(rowIdx, tableLoop, rowLoop){
// if($(this).rowIdx.data.specific-value == 'foo'){
// $(this.node()).addClass('selected');
// }
});
});





you can use individual column searching.
– yash
yesterday




1 Answer
1



This ended up working for me:


$('#select-specific-values-button').click(function(e){
oTable.rows( {search:'applied'} ).every(function(rowIdx, tableLoop, rowLoop){
if(oTable.row( rowIdx ).data().specific-value == 'foo'){
$(this.node()).addClass('selected');
}
});
});






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS