Add data from a datatable to a modal in laravel - php

The spanish version failed me, so I'll test my luck here
I have the following code and I would like to know how I can pass the data from my table to the modal
This is my code in edit.blade.php. The modal does work but I don't know how to make the data show up in the modal, I'll do POST to the database myself but I just want to know how I make that data show up so I can edit it.
The data that I want to appear in the modal are the id, the name and the email.
This is my code in editar.blade.php
<script src="{{ url('/js/vendor/jquery-1.12.4.min.js') }} "></script>
<script src="{{ url('/js/bootstrap.min.js') }} "></script>
<link rel="stylesheet" href="{{ url('/css/bootstrap.min.css') }} ">
<script src="{{ url('/js/jquery.dataTables.min.js') }} "></script>
<div class="container">
<h2>Editar estudiante</h2>
<div class="container">
<h2>Laravel DataTables Tutorial Example</h2>
<table id="user_table" class="table table-bordered table-striped">
<thead>
<tr>
<th width="35%" id="name">Name</th>
<th width="35%" id="email">Email</th>
<th width="30%">Action</th>
</tr>
</thead>
</table>
</div>
<div class="modal fade" id="edit-modal" tabindex="-1" role="dialog" aria-labelledby="edit-modal-label" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="edit-modal-label">Edit Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="attachment-body-content">
<form id="edit-form" class="form-horizontal" method="POST">
<div class="card text-white bg-dark mb-0">
<div class="card-header">
<h2 class="m-0">Edit</h2>
</div>
<div class="card-body">
<!-- id -->
<input type="hidden" name="modal-input-id" class="form-control" id="modal-input-id" required>
<!-- /id -->
<!-- name -->
<div class="form-group">
<label class="col-form-label" for="modal-input-name">Name</label>
<input type="text" name="modal-input-name" class="form-control" id="modal-input-name" required autofocus>
</div>
<!-- /name -->
<!-- description -->
<div class="form-group">
<label class="col-form-label" for="modal-input-description">Email</label>
<input type="text" name="modal-email" class="form-control" id="modal-input-email" required>
</div>
<!-- /description -->
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Done</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#user_table').DataTable({
processing: true,
serverSide: true,
ajax: {
url: "{{ route('getdata') }}",
},
columns: [
{
data: 'name',
name: 'name',
className: 'user-name',
},
{
data: 'email',
name: 'email',
className: 'user-email',
},
{
data: 'action',
name: 'action',
orderable: false
}
]
});
$(document).on('click', "#edit-item", function() {
//obtenemos la instancia de la fila
$tr = $(this).closest('tr');
//rellenamos los campos en el modal
$('#modal-input-name').val($('user-name', $tr).text());
$('#modal-input-email').val($('user-email', $tr).text());
// Mostramos el modal
$('#edit-modal').modal('show');
})
// on modal hide
$('#edit-modal').on('hide.bs.modal', function() {
$('.edit-item-trigger-clicked').removeClass('edit-item-trigger-clicked')
$('#edit-form').trigger('reset');
})
});
</script>
This is my get route in web.php
Route::get('/editar', 'AdminController#editar')->name('editar');
This is my function to bring data to the Datatable in AdminController.php
public function getPosts(Request $request)
{
if($request->ajax()){
$data = StudentModel::latest()->get();
return DataTables::of($data)
->addColumn('action', function($data){
$button = '<button id="edit-item" type="button" name="edit-item" data-target-id="'.$data->id.'" class="edit btn btn-primary btn-sm" data-toggle="modal" data-target="edit-modal">Edit</button>';
$button .= ' <button type="button" name="edit" data-target-id="'.$data->id.'" class="delete btn btn-danger btn-sm">Delete</button>';
return $button;
})
->rawColumns(['action'])
->make(true);
}
return view('editar');
}

Can you try this?
$('#modal-input-name').val($tr.find('td.user-name').text());
$('#modal-input-email').val($tr.find('td.user-email').text());
And also I would suggest that you use a css class for targeting a click event, as Id is normally for a unique identifier, and by doing your way you are having many table rows with the same Id which defeats the original idea.

Related

Closing the modal after sending the data to the database

