I have the below table:
<input type="search" class="light-table-filter" id="search" data-
table="order-table" placeholder="Kerko">
<table border="0" class=" table table-striped table-hover table-bordered table-condensed tableDemo bordered order-table table" >
<tr id="header">
<th>ID</th>
<th>emri</th>
<th>mbiemri</th>
<th>username</th>
<th>password</th>
<th>email</th>
<th>Nr Tel</th>
<th>ACTION</th>
</tr>
<?php
if(count($records)){
$i = 1;
$eachRecord= 0;
foreach($records as $key=>$eachRecord){
?>
<tr id="<?=$eachRecord['ID'];?>">
<td><?=$eachRecord['ID'];?></td>
<td class="Emri"><?=$eachRecord['Emri'];?></td>
<td class="Mbiemri"><?=$eachRecord['Mbiemri'];?></td>
<td class="Username"><?=$eachRecord['Username'];?></td>
<td class="Password"><?=$eachRecord['Password'];?></td>
<td class="Email"><?=$eachRecord['Email'];?></td>
<td class="nrtel"><?=$eachRecord['nrtel'];?></td>
<td>
<img src="" class="eimage"> <span class="glyphicon glyphicon-pencil"></span>
<img src="" class="dimage"> <span class="glyphicon glyphicon-trash"></span>
</td>
</tr>
<?php }
}
?>
</table>
Also,i want to make a real time search and i have the below jquery code:
<script type="text/javascript">
(function(document) {
'use strict';
var LightTableFilter = (function(Arr) {
var _input;
function _onInputEvent(e) {
_input = e.target;
var tables = document.getElementsByClassName(_input.getAttribute('data-table'));
Arr.forEach.call(tables, function(table) {
Arr.forEach.call(table.tBodies, function(tbody) {
Arr.forEach.call(tbody.rows, _filter);
});
});
}
function _filter(row) {
var text = row.textContent.toLowerCase(), val = _input.value.toLowerCase();
row.style.display = text.indexOf(val) === -1 ? 'none' : 'table-row';
}
return {
init: function() {
var inputs = document.getElementsByClassName('light-table-filter');
Arr.forEach.call(inputs, function(input) {
input.oninput = _onInputEvent;
});
}
};
})(Array.prototype);
document.addEventListener('readystatechange', function() {
if (document.readyState === 'complete') {
LightTableFilter.init();
}
});
})(document);
Everything it works perfectly.But I want to search all rows except the header of table that has id #header.I can't change the jquery code.Also I have tried some different jquery but only the above jquery code works perfect for me. The only issue is that the code when I write something,it find the row but the header hidded.
Related
I am a fresh graduate I am studying Codeigniter for the first time and I am having a hard time pls help me to fix my issue. I created a system that records employees information. I included a live search in my code. There are no error in syntax but there is no results shown. Below is my code.
Here is the controller. It's file name is Crud.php
Controller
public function fetch()
{
$output = '';
$query = '';
if($this->input->post('query'))
{
$query = $this->input->post('query');
}
$data = $this->Crudmodel->fetch();
$output .= '
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Job Title</th>
</tr>
';
if($data->num_rows() > 0)
{
foreach($data->result() as $row)
{
$output .= '
<tr>
<td>'.$row->fname.'</td>
<td>'.$row->lname.'</td>
<td>'.$row->job_title.'</td>
</tr>
';
}
}
else
{
$output .= '<tr>
<td colspan="5">No Data Found</td>
</tr>';
}
$output .= '</table>';
echo $output;
}
Here is the model. File name: Crudmodel.php
Model
public function fetch_data($query){
$this->db->select("*");
$this->db->from("employees");
if($query != ''){
$this->db->or_like('fname', $query);
$this->db->or_like('lname', $query);
$this->db->or_like('job_title', $query);
}
$this->db->order_by('id');
return $this->db->get();
}
View
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Employee Registation</h2>
</div><br>
<div class="pull-right">
<a class="btn btn-success" href="<?php echo base_url('crud/create') ?>"> Add Employee</a>
<a type="button" class="btn btn-danger" href="crud/logout">Logout</a>
</div>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Search</span>
<input type="text" name="search_text" id="search_text" placeholder="Search Employee" class="form-control" />
</div>
</div>
<div id="result"></div>
<div style="clear:both"></div>
<table class="table table-bordered">
<thead>
<tr>
<!-- <th>Full Name</th> -->
<th>First Name</th>
<th>Last Name</th>
<th>Job Title</th>
<th width="220px">Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $employees)
{
?>
<tr>
<td> <?php echo $employees->fname; ?></td>
<td> <?php echo $employees->lname; ?></td>
<td> <?php echo $employees->job_title; ?></td>
<td>
<form method="DELETE" action="<?php echo base_url('crud/delete/'.$employees->id); ?>">
<a class="btn btn-info" href="<?php echo base_url('crud/show/'.$employees->id) ?>"> View</a>
<a class="btn btn-primary" href="<?php echo base_url('crud/edit/'.$employees->id) ?>"> Edit</a>
<button type="submit" class="btn btn-danger"> Delete</button>
</form>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
<script>
$(document).ready(function () {
load_data();
function load_data(query)
{
$.ajax({
url: "<?php echo base_url('crud/fetch'); ?>",
method: "POST",
data: {query: query},
success: function (data) {
$('#result').html(data);
}
})
}
$('#search_text').keyup(function () {
var search = $(this).val();
if (search != '')
{
load_data(search);
} else
{
load_data();
}
});
});
</script>
Please help me to figure it out. Your help will be appreciated!
First, in your controller you are trying to call a method that may not exist:
$data = $this->Crudmodel->fetch();
Because in your model the method is named "fetch_data". So I think you intend to do this:
$data = $this->Crudmodel->fetch_data();
Second, you are not passing the query parameter to your model's method. It should be like this:
$data = $this->Crudmodel->fetch_data( $query );
These are two essential things that should help get you started.
Also note that it is not shown if crudmodel is loaded. You may need:
$this->load->model('crudmodel');
When you have a Crudmodel loaded, you don't need to uppercase your object usage. So in the end, you should probably have this line:
$data = $this->crudmodel->fetch_data( $query );
Besides the problems I pointed out in my other answer, I thought I'd help by cleaning things up a bit. Although not tested, this might help:
How about this:
Controller
public function fetch()
{
$data = $this->crudmodel->fetch_data();
echo $this->load->view('snippet', ['data' => $data], TRUE );
}
New view snippet.php
$output = '
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Job Title</th>
</tr>
';
if( ! empty( $data ) )
{
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->fname.'</td>
<td>'.$row->lname.'</td>
<td>'.$row->job_title.'</td>
</tr>
';
}
}
else
{
$output .= '<tr>
<td colspan="5">No Data Found</td>
</tr>';
}
$output .= '</table>';
echo $output;
Model
public function fetch_data()
{
$query = $this->input->post('query');
$this->db->from("employees");
if( ! empty($query) )
{
$this->db->or_like('fname', $query);
$this->db->or_like('lname', $query);
$this->db->or_like('job_title', $query);
}
$this->db->order_by('id','ASC');
$data = $this->db->get();
if( $data->num_rows() > 0 )
return $data->result();
return NULL;
}
In your existing view
<script>
$(document).ready(function () {
load_data();
function load_data(query)
{
if(query == undefined)
query = '';
$.ajax({
url: "<?php echo site_url('crud/fetch'); ?>",
method: "POST",
data: {'query': query},
dataType: "html",
success: function (data) {
$('#result').html(data);
}
})
}
$('#search_text').keyup(function () {
var search = $(this).val();
if (search != '')
{
load_data(search);
} else
{
load_data();
}
});
});
</script>
On manual page refresh the modals are working fine. But when reloading the datatable with Ajax the popups stop working.
Below is my HTML and JavaScript code:
<table class="table table-bordered table-striped table-hover mb-none" id="IVRFilesTable">
<thead>
<tr class="primary">
<th>IVR Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="tablediv">
<?php foreach($records as $row) {?>
<tr>
<td><?php echo $row['ivrname']?></td>
<td width="10%" class="actions text-center">
<i class="fa fa-trash-o fa-lg text-primary"></i>
</td>
</tr>
<?php }?>
</tbody>
var request = $.ajax({
url: "<?php echo base_url();?>index.php/admin/manageivrs/removeivr",
type: "POST",
data: "ivrid="+ivrid,
success: function (msg)
{
if(msg=='true')
{
var oTable = $('#IVRFilesTable').dataTable();
oTable.fnClearTable();
$("#IVRFilesTable").load(location.href+" #IVRFilesTable>*","");
oTable.fnDraw();
}
}
});
I just want a function like when click toggle button the numeric values should be changed into percentage value by formula.
<table class="table table-striped table-bordered scrollable">
<thead>
<tr class="success">
<th>Answer</th>
<th>Total</th>
<th>PCP</th>
<th>OB/GYN</th>
<th>PAIN</th>
</tr>
</thead>
<tbody><tr><td class="left-align">Male</td>
<td>123</td>
<td>72</td>
<td>18</td>
<td>33</td>
</tr><tr><td class="left-align">Female</td>
<td>78</td>
<td>48</td>
<td>22</td>
<td>8</td>
</tr><tr><td class="left-align">All Respondent</td>
<td>201</td>
<td>120</td>
<td>40</td>
<td>41</td>
</tr></tbody>
</table>
my formula 201*value/100. can anybody tell the way how i can do this.with PHP and jquery.Any suggestion.
Try this
$('table tr td').each(function () {
var value = $(this).text();
if ($.isNumeric(value)) {
var final = 201 * value/ 100;
$(this).text(final)
}
});
DEMO
Yes it is possible using jQuery. You can do something like: (Sample)
<form method="POST">
<button type="button" id="toggle" data-state="off">Toggle</button>
<table class="table table-striped table-bordered scrollable" cellpadding="10">
<thead><tr class="success"><th>Answer</th><th>Total</th><th>PCP</th><th>OB/GYN</th><th>PAIN</th></tr></thead>
<tbody><tr><td class="left-align">Male</td><td>123</td><td>72</td><td>18</td><td>33</td></tr><tr><td class="left-align">Female</td><td>78</td><td>48</td><td>22</td><td>8</td></tr><tr><td class="left-align">All Respondent</td> <td>201</td> <td>120</td> <td>40</td> <td>41</td> </tr></tbody>
</table>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var original = $('tbody').html();
$('#toggle').on('click', function(){
if($('#toggle').attr('data-state') == 'off') {
$('#toggle').attr('data-state', 'on').css({backgroundColor: 'green', color: 'white'});
$('td').each(function(index, element){
var current = $(this).text();
if($.isNumeric(current)) {
$(this).text(201*current/100);
}
});
} else {
$('#toggle').attr('data-state', 'off').css({backgroundColor: 'red', color: 'white'});
$('tbody').html(original);
}
});
});
</script>
This should toggle the original values and computed values
You can use .text(fn) to update the text of any element via a callback:
$('.table').find('td').text(function(_, value) {
return isNaN(+value) ? value : Math.round(201 * parseInt(value, 10) / 100) + '%';
});
i have a script which works for deletion confirmation...
$('document').ready(function(){
$('.ask').jConfirmAction();
});
and the table of products listing is under below
<?php if(isset($query)){ ?>
<input disabled type="hidden" id="recNum" value="<?php echo $rec ?>"/>
<table id="rounded-corner" summary="2007 Major IT Companies' Profit" width="100%">
<thead>
<tr>
<th scope="col" class="rounded-company"></th>
<th scope="col" class="rounded">Product ID</th>
<th scope="col" class="rounded">Product Name</th>
<th scope="col" class="rounded">Product slug</th>
<th scope="col" class="rounded">Type</th>
<th scope="col" class="rounded">Product Category</th>
<th scope="col" class="rounded">Edit</th>
<th scope="col" class="rounded-q4">Delete</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="7" class="rounded-foot-left"><em>All The Pages That Being Included</em></td>
<td class="rounded-foot-right"> </td>
</tr>
</tfoot>
<tbody>
<?php
foreach($query as $key => $row) {
?>
<tr>
<td><input type="checkbox" name="" /></td>
<td><?php echo $row['product_id'];?></td>
<td><?php echo $row['product_name'];?></td>
<td><?php echo $row['product_slug'];?></td>
<td><?php echo $row['type'];?></td>
<td><img src="<?php echo base_url().$row['product_image'];?>" width="80px" height="80px"/></td>
<td><img src="<?php echo base_url();?>assets/admin_assets/images/user_edit.png" alt="" title="" border="0" /></td>
<td><img src="<?php echo base_url();?>assets/admin_assets/images/trash.png" alt="" title="" border="0" /></td>
</tr>
<?php
}?>
</tbody>
</table>
<span class="bt_green_lft"></span><strong>Add new item</strong><span class="bt_green_r"></span>
<span class="bt_blue_lft"></span><strong>View all items from category</strong><span class="bt_blue_r"></span>
<span class="bt_red_lft"></span><strong>Delete items</strong><span class="bt_red_r"></span>
heres the jquery for the jQuery confirmation:
/*
* jQuery Plugin : jConfirmAction
*
* by Hidayat Sagita
* http://www.webstuffshare.com
* Licensed Under GPL version 2 license.
*
*/
(function($){
jQuery.fn.jConfirmAction = function (options) {
// Some jConfirmAction options (limited to customize language) :
// question : a text for your question.
// yesAnswer : a text for Yes answer.
// cancelAnswer : a text for Cancel/No answer.
var theOptions = jQuery.extend ({
question: "Are You Sure ?",
yesAnswer: "Yes",
cancelAnswer: "Cancel"
}, options);
return this.each (function () {
$(this).bind('click', function(e) {
e.preventDefault();
thisHref = $(this).attr('href');
if($(this).next('.question').length <= 0)
$(this).after('<div class="question">'+theOptions.question+'<br/> <span class="yes">'+theOptions.yesAnswer+'</span><span class="cancel">'+theOptions.cancelAnswer+'</span></div>');
$(this).next('.question').animate({opacity: 1}, 300);
$('.yes').bind('click', function(){
window.location = thisHref;
});
$('.cancel').bind('click', function(){
$(this).parents('.question').fadeOut(300, function() {
$(this).remove();
});
});
});
});
}
})(jQuery);
all the thing was working well,
then i made ana jax pagination for the lists;
now the problem is that after this task, the jquery confirmation isnt working, i want the jquery script to be converted into simple jquery function like this
function ask()
{
}
EDIT:
i have written the function
function ask(obj)
{
alert('hii');
//var theOptions = jQuery.extend ({
var question= "Are You Sure ?";
var yesAnswer= "Yes";
var cancelAnswer= "Cancel";
//e.preventDefault();
var thisHref = $(obj).attr('href');
alert(thisHref) ;
if($(obj).next('.question').length <= 0)
$(obj).after('<div class="question">'+question+'<br/> <span class="yes">'+yesAnswer+'</span><span class="cancel">'+cancelAnswer+'</span></div>');
$(obj).next('.question').animate({opacity: 1}, 300);
$('.yes').bind('click', function(){
window.location = thisHref;
});
$('.cancel').bind('click', function(){
$(obj).parents('.question').fadeOut(300, function() {
$(obj).remove();
});
});
}
and i am calling this from
<img src="<?php echo base_url();?>assets/admin_assets/images/trash.png" alt="" title="" border="0" />
its taking the effect also;
but just after the animating effect, its getting redirected to the link, i need to make it stay, so that user can choose the option. but i cant...
MORE EDIT:
Ok i got the answer; i used the small code return false and it solved my problem
but then there's another problem
this line of code isnt working
$('.cancel').bind('click', function(){
$(obj).parents('.question').fadeOut(300, function() {
$(obj).remove();
});
});
You need to bind $('.ask').jConfirmAction(); to all the links again after you have gone to the new page.
I have a results list produced by a search. Each item has a check box. What I want to do is post the selected items to a different table.
Here is the code that displays the data:
<section class="grid_12">
<div class="block-border">
<form class="block-content form" id="table_form" method="post" action="">
<h1>Sortable table</h1>
<table id="mylist" data-campaignId="<?=$campaignid?>" class="table sortable no-margin" cellspacing="0" width="100%">
<thead>
<tr>
<th class="black-cell"><span class="loading"></span></th>
<th scope="col">
<span class="column-sort">
</span>
Name
</th>
<th scope="col">Client Of</th>
<th scope="col">
<span class="column-sort">
</span>
Home Phone
</th>
<th scope="col">
<span class="column-sort">
</span>
Email
</th>
<th scope="col">
<span class="column-sort">
</span>
Created On
</th>
<th scope="col" class="table-actions">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach($r->result() as $row) : ?>
<tr data-clientid="$row->id">
<td class="th table-check-cell">
<input type="checkbox" name="selected[]" class="checkbox">
</td>
<td><?=$row->firstname?> <?=$row->lastname?></td>
<td><ul class="keywords">
<li><?=$row->clientof?></li>
<li class="orange-keyword"><?=$row->status?></li>
</ul></td>
<td><?=$row->homephone?></td>
<td><?=$row->email?></td>
<td><?=$row->created?></td>
<td class="table-actions">
<img src="/images/icons/fugue/pencil.png" width="16" height="16">
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<div class="clear"></div>
<div class="block-footer">
<img src="images/icons/fugue/arrow-curve-000-left.png" width="16" height="16" class="picto">
Select All
Unselect All
<span class="sep"></span>
<button id="post_diff_table">POST selected</button>
</div>
</form></div>
</section>
Here is all the current javascript in the above page.
<? $campaignid = $this->uri->segment(4); ?>
<script type="text/javascript">
<!--
$(document).ready(function(){
toggleChecked = function(status)
{
alert('trigger');
$(".checkbox").each( function() {
$(this).attr("checked",status);
});
}
});
//-->
</script>
<script type="text/javascript">
$(function() {
$('#post_diff_table').click(function() {
var table = $('table#mylist');
var campagnid = table.data('campaignId');
//Array of checked client ids
var checked_clients = $.map(table.find('tr td.table-check-cell input:checked'), function(e) {
return $(e).parents('tr').data('clientid');
});
if (checked_clients.length === 0) {return false;}
//call the controller
$.post('/ajaxmodals/campaign_post_to_other_table', {
campagnid: campagnid,
clients: checked_clients
}, function(data) {
//process response
},"json");
});});
</script>
Here is the code in ajax controller.
function campaign_post_to_other_table()
{
$campagnid=$this->input->post('campagnid');
/* #var Array */
$clients = $this->input->post('clients');
foreach ($clients as $client_id) {
$clientid = $this->input->post('clientid');
$submit['clientid'] = $clientid;
$submit['campaignid'] = $this->input->post('campaignid');
$submit['creationdate'] = date('Y-m-d H:i:s');
$submit['createdby'] = $this->session->userdata('uid');
//print_r($submit); exit();
$newid = $this->campaign_model->add_user_to_campaign($submit);
$cname = $this->client_model->get_client_name($clientid);
echo json_encode(array("response"=>"Success","name"=>$cname,"id"=>$newid,"clientid"=>$clientid));
}
}
Here is the add_user_to_campaign model.
function add_user_to_campaign($data)
{
$this->db->insert('campaign_to_client',$data);
$newid = $this->db->insert_id();
return $newid;
}
In table definition (I assume that $campaignid is campaignid, change mylist to whatever you want):
<table id="mylist" data-campaignId="<?=$campaignid?>" class="table sortable no-margin" cellspacing="0" width="100%">
In your loop set data for client
<?php foreach($r->result() as $row) : ?>
<tr data-clientid="$row->id">
<td class="th table-check-cell">
<input type="checkbox" name="selected[]" class="checkbox">
</td>
<!---other td here -->
</tr>
<?php endforeach;?>
</table>
<!--button to post all selected items -->
<button id="post_diff_table">POST selected</button>
javascript:
$(function() {
$('#post_diff_table').click(function() {
var table = $('table#mylist');
var campagnid = table.data('campaignId');
//Array of checked client ids
var checked_clients = $.map(table.find('tr td.table-check-cell input:checked'), function(e) {
return $(e).parents('tr').data('clientid');
});
if (checked_clients.length === 0) {return false;}
//call the controller
$.post('/ajaxmodals/campaign_post_to_other_table', {
campagnid: campagnid,
clients: checked_clients
}, function(data) {
//process response
},"json");
});});
in controller:
function campaign_post_to_other_table()
{
$campagnid=$this->input->post('campagnid');
/* #var Array */
$clients = $this->input->post('clients');
foreach ($clients as $client_id) {
/* sql insertion code here
or call $this->campaign_model->add_user_to_campaign for each clientid
*/
}
}
The answer is not the copy-pastable code, as i have no idea of your project internals, but you should get the idea.
the suggestions on other code: do not do this in js:
$.post('/ajaxmodals/campaign_subscriber_add', {
clientid : cid,
campaignid : <?=$r->id?>
get the campaignid from table data as in my sample
do not do this:
$("#subscriberList").append('<li id="'+data.id+'">'+data.name+'<span style="display: inline; float: right" onclick="javascript:delete_subscriber(\''+data.id+'\')">delete</span></li>');
compose element in js, add event handlers, etc. your way make you to use to much global functions and make the code unreadable.