start copy from cimac web
This commit is contained in:
@@ -0,0 +1,678 @@
|
||||
/*!
|
||||
|
||||
* Tabledit v1.2.3 (https://github.com/markcell/jQuery-Tabledit)
|
||||
|
||||
* Copyright (c) 2015 Celso Marques
|
||||
|
||||
* Licensed under MIT (https://github.com/markcell/jQuery-Tabledit/blob/master/LICENSE)
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* @description Inline editor for HTML tables compatible with Bootstrap
|
||||
|
||||
* @version 1.2.3
|
||||
|
||||
* @author Celso Marques
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
if (typeof jQuery === 'undefined') {
|
||||
|
||||
throw new Error('Tabledit requires jQuery library.');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
(function($) {
|
||||
|
||||
'use strict';
|
||||
|
||||
|
||||
|
||||
$.fn.Tabledit = function(options) {
|
||||
|
||||
if (!this.is('table')) {
|
||||
|
||||
throw new Error('Tabledit only works when applied to a table.');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
var $table = this;
|
||||
|
||||
|
||||
|
||||
var defaults = {
|
||||
|
||||
url: window.location.href,
|
||||
|
||||
inputClass: 'form-control input-sm',
|
||||
|
||||
toolbarClass: 'btn-toolbar',
|
||||
|
||||
groupClass: 'btn-group btn-group-sm',
|
||||
|
||||
dangerClass: 'danger',
|
||||
|
||||
warningClass: 'warning',
|
||||
|
||||
mutedClass: 'text-muted bg-light',
|
||||
|
||||
eventType: 'click',
|
||||
|
||||
rowIdentifier: 'id',
|
||||
|
||||
hideIdentifier: false,
|
||||
|
||||
autoFocus: true,
|
||||
|
||||
editButton: true,
|
||||
|
||||
deleteButton: true,
|
||||
|
||||
saveButton: true,
|
||||
|
||||
restoreButton: true,
|
||||
|
||||
buttons: {
|
||||
|
||||
edit: {
|
||||
|
||||
class: 'btn btn-sm btn-default',
|
||||
|
||||
html: '<span class="fa fa-pencil "></span>',
|
||||
|
||||
action: 'edit'
|
||||
|
||||
},
|
||||
|
||||
delete: {
|
||||
|
||||
class: 'btn btn-sm btn-default',
|
||||
|
||||
html: '<span class="fa fa-trash "></span>',
|
||||
|
||||
action: 'delete'
|
||||
|
||||
},
|
||||
|
||||
save: {
|
||||
|
||||
class: 'btn btn-sm btn-success',
|
||||
|
||||
html: 'Save'
|
||||
|
||||
},
|
||||
|
||||
restore: {
|
||||
|
||||
class: 'btn btn-sm btn-warning',
|
||||
|
||||
html: 'Restore',
|
||||
|
||||
action: 'restore'
|
||||
|
||||
},
|
||||
|
||||
confirm: {
|
||||
|
||||
class: 'btn btn-sm btn-danger',
|
||||
|
||||
html: 'Confirm'
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
onDraw: function() { return; },
|
||||
|
||||
onSuccess: function() { return; },
|
||||
|
||||
onFail: function() { return; },
|
||||
|
||||
onAlways: function() { return; },
|
||||
|
||||
onAjax: function() { return; }
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
var settings = $.extend(true, defaults, options);
|
||||
|
||||
|
||||
|
||||
var $lastEditedRow = 'undefined';
|
||||
|
||||
var $lastDeletedRow = 'undefined';
|
||||
|
||||
var $lastRestoredRow = 'undefined';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Draw Tabledit structure (identifier column, editable columns, toolbar column).
|
||||
|
||||
*
|
||||
|
||||
* @type {object}
|
||||
|
||||
*/
|
||||
|
||||
var Draw = {
|
||||
|
||||
columns: {
|
||||
|
||||
identifier: function() {
|
||||
|
||||
// Hide identifier column.
|
||||
|
||||
if (settings.hideIdentifier) {
|
||||
|
||||
$table.find('th:nth-child(' + parseInt(settings.columns.identifier[0]) + 1 + '), tbody td:nth-child(' + parseInt(settings.columns.identifier[0]) + 1 + ')').hide();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
var $td = $table.find('tbody td:nth-child(' + (parseInt(settings.columns.identifier[0]) + 1) + ')');
|
||||
|
||||
|
||||
|
||||
$td.each(function() {
|
||||
|
||||
|
||||
|
||||
// Create hidden input with row identifier.
|
||||
|
||||
var span = '<span class="tabledit-span tabledit-identifier">' + $(this).text() + '</span>';
|
||||
|
||||
var input = '<input class="tabledit-input tabledit-identifier" type="hidden" name="' + settings.columns.identifier[1] + '" value="' + $(this).text() + '" disabled>';
|
||||
|
||||
|
||||
|
||||
// Add elements to table cell.
|
||||
|
||||
$(this).html(span + input);
|
||||
|
||||
|
||||
|
||||
// Add attribute "id" to table row.
|
||||
|
||||
$(this).parent('tr').attr(settings.rowIdentifier, $(this).text());
|
||||
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
editable: function() {
|
||||
|
||||
for (var i = 0; i < settings.columns.editable.length; i++) {
|
||||
|
||||
var $td = $table.find('tbody td:nth-child(' + (parseInt(settings.columns.editable[i][0]) + 1) + ')');
|
||||
|
||||
|
||||
|
||||
$td.each(function() {
|
||||
|
||||
// Get text of this cell.
|
||||
|
||||
var text = $(this).text();
|
||||
|
||||
|
||||
|
||||
// Add pointer as cursor.
|
||||
|
||||
if (!settings.editButton) {
|
||||
|
||||
$(this).css('cursor', 'pointer');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(settings.columns.editable[i][1]=='filenameaudit'){
|
||||
|
||||
var spantext='<a href="upload/'+$(this).text() +'" target="_blank">'+$(this).text()+' </a>';
|
||||
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
var spantext=$(this).text();
|
||||
|
||||
}
|
||||
|
||||
// Create span element.
|
||||
|
||||
var span = '<span class="tabledit-span">' + spantext + '</span>';
|
||||
|
||||
|
||||
|
||||
// Check if exists the third parameter of editable array.
|
||||
|
||||
if (typeof settings.columns.editable[i][2] !== 'undefined') {
|
||||
|
||||
// Create select element.
|
||||
|
||||
if(settings.columns.editable[i][2]=='checkbox'){
|
||||
|
||||
if (text === 'yes') {
|
||||
|
||||
var input = '<input class="tabledit-input" type="checkbox" name="' + settings.columns.editable[i][1] + '" value="yes" checked="checked" style="display: none;" disabled>';
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
else{
|
||||
|
||||
var input = '<input class="tabledit-input" type="checkbox" name="' + settings.columns.editable[i][1] + '" value="yes" style="display: none;" disabled>';
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
else if(settings.columns.editable[i][2]=='file'){
|
||||
|
||||
var input = '<input class="tabledit-input file-upload" type="text" name="' + settings.columns.editable[i][1] + '" style="display: none;" value="' + $(this).text() + '" disabled><span style="display:none;" class="uploadfile" >Upload File</span>';
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
else{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
var input = '<select class="tabledit-input ' + settings.inputClass + '" name="' + settings.columns.editable[i][1] + '" style="display: none;" disabled>';
|
||||
|
||||
|
||||
|
||||
// Create options for select element.
|
||||
|
||||
$.each(jQuery.parseJSON(settings.columns.editable[i][2]), function(index, value) {
|
||||
|
||||
if (text === value) {
|
||||
|
||||
input += '<option value="' + index + '" selected>' + value + '</option>';
|
||||
|
||||
} else {
|
||||
|
||||
input += '<option value="' + index + '">' + value + '</option>';
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Create last piece of select element.
|
||||
|
||||
input += '</select>';
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// Create text input element.
|
||||
|
||||
var input = '<input class="tabledit-input ' + settings.inputClass + '" type="text" name="' + settings.columns.editable[i][1] + '" value="' + $(this).text() + '" style="display: none;" disabled>';
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Add elements and class "view" to table cell.
|
||||
|
||||
$(this).html(span + input);
|
||||
|
||||
$(this).addClass('tabledit-view-mode');
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
toolbar: function() {
|
||||
|
||||
if (settings.editButton || settings.deleteButton) {
|
||||
|
||||
var editButton = '';
|
||||
|
||||
var deleteButton = '';
|
||||
|
||||
var saveButton = '';
|
||||
|
||||
var restoreButton = '';
|
||||
|
||||
var confirmButton = '';
|
||||
|
||||
|
||||
|
||||
// Add toolbar column header if not exists.
|
||||
|
||||
if ($table.find('th.tabledit-toolbar-column').length === 0) {
|
||||
|
||||
$table.find('tr:first').append('<th class="tabledit-toolbar-column"></th>');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Create edit button.
|
||||
|
||||
if (settings.editButton) {
|
||||
|
||||
editButton = '<button type="button" class="tabledit-edit-button ' + settings.buttons.edit.class + '" style="float: none;">' + settings.buttons.edit.html + '</button>';
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Create delete button.
|
||||
|
||||
if (settings.deleteButton) {
|
||||
|
||||
deleteButton = '<button type="button" class="tabledit-delete-button ' + settings.buttons.delete.class + '" style="float: none;">' + settings.buttons.delete.html + '</button>';
|
||||
|
||||
confirmButton = '<button type="button" class="tabledit-confirm-button ' + settings.buttons.confirm.class + '" style="display: none; float: none;">' + settings.buttons.confirm.html + '</button>';
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Create save button.
|
||||
|
||||
if (settings.editButton && settings.saveButton) {
|
||||
|
||||
saveButton = '<button type="button" class="tabledit-save-button ' + settings.buttons.save.class + '" style="display: none; float: none;">' + settings.buttons.save.html + '</button>';
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Create restore button.
|
||||
|
||||
if (settings.deleteButton && settings.restoreButton) {
|
||||
|
||||
restoreButton = '<button type="button" class="tabledit-restore-button ' + settings.buttons.restore.class + '" style="display: none; float: none;">' + settings.buttons.restore.html + '</button>';
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
var toolbar = '<div class="tabledit-toolbar ' + settings.toolbarClass + '" style="text-align: left;">\n\
|
||||
|
||||
<div class="' + settings.groupClass + '" style="float: none;">' + editButton + deleteButton + '</div>\n\
|
||||
|
||||
' + saveButton + '\n\
|
||||
|
||||
' + confirmButton + '\n\
|
||||
|
||||
' + restoreButton + '\n\
|
||||
|
||||
</div></div>';
|
||||
|
||||
|
||||
|
||||
// Add toolbar column cells.
|
||||
|
||||
$table.find('tr:gt(0)').append('<td style="white-space: nowrap; width: 1%;">' + toolbar + '</td>');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Change to view mode or edit mode with table td element as parameter.
|
||||
|
||||
*
|
||||
|
||||
* @type object
|
||||
|
||||
*/
|
||||
|
||||
var Mode = {
|
||||
|
||||
view: function(td) {
|
||||
|
||||
// Get table row.
|
||||
|
||||
var $tr = $(td).parent('tr');
|
||||
|
||||
// Disable identifier.
|
||||
|
||||
$(td).parent('tr').find('.tabledit-input.tabledit-identifier').prop('disabled', true);
|
||||
|
||||
// Hide and disable input element.
|
||||
|
||||
$(td).find('.tabledit-input').blur().hide().prop('disabled', true);
|
||||
|
||||
// Show span element.
|
||||
|
||||
$(td).find('.tabledit-span').show();
|
||||
|
||||
$(td).find('.uploadfile').hide();
|
||||
|
||||
// Add "view" class and remove "edit" class in td element.
|
||||
|
||||
$(td).addClass('tabledit-view-mode').removeClass('tabledit-edit-mode');
|
||||
|
||||
// Update toolbar buttons.
|
||||
|
||||
if (settings.editButton) {
|
||||
|
||||
$tr.find('button.tabledit-save-button').hide();
|
||||
|
||||
$tr.find('button.tabledit-edit-button').removeClass('active').blur();
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
edit: function(td) {
|
||||
|
||||
Delete.reset(td);
|
||||
|
||||
// Get table row.
|
||||
|
||||
var $tr = $(td).parent('tr');
|
||||
|
||||
// Enable identifier.
|
||||
|
||||
$tr.find('.tabledit-input.tabledit-identifier').prop('disabled', false);
|
||||
|
||||
// Hide span element.
|
||||
|
||||
$(td).find('.tabledit-span').hide();
|
||||
|
||||
$(td).find('.uploadfile').show();
|
||||
|
||||
// Get input element.
|
||||
|
||||
var $input = $(td).find('.tabledit-input');
|
||||
|
||||
// Enable and show input element.
|
||||
|
||||
$input.prop('disabled', false).show();
|
||||
|
||||
// Focus on input element.
|
||||
|
||||
if (settings.autoFocus) {
|
||||
|
||||
$input.focus();
|
||||
|
||||
}
|
||||
|
||||
// Add "edit" class and remove "view" class in td element.
|
||||
|
||||
$(td).addClass('tabledit-edit-mode').removeClass('tabledit-view-mode');
|
||||
|
||||
// Update toolbar buttons.
|
||||
|
||||
if (settings.editButton) {
|
||||
|
||||
$tr.find('button.tabledit-edit-button').addClass('active');
|
||||
|
||||
$tr.find('button.tabledit-save-button').show();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Available actions for edit function, with table td element as parameter or set of td elements.
|
||||
|
||||
*
|
||||
|
||||
* @type object
|
||||
|
||||
*/
|
||||
|
||||
var Edit = {
|
||||
|
||||
reset: function(td) {
|
||||
|
||||
$(td).each(function() {
|
||||
|
||||
// Get input element.
|
||||
|
||||
var $input = $(this).find('.tabledit-input');
|
||||
|
||||
var inputname=$input.attr('name');
|
||||
|
||||
// Get span text.
|
||||
|
||||
var text = $(this).find('.tabledit-span').text();
|
||||
|
||||
|
||||
|
||||
// Set input/select value with span text.
|
||||
|
||||
if ($input.is('select')) {
|
||||
|
||||
$input.find('option').filter(function() {
|
||||
|
||||
return $.trim($(this).text()) === text;
|
||||
|
||||
}).attr('selected', true);
|
||||
|
||||
}
|
||||
|
||||
else if($input.is(':checkbox')){
|
||||
|
||||
|
||||
|
||||
if(text=='yes'){
|
||||
|
||||
$input.attr('checked', 'checked');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
else if(inputname=='filenameaudit'){
|
||||
|
||||
|
||||
|
||||
var filename=$(this).find('.tabledit-span').text();
|
||||
|
||||
if(filename!=''){
|
||||
|
||||
$(this).find('.tabledit-span').html('<a href="upload/'+filename +'" target="_blank">'+filename+' </a>');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
$input.val(text);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Change to view mode.
|
||||
|
||||
Mode.view(this);
|
||||
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
submit: function(td) {
|
||||
|
||||
// Send AJAX request to server.
|
||||
|
||||
var ajaxResult = ajax(settings.buttons.edit.action);
|
||||
|
||||
|
||||
|
||||
if (ajaxResult === false) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$(td).each(function() {
|
||||
|
||||
// Get input element.
|
||||
|
||||
var $input = $(this).find('.tabledit-input');
|
||||
|
||||
var inputname=$input.attr('name');
|
||||
|
||||
// Set span text with input/select new value.
|
||||
|
||||
if ($input.is('select')) {
|
||||
|
||||
$(this).find('.tabledit-span').text($input.find('option:selected').text());
|
||||
|
||||
} else {
|
||||
|
||||
$(this).find('.tabledit-span').text($input.val());
|
||||
|
||||
}
|
||||
|
||||
if ($input.is(':checkbox')) {
|
||||
|
||||
if($input.prop('checked')==true){
|
||||
|
||||
$(this).find('.tabledit-span').text('yes');
|
||||
|
||||
Reference in New Issue
Block a user