I don't use pure vue in the app. I want to close the modal after sending the data to the database. I cannot close it for anything in the world by examining the divergence of the Internet. Can you help? :)
<div class="container-fluid">
<!-- DIRECT CHAT -->
<div class="card" id="form">
<!-- /.card-header -->
<div class="card-body">
<div class="row">
<!-- Button trigger modal -->
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#exampleModal">
Dodaj budynek
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Dodawanie budynku</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
#csrf
#{{ info.data }}
<div class="modal-body">
<div class="form-group">
<label>Nazwa budynku <span class="red">*</span></label>
<input type="text" class="form-control" name="name" id="name" v-model="name">
<small>Nazwa identyfikująca budynek, np. ulica i numer budynku lub nazwa własna</small>
<small><div v-if="feedback">
<span style="color:red" v-text="feedback.name[0]" ></span>
</div></small>
</div>
<div class="form-group">
<label>Konto rozliczeniowe</label>
<input type="text" class="form-control" name="bank_account" id="bank_account" v-model="bank_account">
<small>Numer konta bankowego</small>
</div>
<div class="form-group">
<label>Opis</label>
<textarea class="form-control" rows="3" name="description" id="description" v-model="description"></textarea>
<small>Dodatkowe informacje, adnotacje, opis</small>
</div>
<small><span class="red">* pola obowiązkowe</span></small>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Anuluj</button>
<button type="submit" class="btn btn-success" #click="add">Zapisz</button>
</div>
</div>
</div>
</div>
</div>
<div class="row pt-2">
<table class="table table-striped">
<thead>
<tr>
<th scope="col" width="5%">#</th>
<th scope="col" width="25%">Nazwa <i class="fas fa-info-circle fa-sm" rel="tooltip" title="Nazwa pod którą identyfikowana jest zarządzana nieruchomość"></i></th>
<th scope="col" width="30%">Konto księgowe <i class="fas fa-info-circle fa-sm" rel="tooltip" title="Numer konta bankowego"></i></th>
<th scope="col" width="30%">Opis <i class="fas fa-info-circle fa-sm" rel="tooltip" title="Opis identyfikujący lub uzupełniający nazwę zarządzanej nieruchomości"></i></th>
<th width="5%"></th>
<th width="5%"></th>
</tr>
</thead>
<tbody>
<tr v-for="(list, index) in lists" :key="list.id">
<th scope="row">#{{ index + 1 }}</th>
<td>#{{ list.name }}</td>
<td>#{{ list.bank_account }}</td>
<td>#{{ list.description }}</td>
<td><button type="button" class="btn btn-info btn-sm"><i class="fas fa-pencil-alt"></i></button></td>
<td><button type="button" class="btn btn-danger btn-sm" #click="deleteData(list.id)"><i class="far fa-trash-alt"></i></button></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
I present script vue below. After running the add method, I would like to close the modal.
<script>
const app = new Vue({
el: '#form',
data () {
return {
feedback: '',
name: '',
bank_account: '',
description: '',
info: '',
lists: [],
}
},
mounted: function(){
this.show();
},
methods: {
show:function(list){
axios
.get('buildings/show', {
})
.then(({ data }) => (this.lists = data));
},
add () {
axios
.post('buildings/store', {
name: this.name,
bank_account: this.bank_account,
description: this.description,
})
.then(response => {
this.show();
this.name = '';
this.bank_account = '';
this.description = '';
this.info = response;
this.myModal.hide();
})
.catch(error => {
this.feedback = error.response.data.errors;
this.info = '';
})
},
deleteData:function(id){
axios
.post('buildings/destroy', {
id: id
})
.then(response => {
this.show();
})
.catch(error => {})
},
},
})
</script>
I wonder how to edit the code so that after calling the add method, the modal will close. I don't want to write in pure vue.js because I haven't grasped this topic yet.
I'm assuming you're using bootstrap modals here?
In your add() method you have this: this.myModal.hide(); but myModal is not defined anywhere.
In your modal html you can add a ref:
<div class="modal fade" ref="myModal" id="exampleModal">
Then you can use this to close the modal:
this.$refs.myModal.hide();
You can take little help of jquery if you already have it.
let currentOpenModel= jQuery('.modal.in').attr('id');
// modalID or undefined
jQuery('#' + currentOpenModel)?.modal('hide');

