I am getting data from database through ajax & PHP, the returned data is shown as a table, i created a function to sort table column ascending or descending, i want to add arrows beside the column name to show up in case of ascending order
& down in case of descending order
, how can this be done?
HTML Code
<head>
<link rel="stylesheet" href="../lib/css/bootstrap.min.css" type="text/css"/>
<script src="https://use.fontawesome.com/d7a6f6be38.js"></script>
<script type="text/javascript" src="../lib/js/jquery-3.1.1.min.js"></script>
<link href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
</head>
<div class="row">
<div id="OrderTable"></div>
</div>
Part of PHP page searchorders.php
<?php
$output.='<hr />
<table id="SearchResults" class="table table-striped table-bordered" style="width:auto;">
<thead>
<tr>
<th>Order No.</th>
<th>Customer Name</th>
<th>Order Date</th>
<th>Category Name</th>
<th>Category Type</th>
<th>Quantity in Kilos</th>
<th>Delivery Date</th>
<th>Order Status</th>
</tr>
</thead>
';
echo $output;
?>
Javascript Function
function SearchOrders()
{
document.getElementById("OrderTable").innerHTML="";
document.getElementById("NoData").innerHTML="";
document.getElementById("Results").style.display = 'none';
startDate=document.getElementById("Start_Date").value;
endDate=document.getElementById("End_Date").value;
$.ajax({
type:"POST",
url:"searchorders.php",
data:
{
'StartDate': startDate,
'EndDate':endDate
},
success: function(data)
{
if (data == "Failed")
{
$('#NoData').append("No data Found!");
}
else
{
$('#OrderTable').append(data);
document.getElementById("Results").style.display = 'block';
$('#SearchResults').DataTable({"columnDefs": [ {
"targets": [0,4,5,6],
"orderable": false
}]});
}
}
})
}
First add up(for ascending) and down(for descending) arrows. I am using text inside span
here which you can replace with icons or something but keep the id
on them the same.
<th onclick="sortTable(1)">
<span id="asc1">Up</span>
<span id="desc1">Down</span>
Customer Name
</th>
<th onclick="sortTable(2)">
<span id="asc2">Up</span>
<span id="desc2">Down</span>
Order Date
</th>
<th onclick="sortTable(3)">
<span id="asc3">Up</span>
<span id="desc3">Down</span>
Category Name
</th>
Then hide them in the css and style them more as you like
th>span{
display:none;
}
Then write the jquery code since you are passing n
to the function and you know the direction of sort too. so just reverse engineer the id and show that arrow.
$('th>span').hide();
var idChange = '#'+ dir + n;
$(idChange).show();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.