06-04-2024
This commit is contained in:
53
editable-bootstrap-table/action.php
Normal file
53
editable-bootstrap-table/action.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
#include_once("inc/db_connect.php");
|
||||
|
||||
$conn = new PDO('sqlite:../db_photo.sqlite3');
|
||||
//$stmt = $conn->prepare($query);
|
||||
//$stmt->execute();
|
||||
|
||||
include 'functions.php';
|
||||
|
||||
_pr($_POST);
|
||||
// lens legende copyright title creator keywords
|
||||
|
||||
if ($_POST['action'] == 'edit' && $_POST['id']) {
|
||||
$updateField='';
|
||||
if(isset($_POST['lens'])) {
|
||||
$updateField.= "lens='".$_POST['lens']."'";
|
||||
} else if(isset($_POST['legende'])) {
|
||||
$updateField.= "legende='".$_POST['legende']."'";
|
||||
} else if(isset($_POST['copyright'])) {
|
||||
$updateField.= "copyright='".$_POST['copyright']."'";
|
||||
} else if(isset($_POST['title'])) {
|
||||
$updateField.= "title='".$_POST['title']."'";
|
||||
} else if(isset($_POST['creator'])) {
|
||||
$updateField.= "creator='".$_POST['creator']."'";
|
||||
} else if(isset($_POST['keywords'])) {
|
||||
$updateField.= "keywords='".$_POST['keywords']."'";
|
||||
}
|
||||
if($updateField && $_POST['id']) {
|
||||
$sqlQuery = "UPDATE photos SET $updateField WHERE id='" . $_POST['id'] . "'";
|
||||
echo $sqlQuery;
|
||||
/*
|
||||
mysqli_query($conn, $sqlQuery) or die("database error:". mysqli_error($conn));
|
||||
$data = array(
|
||||
"message" => "Record Updated",
|
||||
"status" => 1
|
||||
);
|
||||
echo json_encode($data);
|
||||
*/
|
||||
}
|
||||
}
|
||||
if ($_POST['action'] == 'delete' && $_POST['id']) {
|
||||
$sqlQuery = "DELETE FROM photos WHERE id='" . $_POST['id'] . "'";
|
||||
echo $sqlQuery;
|
||||
/*
|
||||
mysqli_query($conn, $sqlQuery) or die("database error:". mysqli_error($conn));
|
||||
$data = array(
|
||||
"message" => "Record Deleted",
|
||||
"status" => 1
|
||||
);
|
||||
echo json_encode($data);
|
||||
*/
|
||||
}
|
||||
|
||||
0
editable-bootstrap-table/db_photo.sqlite3
Normal file
0
editable-bootstrap-table/db_photo.sqlite3
Normal file
26
editable-bootstrap-table/inc/container.php
Normal file
26
editable-bootstrap-table/inc/container.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<link rel="icon" type="image/png" href="https://webdamn.com/wp-content/themes/v2/webdamn.png">
|
||||
</head>
|
||||
<body class="">
|
||||
<div role="navigation" class="navbar navbar-default navbar-static-top">
|
||||
<div class="container">
|
||||
<div class="navbar-header">
|
||||
<button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button">
|
||||
<span class="sr-only">Toggle navigation</span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a href="https://www.webdamn.com" class="navbar-brand">WEBDAMN.COM</a>
|
||||
</div>
|
||||
<div class="navbar-collapse collapse">
|
||||
<ul class="nav navbar-nav">
|
||||
<li class="active"><a href="https://www.webdamn.com">Home</a></li>
|
||||
|
||||
</ul>
|
||||
|
||||
</div><!--/.nav-collapse -->
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="container" style="min-height:500px;">
|
||||
|
||||
12
editable-bootstrap-table/inc/db_connect.php
Normal file
12
editable-bootstrap-table/inc/db_connect.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
/* Database connection start */
|
||||
$servername = "localhost";
|
||||
$username = "root";
|
||||
$password = "";
|
||||
$dbname = "phpzag_demos";
|
||||
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
|
||||
if (mysqli_connect_errno()) {
|
||||
printf("Connect failed: %s\n", mysqli_connect_error());
|
||||
exit();
|
||||
}
|
||||
?>
|
||||
6
editable-bootstrap-table/inc/footer.php
Normal file
6
editable-bootstrap-table/inc/footer.php
Normal file
@@ -0,0 +1,6 @@
|
||||
<div class="insert-post-ads1" style="margin-top:20px;">
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</body></html>
|
||||
|
||||
9
editable-bootstrap-table/inc/header.php
Normal file
9
editable-bootstrap-table/inc/header.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
|
||||
<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.5/js/bootstrap.min.js"></script>
|
||||
<!-- jQuery -->
|
||||
70
editable-bootstrap-table/index.php
Normal file
70
editable-bootstrap-table/index.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
include("inc/header.php");
|
||||
?>
|
||||
<title>webdamn.com : Demo Create Editable Bootstrap Table with jQuery, PHP & MySQL</title>
|
||||
<?php include('inc/container.php');?>
|
||||
<style>
|
||||
table {
|
||||
background-color: #181818;
|
||||
}
|
||||
table, .table {
|
||||
color: #fff;
|
||||
}
|
||||
</style>
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<h2>Example: Create Editable Bootstrap Table with jQuery, PHP & MySQL</h2>
|
||||
<?php
|
||||
//include_once("inc/db_connect.php");
|
||||
//$sqlQuery = "SELECT id, name, gender, age FROM developers LIMIT 5";
|
||||
//$resultSet = mysqli_query($conn, $sqlQuery) or die("database error:". mysqli_error($conn));
|
||||
|
||||
$base = '../db_photo.sqlite3';
|
||||
|
||||
//$query = "SELECT * FROM photos " . $req . " ORDER BY id";
|
||||
$query = "SELECT * FROM photos WHERE filename LIKE '%3_2007.jpg' OR filename LIKE '%4_2007.jpg' ORDER BY id";
|
||||
|
||||
try {
|
||||
$conn = new PDO('sqlite:../db_photo.sqlite3');
|
||||
$stmt = $conn->prepare($query);
|
||||
$stmt->execute();
|
||||
|
||||
?>
|
||||
|
||||
<table id="editableTable" class="table table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Id</th>
|
||||
<th>Filename</th>
|
||||
<th>Date</th>
|
||||
<th>Lens</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php //while( $developer = mysqli_fetch_assoc($resultSet) ) { ?>
|
||||
<?php //while(true) {
|
||||
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { ?>
|
||||
<tr id="<?php echo $row ['id']; ?>">
|
||||
<td><?php echo $row ['id']; ?></td>
|
||||
<td><?php echo $row ['filename']; ?></td>
|
||||
<td><?php echo $row ['dateoriginal']; ?></td>
|
||||
<td><?php echo $row ['lens']; ?></td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php }
|
||||
catch(PDOException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<script src="plugin/bootstable.js"></script>
|
||||
<script src="js/editable.js"></script>
|
||||
<?php include('inc/footer.php');?>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
41
editable-bootstrap-table/js/editable.js
Normal file
41
editable-bootstrap-table/js/editable.js
Normal file
@@ -0,0 +1,41 @@
|
||||
$( document ).ready(function() {
|
||||
$('#editableTable').SetEditable({
|
||||
columnsEd: "0,1,2,3,4,5,6",
|
||||
onEdit: function(columnsEd) {
|
||||
console.log("===edit=="+(this));
|
||||
// lens legende copyright title creator keywords
|
||||
|
||||
var empId = columnsEd[0].childNodes[1].innerHTML;
|
||||
var legende = columnsEd[0].childNodes[3].innerHTML;
|
||||
var copyright = columnsEd[0].childNodes[5].innerHTML;
|
||||
var title = columnsEd[0].childNodes[7].innerHTML;
|
||||
var creator = columnsEd[0].childNodes[9].innerHTML;
|
||||
var keywords = columnsEd[0].childNodes[11].innerHTML;
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url : "action.php",
|
||||
dataType: "json",
|
||||
data: {id:empId, legende:legende, copyright:copyright, title:title, creator:creator, keywords:keywords, action:'edit'},
|
||||
success: function (response) {
|
||||
if(response.status) {
|
||||
// show update message
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
onBeforeDelete: function(columnsEd) {
|
||||
var empId = columnsEd[0].childNodes[1].innerHTML;
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url : "action.php",
|
||||
dataType: "json",
|
||||
data: {id:empId, action:'delete'},
|
||||
success: function (response) {
|
||||
if(response.status) {
|
||||
// show delete message
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
});
|
||||
201
editable-bootstrap-table/plugin/bootstable.js
Normal file
201
editable-bootstrap-table/plugin/bootstable.js
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
Bootstable
|
||||
@description Javascript library to make HMTL tables editable, using Bootstrap
|
||||
@version 1.1
|
||||
@autor Tito Hinostroza
|
||||
*/
|
||||
"use strict";
|
||||
//Global variables
|
||||
var params = null; //Parameters
|
||||
var colsEdi =null;
|
||||
var newColHtml = '<div class="btn-group pull-right">'+
|
||||
'<button id="bEdit" type="button" class="btn btn-sm btn-default" onclick="rowEdit(this);">' +
|
||||
'<span class="glyphicon glyphicon-pencil" > </span>'+
|
||||
'</button>'+
|
||||
'<button id="bElim" type="button" class="btn btn-sm btn-default" onclick="rowElim(this);">' +
|
||||
'<span class="glyphicon glyphicon-trash" > </span>'+
|
||||
'</button>'+
|
||||
'<button id="bAcep" type="button" class="btn btn-sm btn-default" style="display:none;" onclick="rowAcep(this);">' +
|
||||
'<span class="glyphicon glyphicon-ok" > </span>'+
|
||||
'</button>'+
|
||||
'<button id="bCanc" type="button" class="btn btn-sm btn-default" style="display:none;" onclick="rowCancel(this);">' +
|
||||
'<span class="glyphicon glyphicon-remove" > </span>'+
|
||||
'</button>'+
|
||||
'</div>';
|
||||
var colEdicHtml = '<td name="buttons">'+newColHtml+'</td>';
|
||||
|
||||
$.fn.SetEditable = function (options) {
|
||||
var defaults = {
|
||||
columnsEd: null, //Index to editable columns. If null all td editables. Ex.: "1,2,3,4,5"
|
||||
$addButton: null, //Jquery object of "Add" button
|
||||
onEdit: function() {}, //Called after edition
|
||||
onBeforeDelete: function() {}, //Called before deletion
|
||||
onDelete: function() {}, //Called after deletion
|
||||
onAdd: function() {} //Called when added a new row
|
||||
};
|
||||
params = $.extend(defaults, options);
|
||||
this.find('thead tr').append('<th name="buttons"></th>'); //encabezado vacío
|
||||
this.find('tbody tr').append(colEdicHtml);
|
||||
var $tabedi = this; //Read reference to the current table, to resolve "this" here.
|
||||
//Process "addButton" parameter
|
||||
if (params.$addButton != null) {
|
||||
//Se proporcionó parámetro
|
||||
params.$addButton.click(function() {
|
||||
rowAddNew($tabedi.attr("id"));
|
||||
});
|
||||
}
|
||||
//Process "columnsEd" parameter
|
||||
if (params.columnsEd != null) {
|
||||
//Extract felds
|
||||
colsEdi = params.columnsEd.split(',');
|
||||
}
|
||||
};
|
||||
function IterarCamposEdit($cols, tarea) {
|
||||
//Itera por los campos editables de una fila
|
||||
var n = 0;
|
||||
$cols.each(function() {
|
||||
n++;
|
||||
if ($(this).attr('name')=='buttons') return; //excluye columna de botones
|
||||
if (!EsEditable(n-1)) return; //noe s campo editable
|
||||
tarea($(this));
|
||||
});
|
||||
|
||||
function EsEditable(idx) {
|
||||
//Indica si la columna pasada está configurada para ser editable
|
||||
if (colsEdi==null) { //no se definió
|
||||
return true; //todas son editable
|
||||
} else { //hay filtro de campos
|
||||
//alert('verificando: ' + idx);
|
||||
for (var i = 0; i < colsEdi.length; i++) {
|
||||
if (idx == colsEdi[i]) return true;
|
||||
}
|
||||
return false; //no se encontró
|
||||
}
|
||||
}
|
||||
}
|
||||
function FijModoNormal(but) {
|
||||
$(but).parent().find('#bAcep').hide();
|
||||
$(but).parent().find('#bCanc').hide();
|
||||
$(but).parent().find('#bEdit').show();
|
||||
$(but).parent().find('#bElim').show();
|
||||
var $row = $(but).parents('tr'); //accede a la fila
|
||||
$row.attr('id', ''); //quita marca
|
||||
}
|
||||
function FijModoEdit(but) {
|
||||
$(but).parent().find('#bAcep').show();
|
||||
$(but).parent().find('#bCanc').show();
|
||||
$(but).parent().find('#bEdit').hide();
|
||||
$(but).parent().find('#bElim').hide();
|
||||
var $row = $(but).parents('tr'); //accede a la fila
|
||||
$row.attr('id', 'editing'); //indica que está en edición
|
||||
}
|
||||
function ModoEdicion($row) {
|
||||
if ($row.attr('id')=='editing') {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
function rowAcep(but) {
|
||||
//Acepta los cambios de la edición
|
||||
var $row = $(but).parents('tr'); //accede a la fila
|
||||
var $cols = $row.find('td'); //lee campos
|
||||
if (!ModoEdicion($row)) return; //Ya está en edición
|
||||
//Está en edición. Hay que finalizar la edición
|
||||
IterarCamposEdit($cols, function($td) { //itera por la columnas
|
||||
var cont = $td.find('input').val(); //lee contenido del input
|
||||
$td.html(cont); //fija contenido y elimina controles
|
||||
});
|
||||
FijModoNormal(but);
|
||||
params.onEdit($row);
|
||||
}
|
||||
function rowCancel(but) {
|
||||
//Rechaza los cambios de la edición
|
||||
var $row = $(but).parents('tr'); //accede a la fila
|
||||
var $cols = $row.find('td'); //lee campos
|
||||
if (!ModoEdicion($row)) return; //Ya está en edición
|
||||
//Está en edición. Hay que finalizar la edición
|
||||
IterarCamposEdit($cols, function($td) { //itera por la columnas
|
||||
var cont = $td.find('div').html(); //lee contenido del div
|
||||
$td.html(cont); //fija contenido y elimina controles
|
||||
});
|
||||
FijModoNormal(but);
|
||||
}
|
||||
function rowEdit(but) { //Inicia la edición de una fila
|
||||
var $row = $(but).parents('tr'); //accede a la fila
|
||||
var $cols = $row.find('td'); //lee campos
|
||||
if (ModoEdicion($row)) return; //Ya está en edición
|
||||
//Pone en modo de edición
|
||||
IterarCamposEdit($cols, function($td) { //itera por la columnas
|
||||
var cont = $td.html(); //lee contenido
|
||||
var div = '<div style="display: none;">' + cont + '</div>'; //guarda contenido
|
||||
var input = '<input class="form-control input-sm" value="' + cont + '">';
|
||||
$td.html(div + input); //fija contenido
|
||||
});
|
||||
FijModoEdit(but);
|
||||
}
|
||||
function rowElim(but) { //Elimina la fila actual
|
||||
var $row = $(but).parents('tr'); //accede a la fila
|
||||
params.onBeforeDelete($row);
|
||||
$row.remove();
|
||||
params.onDelete();
|
||||
}
|
||||
function rowAddNew(tabId) { //Agrega fila a la tabla indicada.
|
||||
var $tab_en_edic = $("#" + tabId); //Table to edit
|
||||
var $filas = $tab_en_edic.find('tbody tr');
|
||||
if ($filas.length==0) {
|
||||
//No hay filas de datos. Hay que crearlas completas
|
||||
var $row = $tab_en_edic.find('thead tr'); //encabezado
|
||||
var $cols = $row.find('th'); //lee campos
|
||||
//construye html
|
||||
var htmlDat = '';
|
||||
$cols.each(function() {
|
||||
if ($(this).attr('name')=='buttons') {
|
||||
//Es columna de botones
|
||||
htmlDat = htmlDat + colEdicHtml; //agrega botones
|
||||
} else {
|
||||
htmlDat = htmlDat + '<td></td>';
|
||||
}
|
||||
});
|
||||
$tab_en_edic.find('tbody').append('<tr>'+htmlDat+'</tr>');
|
||||
} else {
|
||||
//Hay otras filas, podemos clonar la última fila, para copiar los botones
|
||||
var $ultFila = $tab_en_edic.find('tr:last');
|
||||
$ultFila.clone().appendTo($ultFila.parent());
|
||||
$ultFila = $tab_en_edic.find('tr:last');
|
||||
var $cols = $ultFila.find('td'); //lee campos
|
||||
$cols.each(function() {
|
||||
if ($(this).attr('name')=='buttons') {
|
||||
//Es columna de botones
|
||||
} else {
|
||||
$(this).html(''); //limpia contenido
|
||||
}
|
||||
});
|
||||
}
|
||||
params.onAdd();
|
||||
}
|
||||
function TableToCSV(tabId, separator) { //Convierte tabla a CSV
|
||||
var datFil = '';
|
||||
var tmp = '';
|
||||
var $tab_en_edic = $("#" + tabId); //Table source
|
||||
$tab_en_edic.find('tbody tr').each(function() {
|
||||
//Termina la edición si es que existe
|
||||
if (ModoEdicion($(this))) {
|
||||
$(this).find('#bAcep').click(); //acepta edición
|
||||
}
|
||||
var $cols = $(this).find('td'); //lee campos
|
||||
datFil = '';
|
||||
$cols.each(function() {
|
||||
if ($(this).attr('name')=='buttons') {
|
||||
//Es columna de botones
|
||||
} else {
|
||||
datFil = datFil + $(this).html() + separator;
|
||||
}
|
||||
});
|
||||
if (datFil!='') {
|
||||
datFil = datFil.substr(0, datFil.length-separator.length);
|
||||
}
|
||||
tmp = tmp + datFil + '\n';
|
||||
});
|
||||
return tmp;
|
||||
}
|
||||
Reference in New Issue
Block a user