Laravel + Vue.js: Bootstrap modal not showing when called using vue js model

I have this template
<template>
<div class="main">
<div class="container-fluid" style="padding-top: 2.5%">
<h4 class="page-title"><i class="lnr lnr-funnel"> </i> Infusionsoft Accounts
<button #click="addInfusionsoft" class="btn btn-xs btn-success" ><i class="lnr lnr-plus-circle"> </i> New </button>
</h4>
<div class="row">
<div class="col-md-12">
<table class="table table-responsive table-bordered table-striped">
<tr>
<th>ID</th>
<th>App Name</th>
<th>Auth Key</th>
<th>Status</th>
<th>Created</th>
<th>Updated</th>
<th width="300px">Action</th>
</tr>
</table>
</div>
</div>
<div class="modal fade" id="modalAddAccount" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<form method="POST" action="" id="addInfsAccount"/>
<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="myModalLabel">Infusionsoft Account</h4>
</div>
<div class="modal-body">
<p class="small">Fill up the form to add a new Infusionsoft Account.</p>
<br/>
<div class="form-group row">
<div class="col-md-8 col-md-offset-2">
<input id="appName" type="text" required placeholder="App Name e.g l328" class="form-control" name="appName">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add Account</button>
</div>
</div>
</form>
</div>
</div>
<app-legend/>
</div>
</div>
</template>
<script>
import Legend from "./Legend";
import NavBar from "../layouts/NavBar";
import SideBar from "../layouts/SideBar";
export default {
data() {
return {
infsModal: false,
}
},
created() {
},
components: {
'app-legend': Legend,
'nav-bar': NavBar,
'side-bar': SideBar,
},
methods: {
addInfusionsoft() {
$('#modalAddAccount').modal('show');
}
},
}
</script>
I'm using this template for the project https://www.themeineed.com/downloads/klorofil-free-bootstrap-admin-template/, I kept everything as default and didn't have any major modifications with the bootstrap classes. If I use jquery function to call the modal it will show correctly, however moving the modal to another template and calling is by using v-model and v-if, nothing really happens. Please see sample below
parent template
<template>
<div class="main">
<div class="container-fluid" style="padding-top: 2.5%">
<h4 class="page-title"><i class="lnr lnr-funnel"> </i> Infusionsoft Accounts
<button #click="infsModal = true" class="btn btn-xs btn-success" ><i class="lnr lnr-plus-circle"> </i> New </button>
</h4>
<div class="row">
<div class="col-md-12">
<table class="table table-responsive table-bordered table-striped">
<tr>
<th>ID</th>
<th>App Name</th>
<th>Auth Key</th>
<th>Status</th>
<th>Created</th>
<th>Updated</th>
<th width="300px">Action</th>
</tr>
</table>
</div>
</div>
<div v-if="infsModal">
<div class="modal fade" id="modalAddAccount" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<form method="POST" action="" id="addInfsAccount"/>
<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="myModalLabel">Infusionsoft Account</h4>
</div>
<div class="modal-body">
<p class="small">Fill up the form to add a new Infusionsoft Account.</p>
<br/>
<div class="form-group row">
<div class="col-md-8 col-md-offset-2">
<input id="appName" type="text" required placeholder="App Name e.g l328" class="form-control" name="appName">
<span v-if="false" span class="invalid-feedback">
<strong>Error</strong>
</span>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add Account</button>
</div>
</div>
</form>
</div>
</div>
</div>
<app-legend/>
</div>
</div>
</template>
<script>
import Legend from "./Legend";
import NavBar from "../layouts/NavBar";
import SideBar from "../layouts/SideBar";
import InfusionsoftAdd from "../infusionsoft/InfusionsoftAdd";
export default {
data() {
return {
infsModal: false,
}
},
created() {
},
components: {
'app-legend': Legend,
'nav-bar': NavBar,
'side-bar': SideBar,
'infs-modal': InfusionsoftAdd,
},
methods: {
addInfusionsoft() {
//this.$router.push('/infusionsoft')
}
},
}
</script>
this is the modal:
<template>
<div v-if="dialog">
<transition name="modal">
<div class="modal fade" id="modalAddAccount" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<form method="POST" action="" id="addInfsAccount"/>
<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="myModalLabel">Infusionsoft Account</h4>
</div>
<div class="modal-body">
<p class="small">Fill up the form to add a new Infusionsoft Account.</p>
<br/>
<div class="form-group row">
<div class="col-md-8 col-md-offset-2">
<input id="appName" type="text" required placeholder="App Name e.g l328" class="form-control" name="appName">
<span v-if="false" span class="invalid-feedback">
<strong>Error</strong>
</span>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add Account</button>
</div>
</div>
</form>
</div>
</div>
</transition>
</div>
</template>
<script>
export default {
data() {
return {
dialog: true
}
},
methods: {
back() {
//this.$router.back();
}
}
}
</script>
Is there anything I'm doing wrong?
When you set infsModal to true, all that's happening is that you're including the modal code but you're still not displaying the modal. You can still use jQuery here if you'd like, no reason not to. You can remove the infsModal variable since it's not needed (the modal won't display even if that's set to true... you still need to tell Bootstrap to actually show the modal). If you want to get away from jQuery, a solid option is Bootstrap-Vue https://bootstrap-vue.org/docs/components/modal ... let me know if that helps!

