I use jQuery's Datatable plugin and jQuery's switchButton plugin, like this:
<table id="test-list" class="cell-border" cellspacing="0" width="100%">
<thead>
<tr>
<th>Enabled ?</th>
</tr>
</thead>
<tbody>
<?php for ($i=0; $i<100; $i++) { ?>
<tr>
<td>
<input type="checkbox" class="enable-disable" name="enable-disable" value="<?php echo $i; ?>" checked />
</td>
</tr>
<?php } ?>
</tbody>
</table>
This is javascript code:
$(document).ready(function () {
// format datatable
$('#test-list').dataTable({
"scrollCollapse": true,
"paging": true,
"info": false,
// other fields
// ...
});
// format checkboxes as switch buttons
$('.enable-disable').switchButton();
// others
// ...
});
The problem is: all checkboxes on the first page of the datatable are formated correctly (switch button type), but when I turn to other pages (from the second page to the end page) checkboxes are not formated (traditional type)
Related
I've been working on a small project where I display multiple tables for a user on a web page. I would like for the users to be able to edit their tables and have that data update in the database. I'm using the jQuery tabledit plugin to achieve this.
I followed this tutorial Live editable tables and was able to successfully get this to work for one table.
The problem I'm having is this doesn't seem to be working for all my tables. I can only click into the first displayed table but I need to be able to edit all of them.
I've searched for anybody trying to achieve something similar but couldn't find much.
<?php
// ...
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<table id="data_table">
<thead>
<tr>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row ['col1_data']; ?></td>
<td><?php echo $row ['col2_data']; ?></td>
<td><?php echo $row ['col3_data']; ?></td>
<td><?php echo $row ['col4_data']; ?></td>
</tr>
</tbody>
</table>
<?php
}
}
$(document).ready(function () {
$('#data_table').Tabledit({
deleteButton: false,
editButton: false,
columns: {
identifier: [ 0, 'col1_data' ],
editable: [ [ 1, 'col2_data' ], [ 2, 'col3_data' ], [ 3, 'col4_data' ] ],
},
hideIdentifier: false,
url: 'includes/liveedit.inc.php',
})
})
I think this may be a problem with each table having the same identifier but I'm not sure. I return each table from the database with a unique identifier but I'm not sure how to use that with the plugin (it seems like I need to).
Sorry if this has an easy solution. I'm pretty new to php and jQuery.
Thanks in advance for any help and please let me know if I'm not being clear enough!
You have an id and it must be unique. Your while statement creates many tables with the same id.
Your JS always going to the first table with such id. Change it on class. Don't use id if you wanna apply your JS code at once.
$(document).ready(function() {
$('.data_table').Tabledit({
deleteButton: false,
editButton: false,
columns: {
identifier: [0, 'col1_data'],
editable: [[1, 'col2_data'], [2, 'col3_data'], [3, 'col4_data']]
},
hideIdentifier: false,
url: 'includes/liveedit.inc.php'
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jquery-tabledit#1.0.0/jquery.tabledit.min.js"></script>
<table class="data_table">
<thead>
<tr>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
</tr>
</thead>
<tbody>
<tr>
<td>rew</td>
<td>ter</td>
<td>yrt</td>
<td>qwe</td>
</tr>
</tbody>
</table>
<table class="data_table">
<thead>
<tr>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
</tr>
</thead>
<tbody>
<tr>
<td>data_column1</td>
<td>dat2a_column1</td>
<td>da3ta_column1</td>
<td>data_co5lumn1</td>
</tr>
</tbody>
</table>
Press ENTER in the snippet for saving the new value
I'm building a laravel application where I want to show if there is any update data in database after two seconds.So in this scenario I have given the table a 'id' and then I have laod the table after certain time interval. But the problem is after every javascript call it creates an empty row in table even if the table retrieve the value also. How do I avoid the problem..any suggestion please?
<div class="container">
<h3> List Of Courses </h3></br>
<table class="table table-striped table-bordered dataTable" id="example">
<thead>
<tr>
<td>Serial No</td>
<td>Title</td>
<td>Description</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<?php $i=1; ?>
#foreach($items as $row)
<tr>
<td>{{$i}}</td>
<td class="title" data-id1="{{$row->id}}" contenteditable>{{$row->title}}</td>
<td class="description" data-id2="{{$row->id}}" contenteditable>{{$row->description}}</td>
<td>
<button type="button" onclick="deleteItem({{ $row->id }})" class="btn btn-danger">Delete</button>
</td>
</tr>
<?php $i++; ?>
#endforeach
</tbody>
</table>
</div>
Here is the javascript I have to use to load the table after 3 seconds:
<script type="text/javascript">
setInterval(function() {
$("#example").load("list #example");
}, 30000);
</script>
You need to give load a callback and check if you returned any data.
Without knowing the return data structure my best guess would be
$("#example").load("list #example", function (data) {
// hopefully you are returning json. if not, return json. It's the artisan way :D
let parsed = JSON.parse(data);
if (parsed) {
// build html and inject in dom
}
});
See this is how I actually solved the problem,
I put the table inside a new div and I called the jquery .load() on that div.
Like this :
<div class="reloadTable">
<table class="table table-striped table-bordered dataTable" id="example">
// table data
</table>
</div>
javaScript :
<script type="text/javascript">
setInterval(function() {
$(".reloadTable").load("list #example");
}, 3000);
</script>
I have the problem to fetch the as per number of rows at a time in jQuery DataTables with Bootstrap. Firstly, my code below fetches whole data from the database and starts the pagination.
But I want data loaded for one page at a time.
My PHP code is:
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th> Name</th>
<th>category</th>
<th>Location</th>
</tr>
</thead>
<tbody>
<?php foreach ($datastat as $row) {
?>
<tr>
<td><?php echo $row['fname'];?> <?php echo $row['mname'];?> <?php echo $row['lname'];?></a></td>
<td><?php echo $row['category'];?></td>
<td><?php echo $row['location'];?></td>
</tr>
<?php }?>
</tbody>
<tfoot>
<tr>
<th> Name</th>
<th>category</th>
<th>Location</th>
</tr>
</tfoot>
</table>
My javascript code is:
$(function() {
$('#example1').dataTable({
"bPaginate": true,
"bLengthChange": true,
"bFilter": true,
"bSort": true,
"pageLength": 10
});
});
You need to implement server-side processing in order to retrieve one page at a time from the server, see this example.
Luckily DataTables distribution includes PHP class ssp.class.php and sample code that makes it fairly easy to use with PHP.
I have a datatable and buttons for delete and update. And these button are disabled. I also have some checkbox in the first column of every row. Whenever I click the checkbox, the edit and delete button will be enabled. It is working on the first page of my datatable but when I click a row from other pages of the datatable, it doesn't enabling the buttons. What is the problem with it? I have this code:
HTML Table:
<table class="table table-bordered table-hover table-striped" id="account_data" width="100%" style="margin-top: 0px"> <thead class="header">
<tr class="well"><th colspan="3" style="text-align:center">LIST OF USERS</th></tr>
<tr class="well">
<th style="width: 5px"><input type="checkbox" class="check"/></th>
<th style="font-size: 14px;padding-left: 20px;"> ID#</th>
<th style="font-size: 14px;padding-left: 20px;"> Name</th>
</tr>
</thead>
<tbody>
<?php if($users != NULL){?>
<?php foreach($users as $row){ ?>
<tr>
<td align="center"><input type="checkbox" class="accounts" name="accounts[]" value="<?php echo $row->user_id;?>"/></td>
<td style="font-size: 15px;padding-left: 20px;"><?php echo $row->user_id;?></td>
<td style="font-size: 15px;padding-left: 20px;"><?php echo $row->user_name;?></td>
</tr>
<?php }?>
<?php }?>
</tbody>
</table>
JQUERY:
<script>
$(document).ready(function(){
var count=0;
var chkId=new Array();
var checkedValue ='';
$('.check').click(function() {
$(" .check").each(function () {
if($(this).is(":checked"))
{
$(" .accounts").prop('checked',true);
// $('#edit_acc').prop('disabled',false);
var n = $( " .accounts:checked" ).length;
if(n==1){
$('#edit_acc').prop('disabled',false);
}
if(n>1){
$('#edit_acc').prop('disabled',true);
}
$('#delete_acc').prop('disabled',false);
return
}
$(" .accounts").prop('checked',false);
$('#edit_acc').prop('disabled',true);
$('#delete_acc').prop('disabled',true);
});
});
});
</script>
The problem is that the checkboxes on page #2 and forth not is part of the DOM when your $(document).ready() is fired. jQuery dataTables inject and remove rows in order to show different pages.
So you must use a delegated event handler instead of click(). By that, dynamically injected rows (i.e their checkboxes) also will be included :
$('#account_data').on('click', '.check', function() {
...
});
instead of $('.check').click(function() {
I've created a filter to show only rows containing td's with the value selected from a dropdown. The filter works fine first time but second time i run it, all rows dissapear and I cant figure out why.
This is my filter:
$(document).ready(function(){
$('select[name=selectedName]').change(function() {
$('tr').filter(function () {
return $(this).find('td.userName').filter(function () {
return $(this).text().indexOf($('select[name=selectedName]').val()) == -1;
}).length;
}).hide();
});
});
The drop down:
$query = "SELECT user_name FROM users";
$result = mysql_query($query); ?>
<select name="selectedName" id="userSelected">
<option value="" disabled selected>user name</option>
<?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
<option value="<?php echo $line['user_name'];?>">
<?php echo $line['user_name'];?>
</option>
<?php } ?>
</select>
And finally the creation of the table:
<table class="table table-bordered table-hover" ng-controller="tableCtrl">
<thead>
<th>user name</th>
<th>script name</th>
<th>cron format<span class="glyphicon glyphicon-question-sign"></span></th>
<th>schedule last update</th>
<th>next execution time</th>
<th>script exec</th>
</thead>
<tbody ng-repeat="(user_id,script_id) in data">
<tr ng-repeat="(script_id, cron_format) in script_id">
<td class="userName">{{user(user_id)}}</td>
<td class="scriptName">{{script(script_id)}}</td>
<td class="cronFormat"><span contenteditable="true" ng-repeat="l in letters(cron_format) track by $index">{{l}}</span></td>
<td>{{compare(user_id,script_id,cron_format)[0]}}</td> <!--[0] represents scheduler last update-->
<td>{{compare(user_id,script_id,cron_format)[1]}}</td> <!--[1] represents next execution time-->
<td>{{compare(user_id,script_id,cron_format)[2]}}</td> <!--[2] represents script_exec-->
</tr>
</tbody>
</table>
Any idea why is it happening? Thanks for helping...
UPDATE
i added $('tr').show(); and function works except, how can i add value in dropdown to show the all table/cancel the filter?
You take care of hiding rows, but you never show them back. This is why your table sooner or later will have all rows invisible.