how to id of button in table using jquery? - php

<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Email</th>
<th>Name</th>
<th>Subject</th>
<th>Message</th>
</tr>
</thead>
<tbody>
<?php
foreach($fetchContactData as $value)
{
?>
<tr>
<td><?php echo $value['email'] ?></td>
<td><?php echo $value['name']?></td>
<td><?php echo $value['subject'] ?></td>
<td><button type="button" class="btn btn-warning" value="<?php echo $value['message'] ?>">Show</button></td>
<?php
}?>
</tbody>
<tfoot>
<tr>
<th>Email</th>
<th>Name</th>
<th>Subject</th>
<th>Message</th>
</tr>
</tfoot>
</table>
Js code:
<script type="text/javascript">
$(document).ready(function(){
$("#example1 button").click(function(){
var id=$(this).attr('value');
});
});
</script>
here at each row there is unique id to button. so onclick of that button i want that id in jquery. I tried many searches from stackoverflow but its not working so anyone can help me with jquery part with this exact same code
Here is my js now i want that long message so that i could show it in modal class div.
[Violation] Forced reflow while executing JavaScript took 73ms ---> This is the error i got

An "id" is always used to identify something and therefore should be unique. So you have to make it unique in the first place. Something like this should work.
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Email</th>
<th>Name</th>
<th>Subject</th>
<th>Message</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach($fetchContactData as $value)
{
?>
<tr>
<td><?php echo $value['email'] ?></td>
<td><?php echo $value['name']?></td>
<td><?php echo $value['subject'] ?></td>
<td><button type="button" class="btn btn-warning" id="show-<?= $i; ?>" value="<?php echo $value['id'] ?>">Show</button></td>
<?php
$i++;
}?>
</tbody>
<tfoot>
<tr>
<th>Email</th>
<th>Name</th>
<th>Subject</th>
<th>Message</th>
</tr>
</tfoot>
</table>
In this code, there will be the variable "$i" that is set to 0 in the beginning and just appended to the id "show-". Last step of the foreach-loop, this 0 is just counted up by one.
This will generate and id in the format of "show-0", "show-1" (you could set $i to 1 in the beginning to make it start counting from 1). In jQuery, you then could use the "starts with"-selector to select all buttons and set an onclick event on it. (just one example) https://api.jquery.com/attribute-starts-with-selector/
<script>
$( "button[id^='show-']" ).on('click', clickhandler);
</script>
So for every button that has an id starting with "show-" you can make it do the action "clickhandler".

your id=button is into loop, so is not unique.
I advise you do not use id, better you can use something like class and in your js script listen click to this, like:
<td><button type="button" class="btn btn-warning button_msg" value="<?php echo $value['message'] ?>">Show</button></td>
Js (jquery)
$(".button_msg').click(function(){
//Do anything
})

Related

table display having a column of checkboxes gets hidden when using datatable

Help needed!
I have done coding using php mvc technique, using wamp server and tried using datatables(only for search functionality).
I have a table display in my Admin index page which fetches data from mysql database. I also have checkbox included at each row so that it can be selected and deleted.
...
<form action="<?php echo URLROOT; ?>/posts" method="post">
<button type="submit" value="Delete" name="delete_multiple_data">Delete Multiple Items</button>
<table id="mtab" class="display table table-striped table-bordered dt-responsive nowrap" style="width:100%">
<thead class="thead-dark">
<tr class="danger">
<tr class="danger">
<th scope="col">ID</th>
<th scope="col">Pack Name</th>
<th scope="col">Select</th>
</tr>
</thead>
<tbody>
<?php foreach($data['posts'] as $post) : ?>
<tr>
<td><?php echo $post->id; ?></td>
<td><?php echo $post->pckname; ?></td>
<td><input type="checkbox" name="delete[]" value="<?php echo $post->id; ?>"></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
...
my controller,
...
if(isset($_POST['delete_multiple_data'])){
if(isset($_POST['delete'])){
foreach($_POST['delete'] as $deleteid){
$deletepack = $this->postModel->deletePost($deleteid);
}
}
}
$this->view('posts/index', $data);
...
without table id="mtab"(removing table id="mtab") everything is working fine.
but for search to be enabled in my table id="mtab" i used the below script,
...
<script type="text/javascript">
$(document).ready(function(){
$("#mtab").DataTable({
});
});
</script>
...
if i use this script, it hides my checkboxes totally from display, please help
I have identified the error it was <table class="display table table-striped table-bordered dt-responsive nowrap" style="width:100%">
after I removed nowrap it was working fine, both my checkbox as well as search feature was visible.
Thanks all.....