PHP AJAX CRUD Issue While Fetching Data

So I was developing some crud application with CodeIgniter Framework. But I am facing issues while retrieving data from the database. I am getting a 404 Not Found error for the AJAX function. The function is where it should be but I can't seem to find why it's giving me an error.
Please find the following code for the files and let me know if I am doing anything wrong here.
package_view.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Package List</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/css/dataTables.bootstrap4.css">
</head>
<body>
<div class="container">
<!-- Page Heading -->
<div class="row">
<div class="col-12">
<div class="col-md-12">
<h1>Package
<small>List</small>
<div class="float-right"><span class="fa fa-plus"></span> Add New</div>
</h1>
</div>
<table class="table table-striped" id="mydata">
<thead>
<tr>
<th>Package ID</th>
<th>Test Quantity</th>
<th>Price</th>
<th style="text-align: right;">Actions</th>
</tr>
</thead>
<tbody id="show_data">
</tbody>
</table>
</div>
</div>
</div>
<!-- MODAL ADD -->
<form>
<div class="modal fade" id="Modal_Add" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add New Package</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label class="col-md-2 col-form-label">Test Quantity</label>
<div class="col-md-10">
<input type="text" name="test_quantity" id="test_quantity" class="form-control" placeholder="Test Quantity">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Price</label>
<div class="col-md-10">
<input type="text" name="price" id="price" class="form-control" placeholder="Price">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" type="submit" id="btn_save" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
</form>
<!--END MODAL ADD-->
<!-- MODAL EDIT -->
<form>
<div class="modal fade" id="Modal_Edit" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit Package</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label class="col-md-2 col-form-label">Package ID</label>
<div class="col-md-10">
<input type="text" name="pkg_id_edit" id="pkg_id_edit" class="form-control" placeholder="Package ID" readonly>
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Test Quantity</label>
<div class="col-md-10">
<input type="text" name="test_quantity_edit" id="test_quantity_edit" class="form-control" placeholder="Test Quantity">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Price</label>
<div class="col-md-10">
<input type="text" name="price_edit" id="price_edit" class="form-control" placeholder="Price">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" type="submit" id="btn_update" class="btn btn-primary">Update</button>
</div>
</div>
</div>
</div>
</form>
<!--END MODAL EDIT-->
<!--MODAL DELETE-->
<form>
<div class="modal fade" id="Modal_Delete" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Package</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<strong>Are you sure to delete this record?</strong>
</div>
<div class="modal-footer">
<input type="hidden" name="pkg_id_delete" id="pkg_id_delete" class="form-control">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
<button type="button" type="submit" id="btn_delete" class="btn btn-primary">Yes</button>
</div>
</div>
</div>
</div>
</form>
<!--END MODAL DELETE-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/dataTables.bootstrap4.js"></script>
<script type="text/javascript">
$(document).ready(function(){
show_package(); //call function show all package
$('#mydata').dataTable();
//function show all package
function show_package(){
$.ajax({
type : 'ajax',
url : '<?php echo site_url('Package/package_data')?>',
async : true,
dataType : 'json',
success : function(data){
var html = '';
var i;
for(i=0; i<data.length; i++){
html += '<tr>'+
'<td>'+data[i].pkg_id+'</td>'+
'<td>'+data[i].test_quantity+'</td>'+
'<td>'+data[i].price+'</td>'+
'<td style="text-align:right;">'+
'Edit'+' '+
'Delete'+
'</td>'+
'</tr>';
}
$('#show_data').html(html);
}
});
}
//Save package
$('#btn_save').on('click',function(){
var pkg_id = $('#pkg_id').val();
var test_quantity = $('#test_quantity').val();
var price = $('#price').val();
$.ajax({
type : "POST",
url : "<?php echo site_url('Package/save')?>",
dataType : "JSON",
data : {pkg_id:pkg_id , test_quantity:test_quantity, price:price},
success: function(data){
$('[name="pkg_id"]').val("");
$('[name="test_quantity"]').val("");
$('[name="price"]').val("");
$('#Modal_Add').modal('hide');
show_package();
}
});
return false;
});
//get data for update record
$('#show_data').on('click','.item_edit',function(){
var pkg_id = $(this).data('pkg_id');
var test_quantity = $(this).data('test_quantity');
var price = $(this).data('price');
$('#Modal_Edit').modal('show');
$('[name="pkg_id_edit"]').val(pkg_id);
$('[name="test_quantity_edit"]').val(test_quantity);
$('[name="price_edit"]').val(price);
});
//update record to database
$('#btn_update').on('click',function(){
var pkg_id = $('#pkg_id_edit').val();
var test_quantity = $('#test_quantity_edit').val();
var price = $('#price_edit').val();
$.ajax({
type : "POST",
url : "<?php echo site_url('Package/update')?>",
dataType : "JSON",
data : {pkg_id:pkg_id , test_quantity:test_quantity, price:price},
success: function(data){
$('[name="pkg_id_edit"]').val("");
$('[name="test_quantity_edit"]').val("");
$('[name="price_edit"]').val("");
$('#Modal_Edit').modal('hide');
show_package();
}
});
return false;
});
//get data for delete record
$('#show_data').on('click','.item_delete',function(){
var pkg_id = $(this).data('pkg_id');
$('#Modal_Delete').modal('show');
$('[name="pkg_id_delete"]').val(pkg_id);
});
//delete record to database
$('#btn_delete').on('click',function(){
var pkg_id = $('#pkg_id_delete').val();
$.ajax({
type : "POST",
url : "<?php echo site_url('Package/delete')?>",
dataType : "JSON",
data : {pkg_id:pkg_id},
success: function(data){
$('[name="pkg_id_delete"]').val("");
$('#Modal_Delete').modal('hide');
show_package();
}
});
return false;
});
});
</script>
</body>
</html>
Packages.php
<?php
class Packages extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('package_model');
}
function index(){
$this->load->view('package_view');
}
function package_data(){
$data=$this->package_model->package_list();
echo json_encode($data);
}
function save(){
$data=$this->package_model->save_package();
echo json_encode($data);
}
function update(){
$data=$this->package_model->update_package();
echo json_encode($data);
}
function delete(){
$data=$this->package_model->delete_package();
echo json_encode($data);
}
}
and
package_model.php
<?php
class package_model extends CI_Model{
function package_list(){
$hasil=$this->db->get('tblexampackages');
return $hasil->result();
}
function save_package(){
$data = array(
'pkg_id' => $this->input->post('pkg_id'),
'test_quantity' => $this->input->post('test_quantity'),
'price' => $this->input->post('price'),
);
$result=$this->db->insert('tblexampackages',$data);
return $result;
}
function update_package(){
$pkg_id=$this->input->post('pkg_id');
$test_quantity=$this->input->post('test_quantity');
$price=$this->input->post('price');
$this->db->set('price', $price);
$this->db->set('test_quantity', $test_quantity);
$this->db->where('pkg_id', $pkg_id);
$result=$this->db->update('tblexampackages');
return $result;
}
function delete_package(){
$pkg_id=$this->input->post('pkg_id');
$this->db->where('pkg_id', $pkg_id);
$result=$this->db->delete('tblexampackages');
return $result;
}
}
Other files are autoload.php, config.php and database.php which are configured properly. Please tell me where I am going wrong here.
As for database, MySQL connectivity is good and the table named tblexampackages only has 3 Columns named pkg_id, test_quantity and price.
Thanks.
Your controller is Packages, you're trying to access Package... Edit this line from
url : '<?php echo site_url('Package/package_data')?>', //old
url : "<?php echo site_url('Packages/package_data')?>" //new
After which, edit your package_list to something like this
$query = $this->db->get('tbl_name');
return $query->result_array();
in your Ajax function show_package() you have:
url : '<?php echo site_url('Package/package_data')?>',
it should be
url : '<?php echo site_url('Packages/package_data')?>',

