I want to show the delete button below the table if any of the row checkboxes are checked. Otherwise, the button should stay hidden.
Table I want it to be implemented.
screenshot of table:
This is the html table code behind.
<table id="datatable_example" class="responsive table table-striped table-bordered" style="width:100%;margin-bottom:0; ">
<thead>
<tr>
<th style="width:0px; padding-right:0px;" class="jv no_sort"> <label class="checkbox ">
<input type="checkbox">
</label>
</th>
<th style="width:200px;" class=""> Skill </th>
<th style="width:300px;" class="to_hide_phone no_sort"> Description </th>
<th class=""> Rating </th>
<th style="width:200px;" class="ue no_sort"> Date </th>
<th class="ms no_sort "> Actions </th>
</tr>
</thead>
<tbody>
<?php echo $skills; ?>
</tbody>
</table>
This is the skills variable:
$skills="";
while($row=mysql_fetch_array($skills_list))
{
$showskillid=$row['skill_id'];
$showskillname=$row['skill_name'];
$showskilldescription=$row['skill_desc'];
$showskillrating=$row['skill_rating'];
$showskill_lastupdated=$row['last_updated'];
$skills .="<tr>
<td><label class=\"checkbox\">
<input type=\"checkbox\" class=\"chkboxes\" name=\"ids[]\">
</label></td>
<td class=\"to_hide_phone\"> $showskillname </td>
<td class=\"to_hide_phone\">$showskilldescription</td>
<td> $showskillrating </td>
<td> $showskill_lastupdated </td>
<td class=\"ms\"><div class=\"btn-group1\"> <i class=\"gicon-edit\"></i> <a class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"top\" data-original-title=\"View\"><i class=\"gicon-eye-open\"></i></a> <a class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"bottom\" data-original-title=\"Remove\"><i class=\"gicon-remove \"></i></a> </div></td>
</tr>";
}
The Jquery which I tried but I know I am newbie in jquery so need help to fix it.
<script type="text/javascript">
$(document).ready(function() {
$('.delbtn').hide();
});
$('.chkboxes').click(function(){
$('.delbtn').toggle();
});
</script>
Jquery I tried it somehow works like when I click one checkbox the button shows up and when I go and click the other checkbox the button disappears?
I know toggle should not be used here..
What instead should I use here to fix this issue, or if there is more better option to fix it I am open to it..
This will show the Delete button when at least one checkbox is checked.
$('.chkboxes').change(function(){
$('.delbtn').toggle($('.chkboxes:checked').length > 0);
});
But instead of hiding it, the more common approach is to enable and disable the button:
$('.delbtn').prop('disabled', true);
$('.chkboxes').change(function(){
$('.delbtn').prop('disabled', $('.chkboxes:checked').length == 0);
});
Related
This question already has answers here:
jQuery sum rows adding all cells
(3 answers)
Closed 5 years ago.
Below is my table code with Add New button. Onclick Add New
button new row will create. In picture you can see this. User will
input debit and credit values at the end all debit values should be
calculate and sum should be display and same for the credit value. I
am using jQuery to create new row:
Jquery code:
<script type="text/javascript">
$("#add, .check ").click(function() {
$('#datatable-buttons tbody>tr:last')
.clone(true)
.insertAfter('#datatable-buttons tbody>tr:last').find('input').each(function(){
$(this).val('');
});
});
</script>
Html table code:
<table id="datatable-buttons" class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th>
<input type="checkbox" id="check-all" class="flat">
</th>
<th class="column-title">Account</th>
<th class="column-title">Debits</th>
<th class="column-title">Credits</th>
<th class="column-title">Description</th>
<th class="column-title">Name</th>
<th class="column-title no-link last"><span class="nobr">Action</span>
</th>
<th class="bulk-actions" colspan="7">
<a class="antoo" style="color:#fff; font-weight:500;">Bulk Actions ( <span class="action-cnt"> </span> ) <i class="fa fa-chevron-down"></i></a>
</th>
</tr>
</thead>
<tbody>
<tr class="even pointer">
<td class="a-center ">
<input type="checkbox" class="flat" name="table_records">
</td>
<td class=" "><select class="form-control" name="journalname">
<option>Choose option</option>
<?php
$sql = mysqli_query($conn,'SELECT * FROM `chartofaccounts`');
while ($row1 = mysqli_fetch_array($sql))
{?>
<option value="<?php echo $row1["name"];?>"><?php echo $row1["name"];?></option>
<?php }?>
</select></td>
<td class=""><input class="form-control" type="text" name="debit" ></td>
<td class=" "><input class="form-control" type="text" name="credit"></td>
<td class=" "><input class="form-control" type="text" name="descc"></td>
<td class=" "><input class="form-control" type="text" name="name"></td>
<td class=" last">View
</td>
</tr>
</tbody>
</table>
<button type="button" id="add">Add New</button>
Below image will give you clear understanding:
made you a simple example of how to calculate the sum from the input fields.
check snippet below or jsFiddle
add numbers to the 3 inputs, and then click on View , it will get the sum of the 3 input values.
var input = $("input"),
button = $("td.last a")
$(button).click(function () {
var sum = 0;
$(input).each(function() {
sum += Number($(this).val());
});
$(this).html(sum)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>
<input>
</td>
<td>
<input>
</td>
<td>
<input>
</td>
<td class=" last">View
</tr>
</table>
I want to create a variable that contains the data from one of the tables created by using foreach in PHP, using CodeIgniter. tersebuat variables would I use to see the detail of the data from the database which I will show in the form of pop up.
<table class="table table-striped table-bordered table-hover" id="ab">
<thead>
<tr>
<th> # </th>
<th> Invoice </th>
<th> Product </th>
<th> date </th>
<th> suplier </th>
<th> Description </th>
<th> Detail </th>
</tr>
</thead>
<tbody>
<?php $numb=0;foreach($proses as $isi){$numb++; ?>
<tr>
<td> <?php echo $numb;?> </td>
<td> <?php echo $isi->invoice;?> </td>
<td> <?php echo $isi->name;?> </td>
<td> <?php echo $isi->date;?> </td>
<td> <?php echo $suplier;?> </td>
<td> --- </td>
<td class="text-center">
<a class="btn blue btn-outline sbold" data-toggle="modal" href="#full"> Detail </a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
sample output from the program :
1|000012|Noodle|03-Mar-2016|Suplier01|--|(Detail)|
2|000044|Rice |05-Mar-2016|Suplier03|--|(Detail)|
How to enter an invoice number 000012 into a variable (eg, $invoice) on click by invoice details?
I am assuming you want to have the details appearing in the modal.
To do so, you can either:
print out the modal using PHP and have one modal per table row.
Or, have the variables as data attributes somewhere like the button itself.
For 2, example:
<a data-invoice="<?php echo $isi->invoice;?>" class="btn blue btn-outline sbold" data-toggle="modal" href="#full"> Detail </a>
Then use jquery to detect the button click and set the modal's html value.
$('a.btn').click(function(){
$('.modal <your own selector here>').html($(this).data('invoice'));
});
Based on the question asked here: https://www.quora.com/How-do-I-display-a-div-table-when-a-link-is-clicked
I used the code given by the top answer.
Now, my code is modified to use variables:
echo ''.$row['Name'] .'';
It's in a while loop.
<div id="data-table" style="visibility: hidden;">
<table>
<?php
if $Name ==
?>
</table>
</div>
My div is supposed to show the data for the item whose Name is selected. eg: if "Robin" is selected, Robin's data will be shown. so how do I get the div to check which Name/link was clicked and then show the data for that Name/link?
Your question is a little hard to follow. Hopefully this can clear up some of your front-end issues.
HTML:
<div class="container">
<h3>Triggers</h3>
<p class='table-reference' data-record-ref='1'>Highlight Row #1</p>
<p class='table-reference' data-record-ref='2'>Highlight Row #2</p>
<p class='table-reference' data-record-ref='3'>Highlight Row #3</p>
<hr>
<h3>Table</h3>
<table id="record-table">
<thead>
<tr>
<th>ID</th>
<th>Data</th>
</tr>
</thead>
<tbody>
<tr data-record='1'>
<td>#1</td>
<td>Record</td>
</tr>
<tr data-record='2'>
<td>#2</td>
<td>Record</td>
</tr>
<tr data-record='3'>
<td>#3</td>
<td>Record</td>
</tr>
</tbody>
</table>
</div>
Javascript/jQuery:
(function($) {
$(function() {
$("*[data-record-ref]").click(function() {
var id = $(this).data("record-ref");
var element = $("*[data-record='"+id+"']");
if(!!element) {
$(this).toggleClass("highlight");
element.toggleClass("highlight");
} else {
console.error("Record Not Find.");
}
});
});
}) ($);
Demo
How can i pass data from my table to a pop-up.
This is how i did it in my table:
I populate the table from my database using controllers and models
<table class="table table-striped table-checkable" id="demo-dtable-02" aria-describedby="DataTables_Table_0_info">
<thead>
<tr>
<th class="sorting" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Subject: activate to sort column ascending" style="width: 220px;">??</th><th class="sorting" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Date: activate to sort column ascending" style="width: 79px;">??</th>
</tr>
</thead>
<tbody role="alert" aria-live="polite" aria-relevant="all">
<?php
foreach ($my_page_data['text_list']as $text_list_key => $text_list_data){
?>
<tr class="odd">
<td class=" "><a href="#" onclick="viewText(<?php echo $text_list_data['id'] ?>)" data-toggle="modal" data-target="#view-text" ><?php $msg = $text_list_data['msg']; ?></a></td>
<div id="<?php echo $inbox_list_data['id'] ?>" data-content="<?php $msg = $inbox_list_data['message']?>"></div> <!-- i used this as a hidden div for the data to be passed because i dont know other ways -->
<td class=" "><?php $date = $text_list_data['date_updated'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
Then i used a script to pass the data...
<script>
function viewInbox(id){
$('#inbox-message-body').text($('#'+id).attr('data-content'));
}
</script>
And then the the clicked data will be displayed here:
<div class="modal-body">
<div class=" . . .">
<div class="..">
<span class="view-label">From:</span> MY OTHER DATA <!--TODO: -->
<br>
<span class="view-label">To:</span> MY DATA<!--TODO: -->
<br>
</div>
</div>
<!--TODO: Message body--> I
<span id="message-body" class="span8"></span>
</div>
This code are just in one file.
The page im creating is like. I have a table of messages. Then to view message is to click it and a pop-up will appear.
Any insights?
Your help is greatly appreaciated.
The way i do it is i assign modal template to a different controller, and then call it via ajax request like this.
$(".modalCaller").click(function(){
$.ajax({
type: POST,
url: "/insert/modal/controller/here",
data: {id: $(this).data("id")},
success: function(data){
data.appendTo("body").modal("show");
}
})
});
And just drop your modal into another view.
I have construted my table as follows:
<table id="dataTable">
<thead>
<tr><th>Name</th>
<th>Value</th></tr>
</thead>
<TR><TD>Scooby Doo</TD><TD>6</TD><TD><INPUT TYPE="Button" onClick="AddRow()" VALUE="Add Row"></TD></TR>
</table>
When the button Add Row is clicked, I need to change the button to a delete button and insert a new row on the first line. The first line must contain the same as in the code. How can I do this?
On clicking the delete button, Imust be able to delete the row to which the delete button belong?
Hope this helps
$(function(){
$("input[type='button'].AddRow").toggle(
function(){
var el = $(this);
el.closest('tr').clone(true).prependTo(el.closest('table'));
el.attr("value", "Delete row");
},
function(){
$(this).closest('tr').remove();
});
});
<table id="dataTable" border="1">
<thead>
<tr>
<th>
Name
</th>
<th>
Value
</th>
</tr>
</thead>
<tr>
<td>
Scooby Doo
</td>
<td>
6
</td>
<td>
<input type="Button" value="Add Row" class="AddRow">
</td>
</tr>
</table>
Working Demo
Something like?
$('#dataTable thead').prepend('<tr><th>Name</th><th>Value</th></tr>');
And for the deleting:
$('#dataTable thead tr').click(function() {
$(this).hide(); //hide the row, this doesn't delete it.
});
.