Disable the button in the table based on the td text - php

I have a table as follows:
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Order ID</th>
<th>Name</th>
<th>Address</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
foreach ($rslt as $value) {
?>
<tr>
<td><?= $value['order_id']?></td>
<td><?= $value['cust_name']?></td>
<td><?= $value['address']?></td>
<td><?= $value['st_size']?></td>
<td class="status"><?= $value['status']?></td>
<td><button class="btn btn-primary btn-sm btn-assign" type="button" data-id="<?=$value['order_id']?>">Assign</button></td>
</tr>
<?php
}
?>
</tbody>
</table>
What I am trying to do is, if the $value['status'] is "assigned" then I want to disable the button i.e next to this td. I have tried following code. But i could not disable the button.
$("#dataTable tbody").find("tr").each(function() {
var ratingTdText = $(this).find('td.status').text();
if (ratingTdText == 'assigned')
$(this).parent('tr').find(".btn-assign").prop("disabled",true);
});

Your button doesn't have the id btn-assign, it's a class. Use . instead of #.
Plus, in your case, $(this).parent('tr') won't find anything, since this is already the tr.
$('#dataTable tbody tr').each(function() {
if ($('.status', this).text() == 'assigned') {
$('.btn-assign', this).prop("disabled", true);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Order ID</th>
<th>Name</th>
<th>Address</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td class="status">assigned</td>
<td><button class="btn btn-primary btn-sm btn-assign" type="button" data-id="d">Assign</button></td>
</tr>
</tbody>
</table>

To add to what Zenoo said - there's no need to call find when you have a specific selector for status. Always strive to make the simplest form of the argument.
if ($('.status').text() == 'assigned') {
$('.btn-assign').prop("disabled", true);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Order ID</th>
<th>Name</th>
<th>Address</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td class="status">assigned</td>
<td><button class="btn btn-primary btn-sm btn-assign" type="button" data-id="d">Assign</button></td>
</tr>
</tbody>
</table>

Here you got an easy way to do it with has and contains selectors:
$('tr:has(td.status:contains(assigned)) .btn-assign').prop("disabled", true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Order ID</th>
<th>Name</th>
<th>Address</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>a1</td>
<td>b2</td>
<td>c3</td>
<td>d4</td>
<td class="status">assigned</td>
<td><button class="btn btn-primary btn-sm btn-assign" type="button" data-id="d">Assign</button></td>
</tr>
<tr>
<td>a2</td>
<td>b2</td>
<td>c2</td>
<td>d2</td>
<td class="status">Not assign</td>
<td><button class="btn btn-primary btn-sm btn-assign" type="button" data-id="d">Assign</button></td>
</tr>
<tr>
<td>a3</td>
<td>b3</td>
<td>c3</td>
<td>d3</td>
<td class="status">assigned</td>
<td><button class="btn btn-primary btn-sm btn-assign" type="button" data-id="d">Assign</button></td>
</tr>
<tr>
<td>a4</td>
<td>b4</td>
<td>c4</td>
<td>d4</td>
<td class="status">Not assign</td>
<td><button class="btn btn-primary btn-sm btn-assign" type="button" data-id="d">Assign</button></td>
</tr>
</tbody>
</table>
Note this will select any td with containing word "assigned" even if there are other words.

Why use Javascript, you can fix it on the PHP end.
<td><button <?= ($value['status']=='assigned')?'disabled':''; ?> class="btn btn-primary btn-sm btn-assign" type="button" data-id="<?=$value['order_id']?>">Assign</button></td>

Related

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.

Bootstrap datatable not working on adding accordion

I have a datatable as follows:
foreach($tickets as $tickets)
{
echo ('<tr>');
echo ('<td>'.$tickets->error.'</td>');
echo ('<td>'.$tickets->hours.'</td>');
echo ('<td>'.$tickets->time.'</td>');
echo ('<td>'.$tickets->date.'</td>');
echo ('</tr>');
}
I added accordion effect to it likewise:
foreach($tickets as $tickets)
{
echo ('<tr data-toggle="collapse" data-target=".demo1">');
echo ('<td>'.$tickets->error.'</td>');
echo ('<td>'.$tickets->hours.'</td>');
echo ('<td>'.$tickets->time.'</td>');
echo ('<td>'.$tickets->date.'</td>');
echo ('</tr>');
echo ('<tr>');
echo ('<td class="hiddenRow">');
echo ('<div class="collapse demo1">Demo1</div>');
echo ('</td>');
echo ('</tr>');
}
and the table has lost it's datatable properties such as search, view as 10/25/50 items per page etc.
Jquery:
$('.collapse').on('show.bs.collapse', function () {
$('.collapse.in').collapse('hide');
});
$(".clickable-row").click(function() {
window.location = $(this).data("href");
});
I'd like help regarding this issue.
Try using colspan="4" on the second td
Here is fiddle: http://jsfiddle.net/3ghbLrpu/1/
<html>
<head>
<script src="js/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<table id="dataTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Error</th>
<th>Time Spent</th>
<th>Time</th>
<th>Date</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Error</th>
<th>Time Spent</th>
<th>Time</th>
<th>Date</th>
</tr>
</tfoot>
<tbody>
<tr data-toggle="collapse" data-target=".demo1" class="accordion-toggle">
<td>asd</td>
<td>asd</td>
<td>asd</td>
<td>asd</td>
</tr>
<tr>
<td class="hiddenRow" colspan="4">
<div class="collapse demo1">Demo1</div>
</td>
</tr>
</tbody>
</table>
</body>
CSS
.hiddenRow {
padding: 0 !important;
}
Another option is use bootstrap and remove knockoutjs
https://codepen.io/creativedev/pen/XYMRyQ
$(document).ready(function() {
$("#collapseme").click(function() {
if($("#test").hasClass("out")) {
$("#test").addClass("in");
$("#test").removeClass("out");
} else {
$("#test").addClass("out");
$("#test").removeClass("in");
}
});
});
HTML
<table id="dataTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Error</th>
<th>Time Spent</th>
<th>Time</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr class="" id="collapseme">
<td>asd</td>
<td>asd</td>
<td>asd</td>
<td>asd</td>
</tr>
<tr>
<td class="hiddenRow" colspan="4">
<div class="collapse out" id="test">Demo1</div>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Error</th>
<th>Time Spent</th>
<th>Time</th>
<th>Date</th>
</tr>
</tfoot>
</table>

Passing data to controller on row bases

I have a table display and each row contains a button. With this button l have to pass the id to the controller where using this Admin can view user details one by one.
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<button type="submit" class="btn btn-
default" name="viewcv"> View</button> <br>
<br>
</td>
</tr>
#endforeach
</tbody>
</table>
Please check the code , hope it helps:
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>CV</td>
</tr>
#endforeach
</tbody>
</table>
Assuming you want to pass the field "id" or change it according to your requirement.
If you want to link to a show view, try something like this:
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<a href="{{action('JobController#show', $row->id)}}" class="btn btn-default">View</button>
</td>
</tr>
#endforeach
This will create a link to a view, where you can show the data.
In your controller JobController, you must create a function that receives the data:
public function show(Request $request, $id) {
...
}
Just pass the id of the record to controller function and get the details from database.
<td>
View <br>
</td>

How to make paginated table?

I'm using bootstrap and I have used a table to echo users data by using PHP, the problem is that there's a lot of results, so I want to show pagination below that table, and I want to do this with jQuery if possible? If not I guess I will go for PHP.
How could I do it?
I have tried several plugins and different methods, but actually I'm a beginner and it didn't work smoothly for me.
One of the jQuery plugins I tried is simplePagination.js but nothing is working.
My code:
<table class="table table-striped">
<thead>
<tr>
<th width="30%">User Name</th>
<th width="35%">Email</th>
<th width="30%">Phone</th>
<th width="5%">Option</th>
</tr>
</thead>
<tbody>
<tr id="users">
<th><?php echo $row['mem_uname']; ?></th>
<td><?php echo $row['mem_email']; ?></td>
<td><?php echo $row['mem_phone']; ?></td>
<td>
<a class="btn btn-danger option"
href="home?p=Users&delete=<?php echo $row['mem_id'];?>">Delete</a>
</td>
</tr>
</tbody>
</table>
You could use dataTables jQuery plugin which:
Is bootstrap friendly
Includes pagination, you would only need to tweak it's length via jQuery.
See snippet below:
$('#myTable').dataTable({
"pageLength": 4
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script>
<table id="myTable" class="table table-striped">
<thead>
<tr>
<th width="30%">User Name</th>
<th width="35%">Email</th>
<th width="30%">Phone</th>
<th width="5%">Option</th>
</tr>
</thead>
<tbody>
<tr id="users">
<th>TEST</th>
<td>TEST</td>
<td>TEST</td>
<td>
<a class="btn btn-danger option" href="home?p=Users&delete=<?php echo $row['mem_id'];?>">Delete</a>
</td>
</tr>
<tr id="users">
<th>TEST</th>
<td>TEST</td>
<td>TEST</td>
<td>
<a class="btn btn-danger option" href="home?p=Users&delete=<?php echo $row['mem_id'];?>">Delete</a>
</td>
</tr>
<tr id="users">
<th>TEST</th>
<td>TEST</td>
<td>TEST</td>
<td>
<a class="btn btn-danger option" href="home?p=Users&delete=<?php echo $row['mem_id'];?>">Delete</a>
</td>
</tr>
<tr id="users">
<th>TEST</th>
<td>TEST</td>
<td>TEST</td>
<td>
<a class="btn btn-danger option" href="home?p=Users&delete=<?php echo $row['mem_id'];?>">Delete</a>
</td>
</tr>
<tr id="users">
<th>TEST</th>
<td>TEST</td>
<td>TEST</td>
<td>
<a class="btn btn-danger option" href="home?p=Users&delete=<?php echo $row['mem_id'];?>">Delete</a>
</td>
</tr>
<tr id="users">
<th>TEST</th>
<td>TEST</td>
<td>TEST</td>
<td>
<a class="btn btn-danger option" href="home?p=Users&delete=<?php echo $row['mem_id'];?>">Delete</a>
</td>
</tr>
<tr id="users">
<th>TEST</th>
<td>TEST</td>
<td>TEST</td>
<td>
<a class="btn btn-danger option" href="home?p=Users&delete=<?php echo $row['mem_id'];?>">Delete</a>
</td>
</tr>
<tr id="users">
<th>TEST</th>
<td>TEST</td>
<td>TEST</td>
<td>
<a class="btn btn-danger option" href="home?p=Users&delete=<?php echo $row['mem_id'];?>">Delete</a>
</td>
</tr>
</tbody>
</table>

How to pass the php parameter in jquery

From Here I am doing like I fetch data from database and display in table format still it is working fine, after I click the division_edit() function I want pass the value (id),how can do this.
<table id="dataTable" class="table table-bordered table-hover">
<thead>
<tr>
<th>S.No</th>
<th>Division</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $sql=m ysql_query( "SELECT * FROM division WHERE status !='1'"); for($i=1;$row=m ysql_fetch_assoc($sql);$i++){ ?>
<tr role="row" class="odd">
<td>
<?php echo $i;?>
</td>
<td>
<?php echo $row[ 'division'];?>
</td>
<td><a class="btn btn-success btn-xs" href="state_edit.php?id=<?php echo base64_encode($row['id']);?>" onclick="division_edit()" id="division_edit"><i class="fa fa-pencil-square-o"></i> Edit</a>
<button class="btn btn-danger btn-xs" data-href="state_delete.php?id=<?php echo base64_encode($row['id']);?>"
data-toggle="modal" data-target="#confirm-delete"><i class="fa fa-trash"></i> Delete</button>
</td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th>S.No</th>
<th>Division</th>
<th>Action</th>
</tr>
</tfoot>
</table>
May this help you:
<table id="dataTable" class="table table-bordered table-hover">
<thead>
<tr>
<th>S.No</th>
<th>Division</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $sql=m ysql_query( "SELECT * FROM division WHERE status !='1'"); for($i=1;$row=m ysql_fetch_assoc($sql);$i++){ ?>
<tr role="row" class="odd">
<td>
<?php echo $i;?>
</td>
<td>
<?php echo $row[ 'division'];?>
</td>
<td><a class="btn btn-success btn-xs" href="state_edit.php?id=<?php echo base64_encode($row['id']);?>" onclick="division_edit(<?php echo $row['id']; ?>)" id="division_edit"><i class="fa fa-pencil-square-o"></i> Edit</a>
<button class="btn btn-danger btn-xs" data-href="state_delete.php?id=<?php echo base64_encode($row['id']);?>"
data-toggle="modal" data-target="#confirm-delete"><i class="fa fa-trash"></i> Delete</button>
</td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th>S.No</th>
<th>Division</th>
<th>Action</th>
</tr>
</tfoot>
</table>
Try this..
<a class="btn btn-success btn-xs" href="state_edit.php?id=<?php echo base64_encode($row['id']);?>" onclick="division_edit(<?php echo $row['id'] ?>)" id="division_edit"><i class="fa fa-pencil-square-o"></i> Edit</a>

Categories