How to make this table with expandable rows to display other rows when expanded?

I have a datatable in HTML which i am populating with dynamic data from PHP. each row is expandable but what I want is to have child rows for each parent row. So, for example there is a device type with the number 4 which contains 20 different devices, so I want to display in the parent row Device number 4 and when I expand that row display the 20 devices (with headers such as IMEI, Serial no., etc.)
Here is the table I have right now:
See screenshot of my table
EDIT: Added current table (no child rows)
<table id="example" class="table table-bordered table-striped" cellspacing="0" width="100%">
<thead>
<th>Device</th>
<th>Dev.no</th>
<th>Barcode</th>
<th>IMEI</th>
<th>Serial no.</th>
<th>Date added on stock</th>
</thead>
<tbody>
<?php if (!empty($devices)) { ?>
<?php foreach ($devices as $device) : ?>
<tr>
<td>
<?php echo $device["name"] ?>
</td>
<td>
<?php echo $device["device_no"] ?>
</td>
<td>
<?php echo $device["barcode"] ?>
</td>
<td>
<?php echo $device["serial_imei"] ?>
</td>
<td>
<?php echo $device["serial_no"] ?>
</td>
<td>
<?php echo $device["created_date"] ?>
</td>
</tr>
<?php endforeach; ?>
<?php } ?>
</br>
</tbody>
</table>
I think you're looking for something like this https://datatables.net/reference/api/row().child().
Then you can add a table in the child row with more information.
Note: It looks like you have the responsive option enabled (green plus button that hides/shows hidden columns). If you have a child row open and try to show the hidden columns, it might overwrite your child row.
Edit:
You need to specify the HTML you want to put in the child. I would personally use ajax to retrieve the children when I wanted to display them. Otherwise the data needs to be hidden somewhere in the page.
I've created a codepen that puts the HTML in a data attribute on the row. Then you can click a button to view the child rows (devices). Obviously, there are many other ways to store the data on the page.
$(document).on("click", ".viewChildren", rowClickHandler);
$("#example").DataTable();
function rowClickHandler() {
var btn = $(this);
var tr = btn.closest('tr');
var dtRow = $("#example").DataTable().row(tr);
if (dtRow.child.isShown()) {
dtRow.child.hide();
btn.text('Show Children');
} else {
var children = tr.data("children");
dtRow.child(children).show();
btn.text('Hide Children');
}
}
th{
text-align: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.20/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" />
<table id="example" class="table table-bordered table-striped" cellspacing="0" width="100%">
<thead>
<tr>
<th>Device</th>
<th>Dev.no</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr data-children='<table>
<thead>
<tr>
<th>barcode</th>
<th>imei</th>
<th>serial_no</th>
<th>created_date</th>
</tr>
</thead>
<tbody>
<tr>
<td>123456</td>
<td>11324</td>
<td>654654</td>
<td>2020-01-17</td>
</tr>
<tr>
<td>1234987</td>
<td>1654</td>
<td>654687</td>
<td>2020-04-30</td>
</tr>
</tbody>
</table>'>
<td>Laptop</td>
<td>2</td>
<td><button class = 'viewChildren'>See Children</button></td>
</tr>
</tbody>
</table>

How to write a function that is based on oops and i can return the database values so i can stop rewriting of code?

Right now I am writing a program in core PHP, but I am not able to write a perfect oops concept as I m rewriting the code many times.
for Eg.
public function all()
{
$init = new Database;
$sql = "SELECT * FROM categories";
// Result
$result = $init->conn->query($sql);
if ($result->num_rows > 0) {
?>
<table class="table table-hover">
<thead>
<tr>
<th>Action</th>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td>
<form action="update_categories.php?update_id=<?php echo $row['id'] ?>" method="post">
<button type="submit" class="btn btn-primary" name="update">Update</button>
</form>
<form action="delete_categories.php?id=<?php echo $row['id'] ?>" method="post">
<button type="submit" name="delete" class="btn btn-danger">Delete</button>
</form>
</td>
<td><?php echo $row["id"] ?></td>
<td><?php echo $row["category_name"] ?></td>
<td><?php echo $row["category_description"] ?></td>
</tr>
<?php
} ?>
</tbody>
<?php
} else {
?>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">no result</th>
<td>no result</td>
<td>no result</td>
</tr>
</tbody>
<?php
}
}
So here you can see I have to fetch the data from the database but in this function, I have included Html too so if you want to add database data to another place I have to write the function for that again and fetch the values.
Is there any way I can only fetch the value and i can use the function to print it wherever I want.

