'
);
// add Edit Button
if( this.s.dt.button('edit:name') )
{
this.s.dt.button('edit:name').action( function(e, dt, node, config) {
var rows = dt.rows({
selected: true
}).count();
that._openEditModal();
});
$(document).on('click', '#editRowBtn', function(e)
{
e.preventDefault();
e.stopPropagation();
that._editRowData();
});
}
// add Delete Button
if( this.s.dt.button('delete:name') )
{
this.s.dt.button('delete:name').action( function(e, dt, node, config) {
var rows = dt.rows({
selected: true
}).count();
that._openDeleteModal();
});
$(document).on('click', '#deleteRowBtn', function(e)
{
e.preventDefault();
e.stopPropagation();
that._deleteRow();
});
}
// add Add Button
if( this.s.dt.button('add:name') )
{
this.s.dt.button('add:name').action( function(e, dt, node, config) {
var rows = dt.rows({
selected: true
}).count();
that._openAddModal();
});
$(document).on('click', '#addRowBtn', function(e)
{
e.preventDefault();
e.stopPropagation();
that._addRowData();
});
}
},
/**
* Emit an event on the DataTable for listeners
*
* @param {string} name Event name
* @param {array} args Event arguments
* @private
*/
_emitEvent: function ( name, args )
{
this.s.dt.iterator( 'table', function ( ctx, i ) {
$(ctx.nTable).triggerHandler( name+'.dt', args );
} );
},
/**
* Open Edit Modal for selected row
*
* @private
*/
_openEditModal: function ( )
{
var that = this;
var dt = this.s.dt;
var columnDefs = [];
for( var i = 0; i < dt.context[0].aoColumns.length; i++ )
{
columnDefs.push({ title: dt.context[0].aoColumns[i].sTitle })
}
var adata = dt.rows({
selected: true
});
var data = "";
data += "";
$('#altEditor-modal').on('show.bs.modal', function() {
$('#altEditor-modal').find('.modal-title').html('Edit Record');
$('#altEditor-modal').find('.modal-body').html('
' + data + '
');
$('#altEditor-modal').find('.modal-footer').html("\
");
});
$('#altEditor-modal').modal('show');
$('#altEditor-modal input[0]').focus();
},
_editRowData: function()
{
var that = this;
var dt = this.s.dt;
var data = [];
$('form[name="altEditor-form"] input').each(function( i )
{
data.push( $(this).val() );
});
$('#altEditor-modal .modal-body .alert').remove();
var message = '
\
Success! This record has been updated.\
';
$('#altEditor-modal .modal-body').append(message);
dt.row({ selected:true }).data(data);
},
/**
* Open Delete Modal for selected row
*
* @private
*/
_openDeleteModal: function ()
{
var that = this;
var dt = this.s.dt;
var columnDefs = [];
for( var i = 0; i < dt.context[0].aoColumns.length; i++ )
{
columnDefs.push({ title: dt.context[0].aoColumns[i].sTitle })
}
var adata = dt.rows({
selected: true
});
var data = "";
data += "";
$('#altEditor-modal').on('show.bs.modal', function() {
$('#altEditor-modal').find('.modal-title').html('Delete Record');
$('#altEditor-modal').find('.modal-body').html('
' + data + '
');
$('#altEditor-modal').find('.modal-footer').html("\
");
});
$('#altEditor-modal').modal('show');
$('#altEditor-modal input[0]').focus();
},
_deleteRow: function( )
{
var that = this;
var dt = this.s.dt;
$('#altEditor-modal .modal-body .alert').remove();
var message = '
\
Success! This record has been deleted.\
';
$('#altEditor-modal .modal-body').append(message);
dt.row({ selected:true }).remove();
dt.draw();
},
/**
* Open Add Modal for selected row
*
* @private
*/
_openAddModal: function ( )
{
var that = this;
var dt = this.s.dt;
var columnDefs = [];
for( var i = 0; i < dt.context[0].aoColumns.length; i++ )
{
columnDefs.push({ title: dt.context[0].aoColumns[i].sTitle })
}
var data = "";
data += "";
$('#altEditor-modal').on('show.bs.modal', function() {
$('#altEditor-modal').find('.modal-title').html('Add Record');
$('#altEditor-modal').find('.modal-body').html('
' + data + '
');
$('#altEditor-modal').find('.modal-footer').html("\
");
});
$('#altEditor-modal').modal('show');
$('#altEditor-modal input[0]').focus();
},
_addRowData: function()
{
console.log('add row')
var that = this;
var dt = this.s.dt;
var data = [];
$('form[name="altEditor-form"] input').each(function( i )
{
data.push( $(this).val() );
});
$('#altEditor-modal .modal-body .alert').remove();
var message = '
\
Success! This record has been added.\
';
$('#altEditor-modal .modal-body').append(message);
dt.row.add(data).draw(false);
},
_getExecutionLocationFolder: function() {
var fileName = "dataTables.altEditor.js";
var scriptList = $("script[src]");
var jsFileObject = $.grep(scriptList, function(el) {
if(el.src.indexOf(fileName) !== -1 )
{
return el;
}
});
var jsFilePath = jsFileObject[0].src;
var jsFileDirectory = jsFilePath.substring(0, jsFilePath.lastIndexOf("/") + 1);
return jsFileDirectory;
}
} );
/**
* altEditor version
*
* @static
* @type String
*/
altEditor.version = '1.0';
/**
* altEditor defaults
*
* @namespace
*/
altEditor.defaults = {
/** @type {Boolean} Ask user what they want to do, even for a single option */
alwaysAsk: false,
/** @type {string|null} What will trigger a focus */
focus: null, // focus, click, hover
/** @type {column-selector} Columns to provide auto fill for */
columns: '', // all
/** @type {boolean|null} Update the cells after a drag */
update: null, // false is editor given, true otherwise
/** @type {DataTable.Editor} Editor instance for automatic submission */
editor: null
};
/**
* Classes used by altEditor that are configurable
*
* @namespace
*/
altEditor.classes = {
/** @type {String} Class used by the selection button */
btn: 'btn'
};
// Attach a listener to the document which listens for DataTables initialisation
// events so we can automatically initialise
$(document).on( 'preInit.dt.altEditor', function (e, settings, json) {
if ( e.namespace !== 'dt' ) {
return;
}
var init = settings.oInit.altEditor;
var defaults = DataTable.defaults.altEditor;
if ( init || defaults ) {
var opts = $.extend( {}, init, defaults );
if ( init !== false ) {
new altEditor( settings, opts );
}
}
} );
// Alias for access
DataTable.altEditor = altEditor;
return altEditor;
}));