passing a specific row of data to a modal (laravel datatables plugin)

i have this page
and i have this modal
now i what i want to do is when i click on the "Editar" button of the row that contains the ID Cartão of "1011022" it should show like this:
with the values belonging to that row
how do i do that ? i've already searched for at least an hour and i couldn't find any reliable solutions
my html code is as follows:
#extends('admin.layouts.layout')
#section('content')
<!-- page script -->
<div class="wrapper row-offcanvas row-offcanvas-left">
<!-- Right side column. Contains the navbar and content of the page -->
<aside class="right-side">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Lista de alunos
</h1>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
</div> <!-- /.box-header -->
<div class="box-body table-responsive">
<button type="button" id="add_btn" class="btn btn-primary" data-toggle="modal" data-target="#addAluno">
<span class="glyphicon glyphicon-plus"></span>
Adicionar aluno
</button>
<br/><br/>
<!-- ADD MODAL SECTION -->
<div id="addAluno" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Adicionar aluno</h4>
</div>
<div class="modal-body">
<form role="form">
<div class="form-group">
<label>ID do Cartão</label>
<input type="text" class="form-control" id="id_cartao" placeholder="ID do Cartão"/>
</div>
<div class="form-group">
<label>Nome do Aluno</label>
<input type="text" class="form-control"
id="nome_aluno" placeholder="Nome do Aluno"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" id="email" placeholder="E-mail"/>
</div>
<div>
<label>Triénio</label>
<input type="text" class="form-control" id="trienio" placeholder="Triénio"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Adicionar aluno</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
<!-- END OF THE ADD MODAL SECTION -->
<!-- EDIT MODAL SECTION -->
<div id="editAluno" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Editar aluno</h4>
</div>
<div class="modal-body">
<form role="form">
<div class="form-group">
<label>ID do Cartão</label>
<input type="text" class="form-control" id="id_cartao" placeholder="ID do Cartão" value=>
</div>
<div class="form-group">
<label>Nome do aluno</label>
<input type="text" class="form-control"
id="nome_aluno" placeholder="Nome do Aluno"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" id="email" placeholder="E-mail"/>
</div>
<div>
<label>Triénio</label>
<input type="text" class="form-control" id="trienio" placeholder="Triénio"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Editar aluno</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
<!-- END OF THE EDIT MODAL SECTION -->
<table id="alunos_list" class="table table-bordered table-striped">
<thead>
<tr>
<th>ID Cartão</th>
<th>Nome</th>
<th>E-mail</th>
<th>Triénio</th>
<th>Curso</th>
<th>Ações</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID Cartão</th>
<th>Nome</th>
<th>E-mail</th>
<th>Triénio</th>
<th>Curso</th>
<th>Ações</th>
</tr>
</tfoot>
<tfoot>
</tfoot>
</table>
</div><!-- /.box-body -->
</div><!-- /.box -->
</div>
</div>
</section><!-- /.content -->
</aside><!-- /.right-side -->
</div><!-- ./wrapper -->
#endsection
#push('scripts')
<script>
$(function() {
$('#alunos_list').DataTable({
processing: true,
serverSide: true,
ajax: '{!! route('alunos.data') !!}',
columns: [
{ data: 'id_cartao', name: 'alunos.id_cartao' },
{ data: 'nome', name: 'alunos.nome' },
{ data: 'email', name: 'alunos.email' },
{ data: 'trienio.data_trienio', name: 'trienio.data_trienio' },
{ data: 'trienio.curso.curso', name: 'trienio.curso.curso' },
{ data: 'action', name: 'action', orderable: false, searchable: false }
],
initComplete: function () {
this.api().columns().every(function () {
var column = this;
if(column[0][0] == 5){
// intentionally empty, we want to exclude column 5 from searching
} else {
var input = document.createElement("input");
$(input).appendTo($(column.footer()).empty())
.on('keypress', function () {
column.search($(this).val(), false, false, true).draw();
});
}
});
}
});
});
</script>
<script>
</script>
#endpush
Inside your editor button, put data target to target editor's modal. After that, we listen for show event of modal when it show/shown like following:
Html
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#editAluno">
<span class="glyphicon glyphicon-plus"></span> Edit
</button>
Js
You can choose either show.bs.modal or shown.bs.modal, recommended just listen to show.bs.modal as you want to populate the data immediately after button clicked. Notice relatedTarget :
// listening for show event of editAluno's modal
$( '#editAluno' ).on( 'show.bs.modal', function (e) {
var target = e.relatedTarget;
// get values for particular rows
var tr = $( target ).closest( 'tr' );
var tds = tr.find( 'td' );
// put values into editor's form elements
// tds.eq(0).val() -- 1st column
$( '#id_cartao' ).val( tds.eq(0).val() );
// tds.eq(1).val() -- 2nd column and so on.
// same goes to others element
});