Fetch and dispaly through a modal database contents of selected in a datatable row when button is click

I have a datatable which retrieves data from db, i want to be to implement a modal pop up under the row improvement actions so instead displaying the actual improvement actions i want like a symbol under that row so when i click it a modal appears with the improvement actions while all the other data remains the same , i want to implement this using ajax but am quite a novice in ajax please assist.
<table class="table table-hover table-striped table-bordered datatable-basic" >
<thead>
<tr>
<tr class="bg-violet-400" >
<th>ID</th>
<th>Site</th>
<th>Date</th>
<th>SN</th>
<th>Gap identified</th>
<th>Improvement Actions</th>
<th>Timeline</th>
<th>Person responsible</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
$connect = mysqli_connect("localhost", "root", "", "dqa");
$query=mysqli_query($connect,"SELECT * FROM action_plan");
while($row=mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['site'] ?></td>
<td><?php echo $row['date'] ?></td>
<td><?php echo $row['sn'] ?></td>
<td><?php echo $row['gap_identified'] ?></td>
<td><?php echo $row['Improvement_Actions'] ?></td>
<td><?php echo $row['timeline'] ?></td>
<td><?php echo $row['person_responsible'] ?></td>
<td><?php if( $row['status'] == 1 )
{
echo ' <button type="button" class="btn bg-grey">Approved</button></span>';
}
elseif( $row['status'] == 0 ) {
echo ' <button type="button" class="btn btn-danger">Active Action</button></span>';
}
elseif( $row['status'] == 2 ) {
echo ' <button type="button" class="btn bg-brown"> Resubmission </button></span>';
}
elseif( $row['status'] == 3 ) {
echo ' <button type="button" class="btn bg-orange">Action Due</button></span>';
}
?></td></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
Without any sample data or example data, I can only speculate at what you are trying to accomplish. Here is an example that I think you may find helpful.
$(function() {
$(".table").DataTable();
$(".table tbody .actions").button({
showLabel: false,
icon: "ui-icon-extlink"
}).click(function(e) {
var self = $(this);
var row = $(this).attr("href").substring(1);
var siteurl = "<?php echo siteurl('improvement');?>";
var form = $("<form>", {
id: "form-" + row
});
form.append($("<input>", {
type: "hidden",
name: "rowid",
value: row
}));
var dialog = $("<div>", {
title: "Improvement Actions - Row " + row
}).append(form).dialog({
modal: true,
create: function(e, ui) {
$.getJSON(siteurl + "/" + row, function(data) {
$('[name="rowid"]', this).val(data.id);
});
},
close: function(e, ui) {
self.data("results", form.serialize());
dialog.dialog("destroy").remove();
}
});
});
});
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.js"></script>
<table class="table table-hover table-striped table-bordered datatable-basic">
<thead>
<tr>
<tr class="bg-violet-400">
<th>ID</th>
<th>Site</th>
<th>Date</th>
<th>SN</th>
<th>Gap identified</th>
<th>Improvement Actions</th>
<th>Timeline</th>
<th>Person responsible</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>
1
</td>
<td>
LOC-1
</td>
<td>
04/24/2018
</td>
<td>
1001
</td>
<td>
0.1
</td>
<td>
Improvement Actions
</td>
<td>
</td>
<td>
John Smith
</td>
<td>
<button type="button" class="btn bg-grey">Approved</button>
</td>
</tr>
</tbody>
</table>
Assuming you are using DataTable, The table is populated with various data from PHP, resulting in HTML like shown above. You can then use jQuery UI to program the button to build a dialog. One of the callback events for .dialog() is create, see more: http://api.jqueryui.com/dialog/#event-create
You can perform your AJAX call at this time to get specific data for this dialog. In my example, all of it is being created dynamically, so there is no need to reset forms etc.
I advise using $.getJSON(), but there is nothing wrong with the $.ajax() code you implemented. The $.getJSON() simply assumes you are performing a GET call to a resource that will return JSON data. Resulting in simplified code.
Testing of this example is not complete as the PHP Script URL is fake or will be null.
Hope that helps.

How to select id from the datatables using checkbox

Heres my code: HTML with Server side to get data from the database. How do I get id from the datatables using checkbox option?
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<h4>
<th></th> <!-- For checkbox column-->
<th>ID Number</th>
<th>Lastname</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Gender</th>
<th>Course</th>
<th>Department</th>
<th>Address</th>
<th>Contact</th>
<th>Birthdate</th>
<th>View</th>
<th>Events</th>
</h4>
</tr>
</thead>
<tbody>
<?php
include 'pgconnect.php';
$mydata = mysqli_query($conn,"SELECT * FROM student") or die(mysql_error());
while($record = mysqli_fetch_assoc($mydata))
{
$id=$record['id'];
?>
<tr>
<td><input type="checkbox"></td>
<td class="id"><?php echo $record['id'] ?></td>
<td class="lname"><?php echo $record['last_name'] ?></td>
<td class="fname"><?php echo $record['first_name'] ?></td>
<td class="mname"><?php echo $record['mid_name'] ?></td>
<td class="sex"><?php echo $record['gender'] ?></td>
<td class="course"><?php echo $record['course'] ?></td>
<td class="dept"><?php echo $record['department'] ?></td>
<td class="add"><?php echo $record['home_address'] ?></td>
<td class="contact"><?php echo $record['contact'] ?></td>
<td class="bdate"><?php echo $record['birthdate'] ?></td>
<td> View Records </td>
<td> <a title='Click here to update record.'href='#edit' data-toggle='modal'><i class="fa fa-pencil fa-fw"></i></a>
<a title='Click here to add record.' href='#'><i class="fa fa-plus fa-fw"></i></a>
<a title='Click here to delete record.' href='delete.php?del=<?php echo $record['id'] ?>'><i class="fa fa-trash-o fa-fw"></i></a></td>
</tr>
<?php
}
?>
</tbody>
</table>
Here's my script:
$(document).ready(function(){
$('#dataTables-example').dataTable();
});
I tried to get the ID but still it didn't works. Hope you can help me.
In your work, you get the ID in table. your should grab it from the checkbox if you use jquery, as in;
<script>
$(document).ready(function(){
document.getElementByID("ckmyid").innerHTML;
});
</script>
Note:add Selector ID in your checkbox, first, say, "ckmyid" so the code will be like this:
<input id="ckmyid" type="checkbox" value="<?php echo $record['id'] ?>">
Based on your structure, you can grab it directly from the link in your view records link if you want to place the ID there,as in;
<td> View Records </td>

Categories