I added a search field to show live my data, but nothing works when I fill that field.
I made a route called retour.action, and that's in my controller, so when I try a console.log('test') i can see test in my Console on my browser, but the rest of the code I made doesn't work, and I also get no error
here is my controller
public function action(Request $request)
{
if ($request->ajax()) {
$output = '';
$query = $request->get('query');
if ($query != '') {
$retours = Returnorder::all()
->where('firmaname', 'like', '%' . $query . '%')
->orWhere('ordernumber', 'like', '%' . $query . '%')
->orWhere('status', 'like', '%' . $query . '%')
->get();
} else {
$retours = Returnorder::latest('id')->paginate(15);
}
$total_row = $retours->count();
if ($total_row > 0) {
foreach ($retours as $retour) {
$output .= '
<tr>
<td>' . $retour->firmaname . '</td>
<td>' . $retour->ordernumber . '</td>
<td>' . $retour->status . '</td>
</tr>
';
}
} else {
$output = '<tr>
<td align="center" colspan="5">Geen data gevonden</td>
</tr>
';
}
$retours = array(
'table_data' => $output,
);
echo json_encode($retours);
}
}
And this is my script
$(document).ready(function(){
fetch_customer_data();
function fetch_customer_data(query = '')
{
$.ajax({
url:"{{ route('retour.action') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(retours)
{
$('tbody').html(retours.table_data);
}
})
}
$(document).on('keypress', '#search', function(){
let query = $(this).val();
fetch_customer_data(query);
});
});
And the HTML is this
#extends('layouts.app')
#section('content')
<div class="container">
<div class="mTop">
<div class="row justify-content-center">
<div class="col-md-10">
#if(session('message'))
<div class="alert alert-success" role="alert">
{{session('message')}}
</div>
#endif
<div class="card">
<div class="card-header">Retourmeldingen</div>
<div class="card-body">
<div class="form-group" >
<label for="search" hidden>Zoeken</label>
<input type="text" name="search" id="search" class="form-control"
placeholder="Typ hier uw zoekopdracht in"/>
</div>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Firmanaam</th>
<th scope="col"></th>
<th scope="col"></th>
<th scope="col">Ordernummer</th>
<th scope="col">Status</th>
<th scope="col">Verwerkingstijd</th>
<th scope="col">Inzenddatum</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
Help me please
I think you first need to be sure that what you're typing is actually being sent back to the router. You can get the value of what you're typing by using this:
$(function() {
$('#search').on('keyup', (e) => {
console.log(e.target.value);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="search" type="text" name="search" />
Related
Just try to make it simple.
I'm trying to show data in my data table when I click a dropdown value
I'm using ajax to get the data from my controller
everything works fine except the results isn't showing in my view even the status code shows 200 ok
here's my Model
function tampil_renja(){
$this->db->distinct();
$this->db->select
(
'lkp_program.progskpd_id AS prog_id,
CONCAT(msr_mstrurusan.uru_kode, '.', lkp_program.progskpd_kode) AS prog_kode,
lkp_program.progskpd_prog_uraian as prog_uraian');
$this->db->from('msr_mstrurusan');
$this->db->join('lkp_program', 'msr_mstrurusan.uru_id = lkp_program.progskpd_ursid');
$this->db->join('lkp_kegiatan', 'lkp_program.progskpd_id = lkp_kegiatan.kegskpd_prog_id');
$this->db->join('mnv_keg_renja', 'lkp_kegiatan.kegskpd_id = mnv_keg_renja.tar_keg_id');
$this->db->where('mnv_keg_renja.tar_keg_id is NOT NULL', NULL, FALSE);
$this->db->order_by("lkp_program.progskpd_id", "asc");
return $this->db->get();
}
here's my ajax code within the view
<div class="container-fluid">
<header>
<h3 class="mb-2">Renja</h3>
</header>
<div class="card-body">
<div class="table-responsive" style="margin-bottom: 1px;">
<?php
if (count($skpd) > 0) {
?>
<label class="col-sm-1 control-label">OPD</label>
<div class="col-sm-5">
<select id="skpd" name="skpd" class="form-control input-sm">
<option value="0">-- Pilih Perangkat Daerah--</option>
<?php
foreach ($skpd as $pd) {
echo "<option value='" . $pd->uk_kowil . "#" . $pd->uk_id . "'>" . strtoupper($pd->uk_nama) . "</option>";
}
?>
</select>
</div>
<?php
}
else {
?>
<input type='hidden' name='skpd' id='skpd' value='<?php
?>' />
<?php
}
?>
<div id="listskpd" class="col-sm-1">
<i id="rldspin" style="margin-top:5px"></i>
</div>
<div id="jml_angg" class="text-right" style="padding-top: 8px;font-size: 13px;margin-right: 15px;">
</div>
</div>
<table id="list_target" class="table table-striped table-bordered table-hover" width="100%">
<thead>
<tr>
<th rowspan="3" class="text-center text-middle" style="width:1%;">No</th>
<th rowspan="3" class="text-center text-middle" style="width:4%;">Kode Rekening</th>
<th rowspan="3" class="text-center text-middle" style="width:27%;">Program/Kegiatan</th>
<th rowspan="3" class="text-center text-middle" style="width:20%;">Indikator Kinerja program (outcome)/ kegiatan (output) </th>
<th colspan="7" class="text-center text-middle" style="width:30%;">Target Renja [berdasarkan DPA] SKPD pada Tahun</th>
</tr>
<tr>
<th rowspan="2" class="text-center text-middle" style=""> Satuan</th>
<th colspan="2" class="text-center text-middle" style="">Target Triwulan 1</th>
<th colspan="2" class="text-center text-middle" style="">Target Triwulan 2</th>
<th colspan="2" class="text-center text-middle" style="">Target Triwulan 3</th>
<th colspan="2" class="text-center text-middle" style="">Target Triwulan 4</th>
<th rowspan="2" class="text-center text-middle" style="">Target Anggaran</th>
</tr>
<tr>
<th class="text-center text-middle" style="">Kin</th>
<th class="text-center text-middle" style="">Keu</th>
<th class="text-center text-middle" style="">Kin</th>
<th class="text-center text-middle" style="">Keu</th>
<th class="text-center text-middle" style="">Kin</th>
<th class="text-center text-middle" style="">Keu</th>
<th class="text-center text-middle" style="">Kin</th>
<th class="text-center text-middle" style="">Keu</th>
</tr>
</thead>
<tbody id="datatarget">
</tbody>
</table>
</div>
</div>
</article>
<script type="text/javascript" language="javascript" src="<?php echo base_url().'assets/vendor/jquery/jquery.js'?>"></script>
<script type="text/javascript" language="javascript" src="<?php echo base_url().'assets/vendor/datatables/jquery.dataTables.js'?>"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#skpd').change(function(){
var arrval = $(this).val().split("#");
var kowil = arrval[0];
var idpd = arrval[1];
if(idpd != 0){;
tampil_data_renja(kowil, idpd);
} else {
$('#datatarget').DataTable.Destroy();
$('#datatarget').DataTable.Draw();
$('#datatarget').DataTable.Destroy();
}
});
function tampil_data_renja(kowil, idpd){
$.ajax({
type : 'ajax',
url : '<?php echo base_url()?>renja/tampil_renja/',
async : false,
dataType : 'json',
success : function(data){
var html = '';
var i;
alert(data.length);
for(i=0; i<data.length; i++){
html += '<tr>'+
'<td>'+(i+1).toString()+'</td>'+
'<td>'+data[i].prog_kode+'</td>'+
'<td>'+data[i].prog_uraian+'</td>'+
'</tr>';
}
$('#datatarget').html(html);
},
error: function(){
alert('Could not load the data');
}
});
}
and this is the controller
function tampil_renja(){
echo json_encode( $this->m_data->tampil_renja()->result() );
}
JSON result in the network tab of chrome dev tools
[{"prog_id":"1","prog_kode":"6.01.0118","prog_uraian":"Program Koordinasi, pembinaan dan penyelenggaraan pemerintahan, ketentraman dan ketertiban umum, perekonomian, kesejahteraan sosial dan pembangunan"},{"prog_id":"2","prog_kode":"6.01.0118","prog_uraian":"Program Koordinasi, pembinaan dan penyelenggaraan pemerintahan, ketentraman dan ketertiban umum, perekonomian, kesejahteraan sosial dan pembangunan"}]
any help and guide would be appreciated
sorry for my bad english by the way
Please try the following code:
Changes made are:
commented datatype in the AJAX
parse response of success function as JSON.
$.ajax({
type : 'get', // edit 1
url : '<?php echo base_url()?>renja/tampil_renja/',
async : false,
//dataType : 'json', //change over here
success : function(data){
data = JSON.parse(data); //change over here
var html = '';
var i;
alert(data.length);
for(i=0; i<data.length; i++){
html += '<tr>'+
'<td>'+(i+1).toString()+'</td>'+
// '<td>'+data[i].prog_id+'</td>'+
'<td>'+data[i].prog_kode+'</td>'+
'<td>'+data[i].prog_uraian+'</td>'+
'</tr>';
}
$('#datatarget').html(html);
},
error: function(){
alert('Could not load the data');
}
})
You have to redraw the datatable after the element is updated on ajax call success :
//fungsi tampil barang
function tampil_data_renja(kowil, idpd){
// var requrl = '<?php echo base_url()?>renja/tampil_renja/'+kowil+'/'+idpd;
$.ajax({
type : 'ajax',
// url : '<?php echo base_url()?>renja/tampil_renja/'+kowil+'/'+idpd,
url : '<?php echo base_url()?>renja/tampil_renja/',
async : false,
dataType : 'json',
success : function(data){
var html = '';
var i;
// console.log(data.length);
alert(data.length);
for(i=0; i<data.length; i++){
html += '<tr>'+
'<td>'+(i+1).toString()+'</td>'+
// '<td>'+data[i].prog_id+'</td>'+
'<td>'+data[i].prog_kode+'</td>'+
'<td>'+data[i].prog_uraian+'</td>'+
'</tr>';
}
$('#datatarget').html(html);
$('#list_target').DataTable.Draw(); // redraw the datatable
},
error: function(){
alert('Could not load the data');
}
});
// return alert(kowil);
}
i want to post the data to detail controller using form method, and the it needs some of the data from the previous view.
this is my view:
<form role="form" action="<?php echo URL_CTR;?>general/do_performance/detail" method="post">
<div role="tabpanel" class="tab-pane fade in active" id="per_provinsi">
<h3 style="margin: 30px 0px 0px 0px;" class="text-center" id="title_do_1">Performance by <?php echo lang('gen.province.text');?></h3>
<table class="table mb0 table-striped" id="table_1" class="table_width">
<thead>
<tr>
<th style="width: 1%;"><?php echo lang('gen.no.text');?></th>
<th>Plant</th>
<th><?php echo lang('gen.province.text');?></th>
<th style="width: 10%;">Total DO</th>
<th style="width: 10%;">DO tidak terlambat</th>
<th style="width: 15%;">Lead Time Performance</th>
<th style="width: 5%;"><?php echo lang('gen.option.text');?></th>
</tr>
</thead>
<tbody id="data_1">
</tbody>
</table><br>
</div>
</form
and this is my script:
function load_data_1(data) {
var html = "";
for (var i = 0; i < data.length; i++) {
var a = '<tr>\
<th>' + (i + 1) + '</th>\
<td>' + data[i].gudang_nama + '<br>( ' + data[i].gudang_no_referensi + ')</td>\
<td>' + data[i].provinsi_nama + '<br>( ' + data[i].provinsi_id + ')</td>\
<td>' + data[i].total_data + '</td>\
<td>' + data[i].tepat_waktu + '</td>\
<td>' + data[i].delivery_rate + '</td>\
//this is the data i wanted to send
<input type="hidden" name="plant_id" value="'+data[i].gudang_id+'">\
<input type="hidden" name="param_id" value="'+data[i].provinsi_id+'">\
<input type="hidden" name="code" value="1">\
<td>\
<button class="btn btn-success dropdown-toggle btn-xs" type="submit" title="<?php echo lang('gen.detail.text');?>"><?php echo lang('gen.detail.text');?></button>\
</td>\
</tr>';
html = html + a;
}
$("#table_1").dataTable().fnDestroy();
$('#data_1').html(html);
$('#table_1').dataTable({
// "scrollX": true,
});
}
i want to pass the data to this controller:
public function detail() {
$data = array();
$data["plant_id"]= $this->input->post('plant_id');
$data["code"]= $this->input->post('code');
$data["param_id"]= $this->input->post('param_id');
echo json_encode($data);
}
but the result shows the last of the for data (plant_id 104 and param_id 891 are the last data of the looping):
{
plant_id: "104", //this is the last data in looping for
code: "1",
param_id: "891", //this is the last data in looping for
}
in looping 'for', How to send only the data when the same row of the button clicked?
Change the name of various fields to use array, so plant_id should be plant_id[].
Below is how the updated code will look like.
<input type="hidden" name="plant_id[]" value="'+data[i].gudang_id+'">\
<input type="hidden" name="param_id[]" value="'+data[i].provinsi_id+'">\
<input type="hidden" name="code[]" value="1">\
You can read the value in the Controller as you are doing currently. It will now be an array instead of string.
This should be a simple task I am just not fully grasping laravel yet.
I have my controllers view and models setup. I want to use my users.destroy route to delete my row in the db. But I want to do it a certain way. I want to have an alert show In my alert area on my page asking to confirm the deletion of a certain user. Im assuming I need to pass the user id in a session to an alert to confirm my delete on a delete button click.
Click 1 button to open an alert on the top of my page if I click confirm it calls user.destroy.
View:
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>View All Users</h4>
#if(session()->get('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
#endif
#if(session()->get('danger'))
<div class="alert alert-danger">
{{ session()->get('danger') }}
</div>
#endif
</div>
<div class="card-body">
<div class="text-center my-2">
New User
</div>
<div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Username</th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<th>{{$user->id}}</th>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->username}}</td>
<td class="text-center">
Show
Edit
Delete
</td>
</tr>
#endforeach
</tbody>
</table>
Controller:
public function destroy($id)
{
User::find($id)->delete();
return redirect()->route('users.index')->with('success','User Deleted');
}
Route:
Route::resource('users', 'UserController');
Always try to use DELETE method for delete resource that is best way and practice
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>View All Users</h4>
#if(session()->get('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
#endif
#if(session()->get('danger'))
<div class="alert alert-danger">
{{ session()->get('danger') }}
</div>
#endif
</div>
<div class="card-body">
<div class="text-center my-2">
New User
</div>
<div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Username</th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<th>{{$user->id}}</th>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->username}}</td>
<td class="text-center">
Show
Edit
<form method="POST" action="{{ route('users.delete', $user->id) }}">
#csrf // or hidden field
<input name="_method" type="hidden" value="DELETE">
<button type="submit" class="btn btn-xs btn-danger btn-flat show_confirm" data-toggle="tooltip" title='Delete'> <i class="fa fa-trash"> </i></button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$('.show_confirm').click(function(e) {
if(!confirm('Are you sure you want to delete this?')) {
e.preventDefault();
}
});
</script>
If you are using Laravel collective HTML then you can replace that form tag
Its Just An Example i am considering your model as User.php
If You want icon just add the font awesome css
Open Your User.php Model and paste the below code
/**
* #function tableActionButtons
* #author Manojkiran <manojkiran10031998#gmail.com>
* #param string $fullUrl
* #param integer $id
* #param string $titleValue
* #param array $buttonActions
* #usage Generates the buttons
* #version 1.0
**/
/*
NOTE:
if you want to show tooltip you need the JQUERY JS and tooltip Javascript
if you are not well in JavaScript Just Use My Function toolTipScript()
|--------------------------------------------------------------------------
| Generates the buttons
|--------------------------------------------------------------------------
|Generates the buttons while displaying the table data in laravel
|when the project is bigger and if you are laravel expert you this.
|But if you are the learner just go with basic
|
|Basically It Will generate the buttons for show edit delete record with the default
|Route::resource('foo',FooController);
|
|//requirements
|
|//bootstrap --version (4.1.3)
|// <link rel="stylesheet"href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="" crossorigin="">
|//fontawesome --version (5.6.0(all))
|//<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.0/css/all.css" integrity="" crossorigin="">
|
|if you want to show tooltip you nee the jquery and tooltip you need these js and toottipscript javascript or use my function toolTipScript
|
|//jquery
|// <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
|//popper js
|// <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
|//bootstrap js
|// <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
|
|usage
|option1:
|tableActionButtons(url()->full(),$item->id,$item->name);
|this will generate all the buttons
|
|option2:
|tableActionButtons(url()->full(),$item->id,$item->name,['edit',delete]);
|this will generate edit and delete the buttons
|
|option3:
|tableActionButtons(url()->full(),$item->id,$item->name,['edit',delete,delete],'group');
|this will generate all the buttons with button grouping
|
|option4:
|tableActionButtons(url()->full(),$item->id,$item->name,['show','edit','delete'],'dropdown');
|this will generate all the buttons with button dropdown
|
*/
public static function tableActionButtons($fullUrl, $id, $titleValue, $buttonActions = ['show', 'edit', 'delete'], $buttonOptions = '', $encryptId = false)
{
$fullUrl = strtok($fullUrl, '?');
if ($encryptId) {
$id = Crypt::encrypt($id);
}
// dd(get_class_methods(HtmlString::class));
//Value of the post Method
$postMethod = 'POST';
//if the application is laravel then csrf is used
$token = csrf_token();
//NON laravel application
// if (function_exists('csrf_token'))
// {
// $token = csrf_token();
// }elseif (!function_exists('csrf_token'))
// //else if the mcrypt id is used if the function exits
// {
// if (function_exists('mcrypt_create_iv'))
// {
// // if the mcrypt_create_iv id is used if the function exits the set the token
// $token = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
// }
// else{
// // elseopenssl_random_pseudo_bytes is used if the function exits the set the token
// $token = bin2hex(openssl_random_pseudo_bytes(32));
// }
// }
//action button Value
//(url()->full()) will pass the current browser url to the function[only aplicable in laravel]
$urlWithId = $fullUrl . '/' . $id;
//Charset UsedByFrom
$charset = 'UTF-8';
// Start Delete Button Arguments
//title for delete functions
$deleteFunctionTitle = 'Delete';
//class name for the deletebutton
$deleteButtonClass = 'btn-delete btn btn-xs btn-danger';
//Icon for the delete Button
$deleteButtonIcon = 'fa fa-trash';
//text for the delete button
$deleteButtonText = 'Delete';
//dialog Which needs to be displayes while deleting the record
$deleteConfirmationDialog = 'Are You Sure you wnat to delete ' . $titleValue;
$deleteButtonTooltopPostion = 'top';
// End Delete Button Arguments
// Start Edit Button Arguments
//title for Edit functions
$editFunctionTitle = 'Edit';
$editButtonClass = 'btn-delete btn btn-xs btn-primary';
//Icon for the Edit Button
$editButtonIcon = 'fa fa-edit';
//text for the Edit button
$editButtonText = 'Edit';
$editButtonTooltopPostion = 'top';
// End Edit Button Arguments
// Start Show Button Arguments
//title for Edit functions
$showFunctionTitle = 'Show';
$showButtonClass = 'btn-delete btn btn-xs btn-primary';
//Icon for the Show Button
$showButtonIcon = 'fa fa-eye';
//text for the Show button
$showButtonText = 'Show';
$showButtonTooltopPostion = 'top';
// End Show Button Arguments
//Start Arguments for DropDown Buttons
$dropDownButtonName = 'Actions';
//End Arguments for DropDown Buttons
$showButton = '';
$showButton .= '
<a href="' . $fullUrl . '/' . $id . '"class="' . $showButtonClass . '"data-toggle="tooltip"data-placement="' . $showButtonTooltopPostion . '"title="' . $showFunctionTitle . '-' . $titleValue . '">
<i class="' . $showButtonIcon . '"></i> ' . $showButtonText . '
</a>
';
$editButton = '';
$editButton .= '
<a href="' . $urlWithId . '/edit' . '"class="' . $editButtonClass . '"data-toggle="tooltip"data-placement="' . $editButtonTooltopPostion . '" title="' . $editFunctionTitle . '-' . $titleValue . '">
<i class="' . $editButtonIcon . '"></i> ' . $editButtonText . '
</a>
';
$deleteButton = '';
$deleteButton .= '
<form id="form-delete-row' . $id . '" method="' . $postMethod . '" action="' . $urlWithId . '" accept-charset="' . $charset . '"style="display: inline" onSubmit="return confirm("' . $deleteConfirmationDialog . '")">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="' . $token . '">
<input name="_id" type="hidden" value="' . $id . '">
<button type="submit"class="' . $deleteButtonClass . '"data-toggle="tooltip"data-placement="' . $deleteButtonTooltopPostion . '" title="' . $deleteFunctionTitle . '-' . $titleValue . '">
<i class="' . $deleteButtonIcon . '"></i>' . $deleteButtonText . '
</button>
</form>
';
// $deleteButton = "<span class='label label-success'>" ."Test" . "</span>";
$actionButtons = '';
foreach ($buttonActions as $buttonAction) {
if ($buttonAction == 'show') {
$actionButtons .= $showButton;
}
if ($buttonAction == 'edit') {
$actionButtons .= $editButton;
}
if ($buttonAction == 'delete') {
$actionButtons .= $deleteButton;
}
}
if (empty($buttonOptions)) {
return new HtmlString($actionButtons);
} elseif (!empty($buttonOptions)) {
if ($buttonOptions == 'group') {
$buttonGroup = '<div class="btn-group" role="group" aria-label="">
' . $actionButtons . '
</div>';
return new HtmlString($buttonGroup);
} elseif ($buttonOptions == 'dropdown') {
$dropDownButton =
'<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
' . $dropDownButtonName . '
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
' . $actionButtons . '
</div>
</div>
';
return new HtmlString($dropDownButton);
} else {
return 'only <code>group</code> and <code>dropdown</code> is Available ';
}
}
}
Now add this to User.php
use Illuminate\Support\HtmlString;
Now Open Your List index.blade.php and inside the for loop iteration add the below line
{{ App\User::tableActionButtons(url()->full(),$user->id,$user->name,['delete'],null,false) }}
If You want Multiple Buttons 4rt argument Accepts Array
{{ App\User::tableActionButtons(url()->full(),$user->id,$user->name,['show','edit,'delete],null,false) }}
If You face any issue kindly comment below
Hope it helps
Add below link for delete record
Delete
You can simply use an onclick function in your tag like
Delete
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>View All Users</h4>
#if(session()->get('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
#endif
#if(session()->get('danger'))
<div class="alert alert-danger">
{{ session()->get('danger') }}
</div>
#endif
</div>
<div class="card-body">
<div class="text-center my-2">
New User
</div>
<div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Username</th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<th>{{$user->id}}</th>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->username}}</td>
<td class="text-center">
Show
Edit
Delete
</td>
</tr>
#endforeach
</tbody>
</table>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).on('click','.deleteUser',function() {
var url = $(this).attr('rel');
if(confirm("Are you sure you want to delete this?")){
window.location.href = url
}
else{
return false;
}
})
</script>
or
you can use any plugin like
http://myclabs.github.io/jquery.confirm/
http://bootboxjs.com/
In controller
```php
public function deleteposts(Request $request)
{
// your delete code is here
$request->session()->flash('success', 'Post deleted sucessfully');
return redirect()->back();
}
here you setting success message
I am new to codeIgniter. want to learn this. I am facing problem. data is not populated in search page.
in model
function fetch_data($query)
{
$this->db->select('*');
$this->db->from('casedata');
if($query!='')
{
$this->db->like('status',$query);
}
$this->db->order_by('SrNo','DESC');
return $this->db->get();
}
in controller the fetch function not getting data from database
function fetch()
{
$output='';
$query='';
$this->load->model('crud_model');
if ($this->input->post('query'));
{
$query= $this->input->post('query');
}
$data=$this->Crud_model->fetch_data($query);
$output .='<div class="table-responsive">
<table class="table table_bordered table-striped
<tr>
<th>SrNo.</th>
<th>Tile</th>
<th>File No.</th>
<th>Division</th>
<th>Section</th>
<th>LDH</th>
<th>NDH</th>
<th>PDH</th>
<th>STATUS</th>
</tr>';
if($data->num_rows()>0)
{
foreach($data->result() as $row)
{
$output.='<tr> <td>'.$row->SrNo.'</td>
$output.='<tr> <td>'.$row->title.'</td>
<td>'.$row->fileno.'</td>
<td>'.$row->division.'</td>
<td>'.$row->section.'</td>
<td>'.$row->ldh.'</td>
<td>'.$row->ndh.'</td>
<td>'.$row->pdh.'</td>
<td>'.$row->status.'</td></tr>';
}
}
else{
$output.='<tr> <td colspan="5">No Data Found</td></tr> ';
}
$output.='</table>';
echo $output;
}
in view
<html>
<body>
<div class="row">
<div class="col-md-12">
<div class="panel panel-primary" data-collapsed="0">
<div class="panel-heading">
<div class="panel-title" >
<i class="entypo-plus-circled"></i>
<?php echo get_phrase('Search Data');?>
</div>
</div>
<div class="panel-body">
<?php echo form_open(base_url() . 'index.php?
admin/student/create/' , array('class' => 'form-horizontal form-groups-
bordered validate', 'enctype' => 'multipart/form-data'));?>
<div class="form-group">
<input type="text" name="search_text" id="search_text"
placeholder="Search by Customer Detsils" class="form-control"/>
<div id="result">
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function()
{
load_data();
function load_data(query)
{
$.ajax({
url:"<?php echo base_url();?>index.php?
admin/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>
Data is not populated in table. How to get output from this?
You need to loop throught the $data variable. please change the 'if' and 'foreach' as below code.
if(is_array($data) && count($data)>0)
{
foreach($data as $row)
{
still if it not working try var_dump($data) and check whether results are coming or not.
I have a partial view named SIM Balance in mine dashboard. This view should show the number of sims issued to a user date wise.
I have set up the controller
public function actionSimbalance()
{
$sql = "SELECT user.`name` AS issued_to, COUNT(`sims`.`id`) AS sims_issued, sims.`operator_name` AS operator_name,
CASE
WHEN sims.`status` = 'Production Stored SIM' THEN
CAST(`sim_issueance_transaction`.`issued_at` AS DATE)
WHEN sims.`status` = 'Testing Stored SIM' THEN
CAST(`sim_issueance_transaction`.`issued_at` AS DATE)
WHEN sims.`operator_name` = 'Zong' THEN
CAST(`sim_issueance_transaction`.`issued_at` AS DATE)
WHEN sims.`operator_name` = 'Mobilink' THEN
CAST(`sim_issueance_transaction`.`issued_at` AS DATE)
ELSE CAST(`sims`.`created_at` AS DATE) END AS issued_date
FROM `sims`
INNER JOIN `sim_issueance_transaction` ON (`sims`.`id` =
`sim_issueance_transaction`.`sim_id`)
INNER JOIN `user` ON (`sims`.`issued_to` = `user`.`id`)
WHERE sims.`status` IN ('Testing Stored SIM','Production Stored SIM')
GROUP BY user.`name`, sims.`status`, issued_date, sims.`operator_name`";
$rows = Yii::$app->db->createCommand($sql)->queryAll();
$output = [];
$grandtotal = 0;
foreach ($rows as $row) {
$std = new \stdClass();
$std->count = $row['sims_issued'];
$std->issued_to = $row['issued_to'];
$std->operator = $row['operator_name'];
$std->issued_date = $row['issued_date'];
$grandtotal += $std->count;
$output[]= $std;
}
return $this->renderPartial('sim_progress', ['model' => $output, 'grandtotal' => $grandtotal]);
}
The partial view sim_progress is below
<?php foreach ($model as $row){?>
<tr>
<td><?=$row->issued_to?></td>
<td><?=$row->count?></td>
<td><?=$row->operator?></td>
<td><?= $row->issued_date ?></td>
</tr>
<?php } ?>
<tr>
<td><strong>Grand Total</strong></td>
<td>
<strong><?= $grandtotal ?></strong>
</td>
<td></td>
</tr>
Then there is an HTML sim-balance I have designed
<div class="box box-info">
<div id="af8a8d88334">
<div class="print-header print-only">
<center><img style="width: 100px" src="<?=
\yii\helpers\Url::to('#web/images/logo.png', true); ?>"/>
</center>
<br/>
<hr/>
</div>
<div class="box-header with-border">
<h3 class="box-title">SIM Balance</h3>
</div>
<div class="box-body">
<div class="table-responsive">
<table class="table no-margin">
<thead>
<tr>
<th>Issued To</th>
<th>Sims Issued</th>
<th>Operator Name</th>
<th>Issued At</th>
</tr>
</thead>
<tbody id="dashboard-sim-balance">
</tbody>
</table>
</div>
</div>
</div>
<div class="box-footer clearfix">
<a href="javascript:void(0)" onclick="$('#af8a8d88334').printThis();"
class="btn btn-sm btn-default btn-flat pull-right">Print</a>
</div>
Then in my main site index view, I am calling it like below
.
.
.
<?php if (!Yii::$app->user->isGuest && in_array(Yii::$app->user->identity->user_role,[1,6])) {
echo $this->render('dashboard/sim-balance');
} ?>
.
.
.
.
$url_sim_balance = Url::toRoute('/dashboard/simbalance');
.
.
.
.
In my JS I am creating an ajax function which should show the details.
$script = <<< JS
$(document).ready(function () {
.
.
.
.
loadSimBalance();
.
.
.
});
function loadSimBalance() {
$('#dashboard-sim-balance').html('');
$.ajax({
url: '$url_sim_balance',
data: data.val(), // also tried $('#dashboard-sim-balance').val()
success:function(data) {
$('#dashboard-sim-balance').html(data);
},
error: function() {
}
});
}
JS;
$this->registerJs($script);
But when I run my project I cannot see the details but an empty table like below
How can I set my ajax call to view the details.
Any help would be highly appreciated.
change
function loadSimBalance() {
$('#dashboard-sim-balance').html('');
$.ajax({
url: '$url_sim_balance',
data: data.val(), // also tried $('#dashboard-sim-balance').val()
success:function(data) {
$('#dashboard-sim-balance').html(data);
},
error: function() {
}
});
}
To
function loadSimBalance() {
$('#dashboard-sim-balance').html('');
$.ajax({
url: '$url_sim_balance',
success:function(data) {
$('#dashboard-sim-balance').html(data);
},
error: function() {
}
});
}