JavaScript code in modal of Bootstrap not run

JavaScript code in modal of Bootstrap not run:
in cosole, the error is like this:
if the code not put in modal,there is no error like above screenshot.
code:(code has not completed,I must let it run first.)
<style>
#image {
max-width: 100%;
}
</style>
<div id="gridSystemModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="gridModalLabel"
aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content" id="app" #submit.prevent="submit">
<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="gridModalLabel">upload avatar</h4>
</div>
<div class="modal-body">
<div class="container-fluid bd-example-row">
<div class="row">
<div class="col-md-8" style="height: 540px; ">
<img id="originalImage" class="card-img-top" src="holder.js/540x540?text=preview&nowrap=true" alt="" style="width: 100%; display: block;">
</div>
<div class="col-md-4">
<div class="card">
<div class="card-block">
<h6 class="card-subtitle text-muted">preview:</h6>
</div>
<img id="preview" alt="avatar" src="holder.js/180x180?text=preview&nowrap=true" style="height: 180px; width: 180px;">
</div>
<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-secondary"><i class="fa fa-undo" aria-hidden="true"></i></button>
<button type="button" class="btn btn-secondary"><i class="fa fa-repeat" aria-hidden="true"></i></button>
</div>
<div class="form-group">
<label for="avatar">File input</label>
<input type="file" class="form-control-file" id="avatar" name="avatar" #change="preview($event)">
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">cancel</button>
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</div>
</div>
<div class="bd-example bd-example-padded-bottom">
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#gridSystemModal">
upload avatar
</button>
</div>
<script>
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name=csrf-token]').getAttribute('content')
new Vue({
el: '#app',
data: {
avatar: {},
originalImage:{}
},
methods: {
preview: function(event) {
var picture = event.target.files;
$('#preview').attr('src', picture[0]);
this.avatar=picture[0];
},
submit: function(event) {
var formData=this.businessLicence.formData;
console.log(formData);
this.$http.post('/avatar/', formData,{
before(request) {
if (this.previousRequest) {
this.previousRequest.abort();
}
this.previousRequest = request;
}
}).then((response) => {
// success callback
}, (response) => {
// error callback
});
}
}
});
</script>
<script>
$('#image').cropper({
aspectRatio: 1 / 1,
crop: function (e) {
// Output the result data for cropping image.
console.log(e.x);
console.log(e.y);
console.log(e.width);
console.log(e.height);
console.log(e.rotate);
console.log(e.scaleX);
console.log(e.scaleY);
}
});
</script>
Based on the error you have at the top of your question you need to include the files that Vue references and you also need to include Jquery
JQUERY
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
Bootstrap:
Some plugins and CSS components depend on other plugins. If you
include plugins individually, make sure to check for these
dependencies in the docs. Also note that all plugins depend on jQuery
(this means jQuery must be included before the plugin files).
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" ></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.min.js"></script>
Don't forget to init your Modal:
<script>
$('#gridSystemModal').modal('show');
</script>

Categories