DataTable: Sort by Last Name

by Austin Adair

HTML

<link rel="stylesheet" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<script src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<input type="button" id="btn-filter" value="click to select only condition1"/>
<input type="button" id="btn-undo" value="click to undo what '#btn-filter' did"/>
<br/><br/>

<table id="table"></table>

JavaScript

let data = [{
  "item1": "this is item 1 - data set 1",
  "item2": "this is item 2 - meets condition1"
}, {
  "item1": "this is item 1 - data set 2",
  "item2": "this is item 2"
}, {
  "item1": "this is item 1 - data set 3",
  "item2": "this is item 2 - meets condition2"
}, {
  "item1": "this is item 1 - data set 4",
  "item2": "this is item 2 - meets condition1"
}, {
  "item1": "this is item 1 - data set 5",
  "item2": "this is item 2"
}, {
  "item1": "this is item 1 - data set 6",
  "item2": "this is item 2"
}, {
  "item1": "this is item 1 - data set 7",
  "item2": "this is item 2 - meets condition1"
}, {
  "item1": "this is item 1 - data set 8",
  "item2": "this is item 2 - meets condition2"
}, {
  "item1": "this is item 1 - data set 9",
  "item2": "this is item 2"
}];

$('#table').DataTable({
  data: data,
	columns: [
    { "data": "item1",					  
			"render": function ( data, type, row ) {
				if(type === 'display'){
					return "<span class='format1'>"+data+"</span>";
				}else if(type === 'sort'){
					return data;
				}else if(type === 'filter'){
					return data;
				}else{
					return data;
				}
			}
		},
    { "data": "item2",					  
			"render": function ( data, type, row ) {
				if(type === 'display'){
					return "<span class='format2'>"+data+"</span>";
				}else if(type === 'sort'){
					return data;
				}else if(type === 'filter'){
					return data;
				}else{
					return data;
				}
		  }
    }],
	createdRow: function ( row, data, index ) {
		if (data.item2 == 'this is item 2 - meets condition1'){
            $(row).addClass('condition1');
        }
		if (data.item2 == 'this is item 2 - meets condition2'){
            $(row).addClass('condition2');
        }
    }
});

$('#btn-filter').on('click',function(){
	//// only show table data that contains the class condition1
});
$('#btn-undo').on('click',function(){
	//// remove the filter that was applied with btn-filter
});