I have a group of fields above my table and these fields are used to search my table. Here's a code snippet:
<?php $form = ActiveForm::begin(['id' => 'devicemgmt-form']); ?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-5">
<div class="row">
<div class="col-sm-5">Device Name</div>
<div class="col-sm-7">
<input type="text" name="device-name" class="form-control input-sm">
</div>
</div>
<br>
<div class="row">
<div class="col-sm-5">Broadcasted Program</div>
<div class="col-sm-7">
<select name="country" class="form-control input-sm">
<option selected disabled>Please Select</option>
<?php
foreach($devices as $key => $value):
echo '<option value="'.$key.'">'.$value['Broadcasted Program'].'</option>';
endforeach;
?>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-sm-5">Store</div>
<div class="col-sm-7">
<select class="form-control input-sm">
<option selected disabled>Please Select</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
</div>
</div>
<div class="col-sm-5">
<div class="row">
<div class="col-sm-5">Model name</div>
<div class="col-sm-7">
<select name="country" class="form-control input-sm">
<option selected disabled>Please Select</option>
<?php
foreach($devices as $key => $value):
echo '<option value="'.$key.'">'.$value['Device Model'].'</option>';
endforeach;
?>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-sm-5">Status</div>
<div class="col-sm-7">
<select class="form-control input-sm">
<option selected disabled>Please Select</option>
<option>Started</option>
<option>Pending</option>
<option>Complete</option>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-sm-5">Log Output Date</div>
<div class="col-sm-7">
<input type="text" name="device-name" class="form-control input-sm">
</div>
</div>
</div>
<div class="col-sm-2">
<div class="row" style="height: 98px;"></div>
<div class="row">
<button href="#" class="btn download-btn" id="downloadBtn">Log Download</button>
</div>
</div>
</div>
</div>
<?php ActiveForm::end(); ?>
<div class="container-fluid">
<table id="device-list_table" class="table table-striped table-bordered dataTable display">
<thead>
<tr>
<th class="text-center"><input type="checkbox" id="selectAll"></th>
<th class="text-center">Device Name</th>
<th class="text-center">Device Model</th>
<th class="text-center">Broadcasted Program</th>
<th class="text-center" colspan="2">Device Status</th>
<th class="text-center">Last Communication Date and Time</th>
<th class="text-center"></th>
</tr>
</thead>
<tbody>
<?php foreach($devices as $key => $value) { ?>
<tr>
<td class="text-center"><input type="checkbox" name="device-checkbox" value="<?= $value['Device Name'] ?>" id="<?php echo 'boxId'.$key ?>"></td>
<td class="text-center"><?= $value['Device Name']; ?></td>
<td class="text-center"><?= $value['Device Model']; ?></td>
<td class="text-center"><?= $value['Broadcasted Program']; ?></td>
<td class="text-center">
<?php
if ($value['Device Status'] == "Start") {
echo "<font color='yellow'>⚫</font>";
} elseif ($value['Device Status'] == "Pending") {
echo "<font color='red'>⚫</font>";
} elseif ($value['Device Status'] == "Complete") {
echo "<font color='green'>⚫</font>";
}
?>
</td>
<td class="text-center"><?= $value['Device Status']; ?></td>
<td class="text-center"><?= $value['Last Communication Date and Time']; ?></td>
<td class="text-center">
<ul class="list-inline">
<li>
<button href="#" class="btn">
Display Log
</button>
</li>
<li>
<button href="#" class="btn">
Edit
</button>
</li>
</ul>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
How do I search data in my table and display the results (inside the same table) without refreshing the page? As you can see, I have a "Log Download" button, maybe when I click that, the results will then show.
EDIT: Somewhat like the implementation of dataTables (/) but in my case, I have multiple form fields to use in searching.
I have a group of fields above my table and these fields are used to search my table. Here's a code snippet:
<?php $form = ActiveForm::begin(['id' => 'devicemgmt-form']); ?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-5">
<div class="row">
<div class="col-sm-5">Device Name</div>
<div class="col-sm-7">
<input type="text" name="device-name" class="form-control input-sm">
</div>
</div>
<br>
<div class="row">
<div class="col-sm-5">Broadcasted Program</div>
<div class="col-sm-7">
<select name="country" class="form-control input-sm">
<option selected disabled>Please Select</option>
<?php
foreach($devices as $key => $value):
echo '<option value="'.$key.'">'.$value['Broadcasted Program'].'</option>';
endforeach;
?>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-sm-5">Store</div>
<div class="col-sm-7">
<select class="form-control input-sm">
<option selected disabled>Please Select</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
</div>
</div>
<div class="col-sm-5">
<div class="row">
<div class="col-sm-5">Model name</div>
<div class="col-sm-7">
<select name="country" class="form-control input-sm">
<option selected disabled>Please Select</option>
<?php
foreach($devices as $key => $value):
echo '<option value="'.$key.'">'.$value['Device Model'].'</option>';
endforeach;
?>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-sm-5">Status</div>
<div class="col-sm-7">
<select class="form-control input-sm">
<option selected disabled>Please Select</option>
<option>Started</option>
<option>Pending</option>
<option>Complete</option>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-sm-5">Log Output Date</div>
<div class="col-sm-7">
<input type="text" name="device-name" class="form-control input-sm">
</div>
</div>
</div>
<div class="col-sm-2">
<div class="row" style="height: 98px;"></div>
<div class="row">
<button href="#" class="btn download-btn" id="downloadBtn">Log Download</button>
</div>
</div>
</div>
</div>
<?php ActiveForm::end(); ?>
<div class="container-fluid">
<table id="device-list_table" class="table table-striped table-bordered dataTable display">
<thead>
<tr>
<th class="text-center"><input type="checkbox" id="selectAll"></th>
<th class="text-center">Device Name</th>
<th class="text-center">Device Model</th>
<th class="text-center">Broadcasted Program</th>
<th class="text-center" colspan="2">Device Status</th>
<th class="text-center">Last Communication Date and Time</th>
<th class="text-center"></th>
</tr>
</thead>
<tbody>
<?php foreach($devices as $key => $value) { ?>
<tr>
<td class="text-center"><input type="checkbox" name="device-checkbox" value="<?= $value['Device Name'] ?>" id="<?php echo 'boxId'.$key ?>"></td>
<td class="text-center"><?= $value['Device Name']; ?></td>
<td class="text-center"><?= $value['Device Model']; ?></td>
<td class="text-center"><?= $value['Broadcasted Program']; ?></td>
<td class="text-center">
<?php
if ($value['Device Status'] == "Start") {
echo "<font color='yellow'>⚫</font>";
} elseif ($value['Device Status'] == "Pending") {
echo "<font color='red'>⚫</font>";
} elseif ($value['Device Status'] == "Complete") {
echo "<font color='green'>⚫</font>";
}
?>
</td>
<td class="text-center"><?= $value['Device Status']; ?></td>
<td class="text-center"><?= $value['Last Communication Date and Time']; ?></td>
<td class="text-center">
<ul class="list-inline">
<li>
<button href="#" class="btn">
Display Log
</button>
</li>
<li>
<button href="#" class="btn">
Edit
</button>
</li>
</ul>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
How do I search data in my table and display the results (inside the same table) without refreshing the page? As you can see, I have a "Log Download" button, maybe when I click that, the results will then show.
EDIT: Somewhat like the implementation of dataTables (https://www.datatables/) but in my case, I have multiple form fields to use in searching.
Share Improve this question edited Feb 19, 2016 at 2:33 Ethelene Laverne asked Feb 17, 2016 at 7:15 Ethelene LaverneEthelene Laverne 2892 gold badges7 silver badges21 bronze badges4 Answers
Reset to default 4You can do like
$("#deviceName").keyup(function(){
var filter = $(this).val();
$("#device-list_table tr td").each(function(){
if ($(this).text().search(new RegExp(filter, "i")) < 0) {
$(this).hide();
} else {
$(this).show();
}
});
});
This is only for deviceName
type you can add other properties also
here is a simple Jquery plugin for that
http://www.jqueryscript/table/Simple-jQuery-Plugin-For-Html-Table-Live-Search.html
Include the jQuery library and the jQuery Html Table Search plugin in the web page.
<script src="//ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="html-table-search.js"></script>
Call the plugin on an existing Html table.
$(document).ready(function(){
$('table.search-table').tableSearch();
});
I am not quite sure what you are asking, but if I understand you correctly, you have a <table>
element that contains searchable information, and you would like this table to update to contain only the search results after users use the search function?
In this case, you can do this through AJAX requests in the following manner. Create a .php file that generates the contents of the table based on the input from your search function (let us call it generateTable.php), sent to it through the $_GET variable. generateTable.php goes something like this:
$result = search($_GET['device-name'], $_GET['country'], $_GET['store']...);
echo '<tr><td>' . $result[0] . '</td><td>' . $result[1] . '</td><td>' . $result[2] . '</td><td>'...'</tr>'
Where search() is the function that does the search, returning an array containing the result.
Call a JS function when the search button is clicked that builds the URL to generateTable.php, with the $_GET values included (/generateTable.php?device-name=foo&country=usa&store=3...
). You can get the values from the fields using JQuery's .val() function, for instance var deviceName = $('#device-name').val();
. This would require you to add id='device-name'
to your input field named "device-name".
var deviceName = $('#device-name').val();
var country = $('#country').val();
var store = $('#store').val();
...
var generateTableUrl = "generateTable.php?device-name=" + deviceName + "&country=" + country + "&store=" + store...
With JQuery AJAX, you can load the output from generateTable.php with the following JS code:
$('#device-list_table').load(generateTableUrl);
The whole script bees
function getSearchResults() {
var deviceName = $('#device-name').val();
var country = $('#country').val();
var store = $('#store').val();
...
var generateTableUrl = "generateTable.php?device-name=" + deviceName + "&country=" + country + "&store=" + store...
$('#device-list_table').load(generateTableUrl);
}
And to start the whole process, add onClick='getSearchResults();'
to your search button, namely the button that would be the submit button in a traditional form. Note that this process does not use the form in a traditional sense, as the form does not call 'generateTable.php' directly, it is simply used as a place to get input for the JS function that loads 'generateTable.php'.
I must apologise for the code simplification I had to do; I am not currently able to test my code, so what is written here is meant more as a general outline of what to do than a working example.
Here is link for your Question:
https://www.datatables/
This is really helpful for data tables search record without refreshing page.