I am using Jquery DataTables and jEditable. I have a correct JSON response as follows:
[{"country_id":"18","country":"Aruba","country_enabled":"1"},{"country_id":"19","country":"Afghanistan","country_enabled":null},{"country_id":"22","country":"Angola","country_enabled":"1"},{"country_id":"23","country":"Anguilla","country_enabled":null},{"country_id":"24","country":"\u00c5land Islands","country_enabled":null},{"country_id":"25","country":"Albania","country_enabled":null},{"country_id":"26","country":"Andorra","country_enabled":null},{"country_id":"27","country":"United Arab Emirates","country_enabled":null},{"country_id":"29","country":"Argentina","country_enabled":null},{"country_id":"30","country":"Armenia","country_enabled":null},{"country_id":"31","country":"American Samoa","country_enabled":null},{"country_id":"32","country":"Antarctica","country_enabled":null},{"country_id":"33","country":"French Southern Territories","country_enabled":null},{"country_id":"34","country":"Antigua and Barbuda","country_enabled":null},{"country_id":"35","country":"Australia","country_enabled":null},{"country_id":"36","country":"Austria","country_enabled":null},{"country_id":"37","country":"Azerbaijan","country_enabled":null},{"country_id":"38","country":"Burundi","country_enabled":null},{"country_id":"39","country":"Belgium","country_enabled":null},{"country_id":"40","country":"Benin","country_enabled":null},{"country_id":"41","country":"Bonaire, Sint Eustatius and Saba","country_enabled":null},{"country_id":"42","country":"Burkina Faso","country_enabled":null},{"country_id":"43","country":"Bangladesh","country_enabled":null},{"country_id":"44","country":"Bulgaria","country_enabled":null},{"country_id":"45","country":"zoo","country_enabled":null},{"country_id":"46","country":"Xylaphone","country_enabled":null}]
The above is taken from Chrome's developer tools and from the XHR window and, therefore, I know the response looks correct and the data is being received.
Here is my HTML to display the data:
<div class="content">
<div id="pad-wrapper" class="form-page">
<div class="row">
<div class="col-md-12">
<h2>List of Countries</h2>
</div>
<div class="bs-example">
</br>
<form>
<div align = "left">
<button type="button" class="btn btn-success" onclick="window.location='<?php echo site_url("admin/country_add");?>'">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Country
</button>
<button type="button" class="btn btn-danger" onclick="window.location='<?php echo site_url("admin/country_delete");?>'">
<span class="glyphicon glyphicon-minus" aria-hidden="true"></span> Delete Countries
</button>
</div>
<!-- start table listing -->
<table id="myDataTable">
<thead>
<tr>
<th>country_id</th>
<th>country</th>
<th>country_enabled</th>
</thead>
<tbody>
</tbody>
</table>
<button id="btnAddNewRow">Add</button>
<button id="btnDeleteRow">Delete</button>
</div>
</div>
</div>
The datatable appears in the view but just says, "loading..." and no data is ever displayed.
I have renamed the column headers the same as my database but it still does not display the data.
There is an error in the console as follows:
In the client-side processing mode data provided via Ajax should have the following structure, see ajax option for more details.
{
"data": [
// row 1 data source,
// row 2 data source,
// etc
]
}
The solution is to correct your JSON data to look like:
{
"data": [
{"country_id":"18","country":"Aruba","country_enabled":"1"}
]
}
Alternative solution is to set ajax.dataSrc to empty string to indicate that you're returning plain array, see the sample code below:
$('#example').dataTable({
"ajax": {
"url": "data.json",
"dataSrc": ""
}
});
Related
I have a table that is populate using AJAX. Each table row is give an id based on a record id in my database. Under the table are 3 buttons: NEW, EDIT and DELETE.
When I select a row I set the data-id of the DELETE button to the clicked row's ID. When I click DELETE my AJAX call proceeds to delete all records from my database that have the data-id of the DELETE button.
This all works perfectly. After everything is done i get an alert the the data was deleted. Which is great.
My Question is: in the If (data ==1) section of my AJAX call, how do i remove the row, fade out, from the table without refreshing the page like i have to do now.
Here is my index.html code
<div class="container-fluid inputwrapper">
<table id="workRequests" style="width: 100%;">
//table is filled by ajax call to db below is an example of the format
<tr class="table__row" id="124108" onclick="highlight_row()">
<td class="table__content" data-heading="Number">124108</td>
....
</tr>
</table>
</div>
<div class="container-fluid">
<div class="row center-block">
<div class="text-center col-sm-2 col-sm-offset-0" style="float: left;"> </div>
<div class="text-center col-sm-8" style="float: left;">
<button style="margin-right: 10px;" type="button" class="btn btn-default" id="wopnew" onClick="createNew()">New</button>
<button style="margin-right: 10px;" type="button" class="btn btn-default" disabled id="wopedit" onClick="editFile()">Edit</button>
<button type="button" class="btn btn-default" data-id="0" id="wopdelete">Delete</button>
</div>
<div class="text-center col-sm-2 col-sm-offset-0" style="float: left;"> </div>
</div>
Here is my AJAX call.
I am a novice at coding and from what I can see as i Step thought the function in my browser - the var element is set to the DELETE button data-id so when we get to the success part and data == 1 nothing happens as it is focused on the DELETE button not the Table Row.
I have tried to change element to the id of my table but that does not work either.
<script>
$(document).on("click","#wopdelete",function(){
if (confirm("Are you sure you want to delete this Record? It can NOT be recoverd!!")) {
var id = $(this).data('id');
var element = this;
$.ajax({
url :"./scripts/delRecord.php",
type:"POST",
cache:false,
data:{deleteId:id},
success:function(data){
if (data == 1) {
$(element).closest('tr').fadeOut(400, function(){
$(this).remove();
});
alert("User record deleted successfully");
}else{
alert("Record did not delete properly!");
}
}
});
}
});
</script>
I have a list (displayed in a tabulated format). The list consists of names of various departments.
The same appears as below:
-------------------------------------------------------
Sr. No. Department Name Action
-------------------------------------------------------
1 Department A EDIT | DELETE
2 Department B EDIT | DELETE
3 Department C EDIT | DELETE
4 Department D EDIT | DELETE
5 Department E EDIT | DELETE
-------------------------------------------------------
Now I am trying to open a single modal when "EDIT" or "DELETE" is clicked. In the modal I want to display the EDIT Form (for the particular Department) or DELETE Form (for the particular Department).
Please Note: I am using CouchCMS so even a custom PHP code can be helpful which can be changed to fit the CouchCMS Purview.
I have generate a clonable template with custom routes using CouchCMS. The tag helps me generate the table (as shown above). Where in the buttons for EDIT and DELETE are also generated. I can easily provide to the button the page id using the of CouchCMS (if that helps).
I am using the native code of bootstrap to popup the Bootstrap v3 modal. Wherein, a single modal opens for every button that is clicked, hence saving me the memory issue that i might run into if I generate as many modals as the rows in the table.
The code for Bootstrap v3 modal that i am referring to is available here.
In CouchCMS we have a edit form (for editing the data queried from the database) where the page_id parameter needs to be set in order to edit that particular data item. I need to set this page_id. But am unable to figure out how to?
Code to generate the Table list:
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Sr. No.</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<cms:pages masterpage='department/department.php' limit='10' paginate='1' show_future_entries='1'>
<tr>
<td><cms:show absolute_count /></td>
<td><cms:show k_page_title /></td>
<td>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-id="<cms:show k_page_id />">
EDIT
</button>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal" data-id="<cms:show k_page_id />">
DELETE
</button>
</td>
</tr>
</cms:pages>
</tbody>
</table>
Code to open a single modal for every button that is clicked, EDIT or DELETE button:
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var page = button.data('id') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
var modal = $(this)
modal.find('.modal-title').text('New message to ' + page)
modal.find('.modal-body input').val(page)
})
Code for the modal is:
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">New message</h4>
</div>
<div class="modal-body">
<!-- Edit Form Content Here -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">CANCEL</button>
<button type="button" class="btn btn-primary">SAVE</button>
</div>
</div>
</div>
</div>
An edit form in CouchCMS is defined as:
In this form I need to have the page_id=k_page_id set. The k_page_id is available to the button in the table under the data-* (data-id) attribute. If only this data-id could be supplied to the page_id, the rest can be taken forward from there using CouchCMS.
I am also open to the option of using AJAX, if required. But if it is possible without AJAX, it would still be fine.
Any help in this regards will be greatly appreciated. If any clarification or post editing is required or any more inputs are required, I shall do show if someone points out the short coming in my description/code.
Thanks to the community, in advance.
how to get data from table and pass it in controller in json format and get those data in view using angularjs
I want to get data from json encoded variable from controller and show it to view page using angularjs
view page using angularjs
<div class="card" ng-app = "myApp">
<table class="table table-hover" ng-controller = "menuController">
<thead>
<tr>
<th>Name</th>
<th>Parent menu</th>
<th>Order</th>
<th>Status</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
<tr ng-repeat = "x in values">
<td>{{x.name}}</td>
<td>{{x.parent_name}}</td>
<td>{{x.orders}}</td>
<td>{{x.status}}</td>
<td class="text-right">
<button type="button" class="btn btn-icon-toggle"
data-toggle="tooltip" data-placement="top"
data-original-title="Edit row">
<i class="fa fa-pencil"></i>
</button>
<button type="button" class="btn btn-icon-toggle"
data-toggle="tooltip" data-placement="top"
data-original-title="Copy row">
<i class="fa fa-copy"></i>
</button>
<button type="button" class="btn btn-icon-toggle"
data-toggle="tooltip" data-placement="top"
data-original-title="Delete row">
<i class="fa fa-trash-o"></i>
</button>
</td>
</tr>
</tbody>
</table>
</div>
var app = angular.module('myApp');
app.controller('menuController',function ($scope,$http) {
$scope.values = ["Milk", "Bread", "Cheese"];
});
PHP controller code
public function getmenu(){
$records=Menu::all();
return json_encode($records);
}
route code
Route::group(['prefix'=>'admin'],function(){
Route::get('/form','HomeController#form');
});
ErrorException (E_ERROR)
Use of undefined constant x - assumed 'x' (this will throw an Error in a future version of PHP) (View:form.blade.php)
I think it might be because you are serving your view which contains the angular code with Laravel, and in the file form.blade.php you use the angular syntax, which is the same as the blade one.
To solve that, you can try to remove blade word from the view filename, so it would become form.php or (alternative way instead of modifying the filename) each time you have to print something with the JavaScript framework instead of blade, use: #{{ variableToPrint }}.
So for example, part of your loop would become:
<tr ng-repeat="x in values">
<td>#{{x.name}}</td>
<td>#{{x.parent_name}}</td>
<td>#{x.orders}}</td>
<td>#{{x.status}}</td>
<!-- ... -->
You get the error as blade uses the same syntax to evaluate and print values, so if you write: {{ x.name }} blade finds a string literal x which gets interpreted as a constant.
If you prefix the # sign, blade will recognize that as an instruction not to be parsed, but that needs to be left as it is, it would just remove the #, leaving you with the correct code for you JavaScript loop
I have a problem in getting the POST data from a page using ajax. In the jquery code the data is running smoothly and it will display when i alert the data. In the ajax request code the data from jquery has been successfully pass into showpercent.php file. Now the problem about showpercent.php, the data POST index percentage_id is unidentified. How can i fix this problem in getting the value of POST?
Below is the table list with button when the data is coming from.
<table>
<tr>
<td>
<button class="btn btn-info show-percentage" title="Click to add view percentages!" data-percentage_id="'.$row['hidden_id'] .'" data-toggle="show-percentage"><i class="glyphicon glyphicon-time"></i></button>
</td>
</tr>
</table>
Below is the ajax request sending the data into showpercent.php file. When I alert the percentage_id from button click the data will show in the alert and the ajax was successfully pass into specific php file which is showpercent.php.
<script>
$(document).ready(function(){
$(".show-percentage").click(function(){
var percentage_id = $(this).data('percentage_id');
alert("Ajax Landing ID "+landing_id);
$.ajax({
url: 'ajax/showpercent.php',
type: 'POST',
cache: false,
data: { percentage_id : percentage_id },
success: function(data) {
alert(data);
$('#add-percentage').modal('show');
readRecords();
},
error: function(request, status, error){
alert("Error!! "+error);
}
});
// READ recods when the button is click
readRecords();
});
function readRecords() {
$.get("ajax/showpercent.php", {}, function (data, status) {
$(".display_percentage").html(data);
});
}
});
</script>
Below is the modal having a tab will display the data from ajax request. The class display_percentage will display the current data from showpercentage.
<div id="add-percentage" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Fish Landing Percentage</h4>
</div>
<div class="modal-body">
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#menu1">Add Percentage</a></li>
</ul>
<div class="tab-content">
<div id="menu1" class="tab-pane fade in active">
<br>
<div class="display_percentage"></div>
</div>
</div>
<div class="modal-footer">
<button type="button" id="primary" class="btn btn-primary" onclick="AddPercentage()"> Add Percentage </button>
<button type="button" id="danger" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
showpercent.php
This file will read by readRecords() function to display into the modal with tab class display_percentage when the button click is triggered.
This is now the problem comes, when the data was successfully pass from ajax request the data POST in the showpercent.php is not properly set and cannot proceed to the mysql process because the POST is not set.
<?php
include 'db.php';
$data = '
<table">
<thead>
<tr class="success">
<th ><center>No.</center></th>
<th ><center>Percentage ID</center></th>
<th ><center>Landing ID</center></th>
<th><center>Percentage</center></th>
<th><center>Date Added</center></th>
</tr>
</thead>';
if(isset($_POST['percentage_id'])){
$landing_id = $_POST['percentage_id'];
$query = mysqli_query($conn, "SELECT
percentage.percent_id,
percentage.landing_id,
percentage.percentage,
percentage.date_recorded
FROM
percentage
WHERE percentage.landing_id = '$landing_id'");
$number = 1;
while($row = mysqli_fetch_array($query)) {
$data .= '
<tr>
<td><center>' . $number . '</center></td>
<td><center>' . $row['percent_id'] . '</center></td>
<td><center>' . $row['landing_id'] . '</center></td>
<td><center>' . $row['percentage'] . '%</center></td>
<td><center>' . date("M. d, Y", strtotime($row['date_recorded'])) . '</center></td>
</tr>';
$number++;
}
}else{
echo 'Percentage id is not set!';
}
$data .= '
</table>';
echo $data;
?>
But in the console the ajax passing data will run smoothly.
I wish anybody will help me to fix this problem.
I read your code your showpercent.php, javascript and html and soo far and notice this
<button class="btn btn-info show-percentage" title="Click to add view percentages!" data-percentage_id="'.$row['hidden_id'] .'" data-toggle="show-percentage"><i class="glyphicon glyphicon-time"></i></button>
and check this
data-percentage_id="'.$row['hidden_id'] .'"
you're doing it wrong this is not how you put php value into html value, this will return undefined in javascript if you try to get its value so
This is why
var percentage_id = $(this).data('percentage_id');
returns undefined in javascript and php
So replace it with this
data-percentage_id="<?php echo $row['hidden_id']; ?>"
So replace your button into like this
<button class="btn btn-info show-percentage" title="Click to add view percentages!" data-percentage_id="<? echo $row['hidden_id']; ?>" data-toggle="show-percentage"><i class="glyphicon glyphicon-time"></i></button>
thanks to #Keval Mangukiya
try:
add this in your html
<button id="percentage_id" class="btn btn-info show-percentage" title="Click to add view percentages!" data-percentage_id="<?=$row['hidden_id'] ?>" data-toggle="show-percentage"><i class="glyphicon glyphicon-time"></i></button>
set the value of your percentage_id before getting it
add to your JavaScript with
$("#percentage_id]").data('percentage_id',loading_id); //setter
var percentage_id = $(this).data('percentage_id'); //getter
I am fetching the details of a user according to Id on button click in a popup in laravel but It is showing some error.
This is viewuser.blade.php
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<table class="table table-bordered">
<tr>
<td>{{$usersview->id}}</td>
<td>{{$usersview->firstname}}</td>
<td>{{$usersview->filename}}</td>
</tr>
</table>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
This is viewparticipant.blade.php in which I have a button View. When the user will click on this button a popup will open and show the details according to Id.
<td>
View
</td>
My Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
use App\Imageupload;
class ParticipantController extends Controller
{
public function show($id)
{
$usersview = Imageupload::find($id);
return View::make('viewuser', compact('usersview'));
}
}
My Web.php
Route::get('/participant/show/{id}','ParticipantController#show');
The error is showing that unknown variables in modal.
well you only have $usersview in your blade file and you're calling $row->id in viewparticipant.blade.php , which should be the $usersview->id instead
after Edit -->
so you need to make an ajax call to the function you want to get the results. the ajax must contain the id of the user so you should get the id some how. I usually define an on click function and get the id. then on the same click trigger an ajax call, something like this:
$('#user').on('click', function (event) {
$.ajax({
url: '/users?user_id=' + $(this).val(),
success: function (data) {
// parse the data
},
fail: function () {
// do something in case of failure
}
});
});
$(this).val(); is the id here, I assumed you would store that in an input
Try this:
<td>
View
</td>
Because laravel is figuring out where you want to place your $row->id variable, and to my guessing it's not passing the variable correctly in your route.
try
#foreach($usersview as $usersview)
<tr>
<td>{{$usersview->id}}</td>
<td>{{$usersview->firstname}}</td>
<td>{{$usersview->filename}}</td>
</tr>
#endforeach