Search Html Table and Display Results Without Refresh - javascript

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.net/) but in my case, I have multiple form fields to use in searching.

You 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.net/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.com/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 becomes
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.net/
This is really helpful for data tables search record without refreshing page.

Related

Javascript inside PHP foreach

I made a loop using foreach, to show a list of products. Then I made a modal to edit each product. I need to get the value of product dimension and I'll make a dependent drop down between 'job drop down' and 'supply drop down'. At the moment, I can't continue it with Ajax because I got a problem at the first step to get value of dimension. In this code below I tried to get 'length' value of each product through javascript. But it returns the same value, it always is the value of the first product, even if I click on the 2nd, 3rd, 4th of product in the list.
<div class="table-responsive">
<table class="table table-hover" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Product</th>
<th>Supplier</th>
<th>Dimension</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
$pcs_1 = 0;
$meter_1 = 0;
foreach ($stock as $s) { ?>
<tr>
<td class="small">
<?php echo $s->cat . ' - ' . $s->prod ?>
</td>
<td class="small">
<?php echo $s->spl ?>
</td>
<td class="small">
<?php echo $s->length . ' x ' . $s->width . ' x ' . $s->height . ' x ' . $s->diameter ?>
</td>
<td>
<a type="button" data-toggle="modal" class="btn btn-small" data-target="#modal_edit<?php echo $s->id ?>"><i class="fas fa-edit"></i></a>
<!--MODAL-->
<div class="modal fade" id="modal_edit<?php echo $s->id ?>" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal" method="post" action="<?php echo site_url('admin/Stock/edit/') ?>">
<div class="modal-header">
<h5 class="modal-title" id="labelModal">EDIT STOCK</h5>
</div>
<div class="modal-body">
<input type="hidden" id="length_product" class="length_product" value="<?php echo $s->length ?>" />
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<?php echo form_label('JOB') ?>
</div>
</div>
<div class="col-sm-9">
<div class="form-group">
<select name="ref" id="ref" class="form-control" required onchange="GetData()">
<option value="">-- Select Job --</option>
<?php foreach ($job as $j) : ?>
<option value="<?php echo $j->id; ?>"><?php echo $j->id ?> - <?php echo $j->cust ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<?php echo form_label('Supply') ?>
</div>
</div>
<div class="col-sm-9">
<select name="supply" id="supply" class="form-control" required>
<option value="">--Select Supply--</option>
</select>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-warning submitBtn">edit</button>
</div>
</form>
</div>
</div>
</div> <!-- modal -->
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
Here is the javascript :
<script type="text/javascript">
function GetData() {
var length_product = $("#length_product").val();
alert(length_product);
}
</script>
The list works well, but I don't know how to get the detail of each list on javascript (passing multiple values from 'modal inside the list' to 'Javascript').
for me it looks like that you are always getting value of
<input type="hidden" id="length_product" class="length_product" value="<?php echo $s->length ?>" />
rather than option from the select element.
Try the following:
$('#ref').on('change', function(event) {
event.preventDefault();
var length_product = $(this).val();
alert(length_product);
});
and for the PHP and HTML:
<div class="form-group">
<select name="ref" id="ref" class="form-control" required>
<option value="">-- Select Job --</option>
<?php foreach ($job as $j) { ?>
<option value="<?php echo $j["id"]; ?>"><?php echo $j["id"]; ?>-<?php echo $j["cust"]; ?></option>
<?php
}
?>
</select>
</div>
Hope this helps!

Duplicate dynamic select boxes using clone()

I have a div in my code which I need to duplicate as the user clicks on the Add button. In the div there are 2 dropdowns one which is populated dynamically from database and the other one dynamically populated based on the first dropdown selection.
I have achieved duplicating the div using jQuery clone(). But the dynamically populating feature isn't working for the first dropdown divs so as the second one. Also, sometimes when I select an option in parent div, that's also showing weird selections.
var count = 1;
$('#add-new').click(function() {
//clone
var row = $('#scope-brand-div').clone(true);
row.appendTo('#clone-parent');
//add new ids
var scopeselect = $('select.scope-select');
var brandselect = $('select.brand-select');
$(row).find(scopeselect).attr('id', 'project-scope' + count);
$(row).find(brandselect).attr('id', 'brands_used' + count);
count++;
});
<form action="add_project" method="POST">
<h4>Project Scope <button class="clone" name="addnew" id="add-new">Add</button></h4>
<div class="row">
<div class="col-md-6">
<div class="row" id="clone-parent">
<div id="scope-brand-div">
<div class="col-md-6">
<div class="form-group">
<label for="scope"><?php echo $this->lang->line('xin_project_scope');?></label>
<select name="scope" class="form-control select-border-color border-warning scope-select" data-plugin="select_hrm" data-placeholder="<?php echo $this->lang->line('xin_project_scope');?>" id="project-scope">
<option value=""></option>
<?php foreach($all_project_scope as $scope) { ?>
<option value="<?php echo $scope->scope_id; ?>">
<?php echo $scope->scope_name; ?>
</option>
<?php } ?>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group" id="brands-ajax">
<label for="brands_used"><?php echo $this->lang->line('xin_brands_used');?></label>
<select name="brands_used" id="brands_used" class="form-control select-border-color border-warning brand-select" data-plugin="select_hrm" data-placeholder="<?php echo $this->lang->line('xin_brands_used');?>">
<option value=""></option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<input type="submit name=" submit " id="submit-form " value="Submit ">
</form>

Get data-price attribute from dynamically added row and put inside the input box

how to get data-price inside the text box using select in php html and sql?
i tried this so far. what im trying to do is, when i change the selection box, the input box beside should change base on the value of data-price.
been trying this for a week.
e.g
select debit
local treasury 100
<script>
$(document).ready(function(){
var i=1;
var chart_add = '<tr id="row_chart'+i+'"><td><select name="chart_of_account[]" class="selectpicker" data-live-search="true"><option style="width: 400px;">--Select Chart of Account--</option><?php $chart=mysqli_query($conn,"Select acoount_no,account_title from chart_of_account"); while($row=mysqli_fetch_assoc($chart)){$account_no = $row["acoount_no"]; $account_title = $row["account_title"];?><option value="<?php echo $account_no; ?>" style="width: 400px;"><?php echo $account_no ." | ". $account_title; ?></option><?php }?></select></td><td><input type="text" name="chart_debit[]" class="form-control chart_debit" id="chart_debit"onchange="chart_change_debit()"></td><td><input type="text" name="chart_credit[]" class="form-control chart_credit" id="chart_credit" onchange="chart_change_credit()"></td><td><button type="button" name="remove_chart" id="'+i+'" class="btn btn-danger remove_chart">X</button></td></tr>';
$('#add').click(function(){
i++;
$('#dynamic_field').append(chart_add);
$('.selectpicker').selectpicker('render');
$('.chart_debit').val($('select[name="chart_of_account"] option:selected').data('price'));
});
$(document).on('click', '.remove_chart', function(){
var button_id = $(this).attr("id");
$('#row_chart'+button_id+'').remove();
});
});
</script>
<form method = "POST" name="add_name" id="add_name">
<div class="row">
<div class="col-md-12">
<!-- chart of account -->
<div class="row">
<div class="col-md-12">
<div class="box box-default">
<div class="box-header with-border">
<h3 class="box-title">Chart of Account</h3>
</div>
<div class="box-body">
<table class="table table-bordered" id="dynamic_field">
<tr>
<th rowspan="2">Chart of Account</th>
<th>Debit</th>
<th>Credit</th>
<th rowspan="2"><button type="button" name="add" id="add" class="btn btn-success">Add More</button></th>
</tr>
<tr>
<th>
<input type="text" name="chart_debit_sum" class="form-control chart_debit_sum" id="chart_debit_sum" readonly>
</th>
<th>
<input type="text" name="chart_credit_sum" class="form-control chart_credit_sum" id="chart_credit_sum" readonly>
</th>
</tr>
<tr>
<td>
<select name="chart_of_account[]" class="selectpicker" data-live-search="true">
<option style="width: 400px;">--Select Chart of Account--</option>
<?php
$chart = mysqli_query($conn,"Select acoount_no,account_title from chart_of_account");
while($row = mysqli_fetch_assoc($chart)){
$account_no = $row['acoount_no'];
$account_title = $row['account_title'];
$amount = $row['test_amount'];
?>
<option value = "<?php echo $account_no; ?>" style="width: 400px;" data-price = "<?php echo $amount; ?>">
<?php echo $account_no . " | " .$account_title; ?>
</option>
<?php } ?>
</select>
</td>
<td>
<input type="text" name="chart_debit[]" class="form-control chart_debit" id="chart_debit" onchange="chart_change_debit()">
</td>
<td>
<input type="text" name="chart_credit[]" class="form-control chart_credit" id="chart_credit" onchange="chart_change_credit()">
</td>
<td></td>
</tr>
</table>
</div>
</div>
</div>
</div>
<!-- chart of account -->
</div>
<!-- chart and sub-->
</div>
</form>
sample screenshot

Set Value for HTML Dropdown based on Another HTML Dropdown Selection

I have an HTML dropdown list, that when a value is selected, it displays a table that corresponds to the selection. I also have an Add Button. Inside the Add Button are 2 fields of information that need filled out, one being a dropdown box. Here is what I need but cannot figure out. I want the selected value on the dropdown in my Add Button to be the number that was already selected in the first dropdown. How can I do this?
Here is the code that I have so far:
<button class="ui-button ui-corner-all ui-widget" id="insertButton">Add Supplier ID</button><br><br>
<div id="dialog-form" title="Add Supplier ID">
<p class="validateTips">All form fields are required.</p>
<!-- Dialog box displayed after add row button is clicked -->
<form>
<fieldset>
<label for="mr_name">MR_ID</label>
<select name="mr_id" id="mr_id_dialog" class="text ui-widget-content ui-corner-all" value="300">
<?php foreach($user->fetchAll() as $user1) { ?>
<option>
<?php echo $user1['MR_ID'];?>
</option>
<?php } ?>
</select><br><br>
<label for="buyer_id">Supplier ID</label>
<input type="text" name="supp_id" id="supplier_id" class="text ui-widget-content ui-corner-all" value="99">
<!-- Allow form submission with keyboard without duplicating the dialog button -->
<input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
</fieldset>
</form>
</div>
<!-- Form -->
<form name="myForm" action="">
<!-- Dropdown List -->
<select name="master_dropdown" id="mr_id" onChange="updatetable(this.form);">
<option selected value="select">Choose a MR_ID</option>
<?php foreach($users->fetchAll() as $user) { ?>
<option data-name="<?php echo $user['MR_ID'];?>">
<?php echo $user['MR_ID'];?>
</option>
<?php } ?>
</select>
</form>
<!-- Table -->
<p>
<div id="table_div">
<table border="1" id="index_table" class="ui-widget ui-widget-content" style="display: none">
<thead>
<tr class="ui-widget-header">
<td>MR ID</td>
<td>Supplier ID</td>
</tr>
</thead>
<?php foreach($users_one->fetchAll() as $supp) { ?>
<tr>
<td class="mr_id"><div id="dropdown_selection"></div></td>
<td class="supp_id"><?php echo $supp['Supp_ID']?></td>
</tr>
<?php } ?>
</table>
</div>
See Events for Select for info on events with Select tag
This can be done with javascript.
Add a change listener to the first dropdown and remove the onChange from the HTML. You can call the updatetable function from within the javascript.
javascript
function bindListeners() {
var elementToBind = document.querySelector('#mr_id');
// Check if the element has loaded
if(elementToBind === undefined) {
// if not, wait 500ms and retry
setTimeout(bindListeners, 500);
}
else
{
// Add the event listeners
elementToBind.addEventListener('change', updateSelection, false);
elementToBind.addEventListener('click', updateSelection, false);
}
}
bindListeners();
function updateSelection(event) {
// Get the value from this select
var selectedValue = event.target.value;
// find the select object that you want to set the value for
var selectObjectToSet = document.querySelector('#mr_id_dialog');
selectObjectToSet.value = selectedValue;
// Call the function that was in the onChange listener
updatetable(this.form);
}
HTML
Add Supplier ID
<div id="dialog-form" title="Add Supplier ID">
<p class="validateTips">All form fields are required.</p>
<!-- Dialog box displayed after add row button is clicked -->
<form>
<fieldset>
<label for="mr_name">MR_ID</label>
<select name="mr_id" id="mr_id_dialog" class="text ui-widget-content ui-corner-all" value="300">
<?php foreach($user->fetchAll() as $user1) { ?>
<option>
<?php echo $user1['MR_ID'];?>
</option>
<?php } ?>
</select><br><br>
<label for="buyer_id">Supplier ID</label>
<input type="text" name="supp_id" id="supplier_id" class="text ui-widget-content ui-corner-all" value="99">
<!-- Allow form submission with keyboard without duplicating the dialog button -->
<input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
</fieldset>
</form>
</div>
<!-- Form -->
<form name="myForm" action="">
<!-- Dropdown List -->
<select name="master_dropdown" id="mr_id" onChange="updatetable(this.form);">
<option selected value="select">Choose a MR_ID</option>
<?php foreach($users->fetchAll() as $user) { ?>
<option data-name="<?php echo $user['MR_ID'];?>">
<?php echo $user['MR_ID'];?>
</option>
<?php } ?>
</select>
</form>
<!-- Table -->
<p>
<div id="table_div">
<table border="1" id="index_table" class="ui-widget ui-widget-content" style="display: none">
<thead>
<tr class="ui-widget-header">
<td>MR ID</td>
<td>Supplier ID</td>
</tr>
</thead>
<?php foreach($users_one->fetchAll() as $supp) { ?>
<tr>
<td class="mr_id"><div id="dropdown_selection"></div></td>
<td class="supp_id"><?php echo $supp['Supp_ID']?></td>
</tr>
<?php } ?>
</table>
</div>
</p>

Form POST not showing in controller

I am working with CodeIgniter, and I am trying to create a dynamically generated table that includes a checkbox, with values set to the id numbers of the row, in the first column of the row. When a single or multiple checkbox is checked, and the button clicked, I am trying to bring up a second form which consists of a couple of select fields and a textarea.
Ultimately, when I submit the form, I want the values of the checkboxes to be sent to my controller. However, currently I am only getting a false value when I check the values of the POST field in my controller.
Here is my current view code:
<form class="form" role="form" name="ticket_view" id="ticket_view" action="<?php echo site_url('example_controller/example_method'); ?>">
<div class='table-responsive text-left'>
<table class='table table-hover table-bordered table-condensed supportTable'>
<thead>
<th>Select</th>
<th>Id</th>
<th class="text-center">Status</th>
<th>Source</th>
</thead>
<tbody>
<?php foreach($examples as $example): ?>
<?php if ($example->type == "test): ?>
<tr class="<?php echo alternator('oddRow', '');?>">
<td class="text-center" style="width:60px;"><input type="checkbox" value="<?=$example->id;?>" name="supportTicketId[]" class="supportLogCheck"></td>
<td><a href=<?php echo site_url("example_controller/example_method/" . $example->idd); ?>>SUP-<?php echo $example->id; ?></a></td>
<td class="text-center">
<?php if ( ! $example->claimed): ?>
<span class="label label-danger">Unclaimed</span>
<?php else: ?>
<span class="label label-success">Claimed</span>
<?php endif; ?>
</td>
<td><?php if ($example->voicemail): ?>Voicemail <?php else: ?>Email <?php endif; ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="text-center">
<a class="btn btn-primary" id="firstButton" href="javascript:void(0);">First Button</a>
</div>
<div id="secondForm" class="alert alert-info col-md-4 col-md-offset-4" style="display:none; margin-top:10px;">
<div class="form-group">
<label for="closeIssue">Issue:</label>
<select name="issue" id="issue" class="form-control">
<option value="">Select an Issue</option>
<?php foreach ($issues as $issue): ?>
<option value="<?php echo ($e->emailIssueId); ?>"><?php echo ($issue->emailIssueDescription); ?></option>
<?php endforeach; ?>
</select>
<label for="closeAction">Action:</label>
<select name="action" id="action" class="form-control">
<option value="">Select an Action</option>
<?php foreach ($actions as $action): ?>
<option value="<?php echo ($action->emailActionId); ?>"><?php echo ($action->emailActionDescription); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="assignNote">Note:</label>
<textarea name="assignNote" id="assignNote" class="form-control"></textarea>
</div>
<div class="text-center">
<input class="btn btn-primary" name="secondButton" type="submit" id="secondButton" value="second button">
</div>
</div>
</div>
</form>
So, in a nutshell, the table is wrapped in a form. The first button slides down a
div that is orginally hidden, that contains other form fields and a submit button. I want, when that submit button is clicked, for all form fields to post, including the id numbers of the checkboxes that are checked.
Any ideas why this isn't working? Any better suggestions?
Thank you for your help in advance!
You haven't defined method on your form, so it's probably passing the values via GET. You need to add the method="post" attribute in your form tag.

Categories