Can now be enabled inside the DataTables initialisation Allows caller to handle source for child data via callback Wraps provided child data in a div to allow optional animation (slide up / down) Additional options: animateShow: if true, uses jQuery slideDown() function to animate showing child data. If false, uses jQuery show() animateHide: if true, uses jQuery slideUp() function to animate showing child data. If false, uses jQuery hide()pull/381/head
parent
cfe86c4bbe
commit
74395dfa0d
@ -1,117 +1,206 @@
|
|||||||
/**
|
/**
|
||||||
* @summary SlidingChild
|
* @summary SlidingChild
|
||||||
* @description Show/Hide child data plug-in
|
* @description Show / Hide row child plugin
|
||||||
* @version 1.0.1
|
* @version 2.0.0
|
||||||
* @file datatables.slidingChild.js
|
* @file dataTables.slidingChild.js
|
||||||
* @author datahandler (www.datahandler.uk)
|
* @author Nick Adkinson (https://github.com/data-handler)
|
||||||
* @copyright Copyright 2017 datahandler.
|
* @copyright Copyright 2018 Nick Adkinson
|
||||||
|
*
|
||||||
|
* License MIT - http://datatables.net/license/mit
|
||||||
|
*
|
||||||
|
* This feature plug-in provides functionality for showing and hiding row child
|
||||||
|
* information in DataTables. This can be particularly useful for displaying
|
||||||
|
* hierarchical data as a drill-down, or where you wish to convey more information
|
||||||
|
* about a row than there is space for in the host table.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* $('#myTable').DataTable({
|
||||||
|
* slidingChild: {
|
||||||
|
* source: function(parent, response) {
|
||||||
|
* $.get('/Child/GetByParentId/' + parent.data('id'), response);
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* });
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
(function ($) {
|
(function( factory ){
|
||||||
'use strict';
|
if ( typeof define === 'function' && define.amd ) {
|
||||||
var SlidingChild = function (dt, options) {
|
// AMD
|
||||||
var opts = $.extend({}, SlidingChild.defaults, options);
|
define( ['jquery', 'datatables.net'], function ( $ ) {
|
||||||
|
return factory( $, window, document );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
else if ( typeof exports === 'object' ) {
|
||||||
|
// CommonJS
|
||||||
|
module.exports = function (root, $) {
|
||||||
|
if ( ! root ) {
|
||||||
|
root = window;
|
||||||
|
}
|
||||||
|
|
||||||
// bind to selector click
|
if ( ! $ || ! $.fn.dataTable ) {
|
||||||
$(dt.table().node(), '> tbody').on('click', opts.selector, function() {
|
$ = require('datatables.net')(root, $).$;
|
||||||
|
}
|
||||||
|
|
||||||
|
return factory( $, root, root.document );
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Browser
|
||||||
|
factory( jQuery, document );
|
||||||
|
}
|
||||||
|
}(function( $, document ) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var SlidingChild = function ( dt, options )
|
||||||
|
{
|
||||||
|
var table = dt.table();
|
||||||
|
var sliderElement = document.createElement('div');
|
||||||
|
sliderElement.className = 'slider';
|
||||||
|
|
||||||
|
this.s = $.extend({},
|
||||||
|
{
|
||||||
|
dt: dt,
|
||||||
|
table: $(table.node()),
|
||||||
|
slider: $(sliderElement)
|
||||||
|
},
|
||||||
|
SlidingChild.defaults,
|
||||||
|
options
|
||||||
|
);
|
||||||
|
|
||||||
|
this._bind();
|
||||||
|
};
|
||||||
|
|
||||||
|
SlidingChild.prototype = {
|
||||||
|
_bind: function() {
|
||||||
|
var that = this;
|
||||||
|
var settings = that.s;
|
||||||
|
$(settings.table, '> tbody').on('click', settings.selector, function() {
|
||||||
var $this = $(this);
|
var $this = $(this);
|
||||||
var tr = $this.is('tr') ? $this : $this.closest('tr');
|
var tr = $this.is('tr') ? $this : $this.closest('tr');
|
||||||
|
|
||||||
if (!tr.is('tr')) { return; } // throw error?
|
if (!tr.is('tr')) { return; } // throw error?
|
||||||
|
|
||||||
var row = dt.row(tr);
|
var dtRow = settings.dt.row(tr);
|
||||||
toggleChild(row);
|
settings.source( tr, that._response(dtRow) );
|
||||||
});
|
});
|
||||||
|
},
|
||||||
function toggleChild (dtRow) {
|
_response: function(dtRow) {
|
||||||
if (dtRow.child.isShown()) {
|
return function( dtRow, childData ) {
|
||||||
closeChild(dtRow);
|
this._toggleChild( dtRow, childData );
|
||||||
|
}.bind( this, dtRow );
|
||||||
|
},
|
||||||
|
_toggleChild: function(dtRow, childData) {
|
||||||
|
var that = this;
|
||||||
|
if (dtRow.child.isShown()) {
|
||||||
|
that._hideChild(dtRow, function() {});
|
||||||
|
} else {
|
||||||
|
var settings = that.s;
|
||||||
|
var existingShownDtRow = settings.dt.row('.shown');
|
||||||
|
if (existingShownDtRow.length && settings.toggle) {
|
||||||
|
that._hideChild(existingShownDtRow, that._showChild(dtRow, childData));
|
||||||
|
} else {
|
||||||
|
that.__showChild(dtRow, childData);
|
||||||
}
|
}
|
||||||
else {
|
}
|
||||||
var existingShownDtRow = dt.row('.shown');
|
},
|
||||||
if (existingShownDtRow.length && opts.toggleChild) {
|
_showChild: function(dtRow, data) {
|
||||||
closeChild(existingShownDtRow);
|
return function( dtRow, childData ) {
|
||||||
}
|
this.__showChild( dtRow, childData );
|
||||||
|
}.bind( this, dtRow, data );
|
||||||
|
},
|
||||||
|
__showChild: function(dtRow, data) {
|
||||||
|
var settings = this.s;
|
||||||
|
var slider = settings.slider;
|
||||||
|
|
||||||
showChildData(dtRow);
|
slider.append(data);
|
||||||
}
|
dtRow.child(slider, settings.childClass).show();
|
||||||
}
|
|
||||||
// code borrowed from the resource at: https://datatables.net/blog/2014-10-02
|
|
||||||
function closeChild (dtRow) {
|
|
||||||
var showingRow = $(dtRow.node());
|
|
||||||
$(opts.sliderSelector, dtRow.child()).slideUp(opts.animationSpeed, function () {
|
|
||||||
dtRow.child.remove();
|
|
||||||
showingRow.removeClass('shown');
|
|
||||||
if (opts.onHidden !== null) {
|
|
||||||
opts.onHidden(dtRow);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function showChildData (dtRow) {
|
if (settings.animateShow) {
|
||||||
if (opts.ajax.requestUrl === null) {
|
this._showChildAnimation(dtRow);
|
||||||
showChildDataFromRow(dtRow);
|
} else {
|
||||||
}
|
this._showChildNoAnimation(dtRow);
|
||||||
else {
|
|
||||||
$.ajax({
|
|
||||||
type: opts.ajax.requestType,
|
|
||||||
url: opts.ajax.requestUrl,
|
|
||||||
beforeSend: function(xhr, settings) {
|
|
||||||
if (opts.ajax.requestDataCallback) {
|
|
||||||
this.data = opts.ajax.requestDataCallback(dtRow);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
contentType: opts.ajax.contentType,
|
|
||||||
dataType: opts.ajax.dataType,
|
|
||||||
success: function (response) {
|
|
||||||
var data = response;
|
|
||||||
if (opts.dataFormatCallback) {
|
|
||||||
data = opts.dataFormatCallback(response);
|
|
||||||
}
|
|
||||||
showChild(dtRow, data);
|
|
||||||
},
|
|
||||||
error: function (response) { showChild(dtRow, response); }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
_showChildAnimation: function(dtRow) {
|
||||||
|
var selectedRow = $(dtRow.node());
|
||||||
|
var settings = this.s;
|
||||||
|
$(settings.slider, dtRow.child()).slideDown(settings.animationSpeed, function () {
|
||||||
|
selectedRow.addClass('shown');
|
||||||
|
settings.onShown(dtRow);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
_showChildNoAnimation: function(dtRow) {
|
||||||
|
var selectedRow = $(dtRow.node());
|
||||||
|
var settings = this.s;
|
||||||
|
$(settings.slider, dtRow.child()).show();
|
||||||
|
selectedRow.addClass('shown');
|
||||||
|
settings.onShown(dtRow);
|
||||||
|
},
|
||||||
|
_hideChild: function(dtRow, callback) {
|
||||||
|
var settings = this.s;
|
||||||
|
|
||||||
function showChildDataFromRow(dtRow) {
|
if (settings.animateHide) {
|
||||||
if (!opts.dataFormatCallback) { return; } // throw error?
|
this._hideChildAnimation(dtRow, callback);
|
||||||
var data = opts.dataFormatCallback(dtRow);
|
} else {
|
||||||
showChild(dtRow, data);
|
this._hideChildNoAnimation(dtRow, callback);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
_hideChildAnimation: function(dtRow, callback) {
|
||||||
|
var settings = this.s;
|
||||||
|
var showingRow = $(dtRow.node());
|
||||||
|
var slider = settings.slider;
|
||||||
|
$(slider, dtRow.child()).slideUp(settings.animationSpeed, function () {
|
||||||
|
dtRow.child.remove();
|
||||||
|
showingRow.removeClass('shown');
|
||||||
|
slider.empty();
|
||||||
|
settings.onHidden(dtRow);
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
_hideChildNoAnimation: function(dtRow, callback) {
|
||||||
|
var settings = this.s;
|
||||||
|
var showingRow = $(dtRow.node());
|
||||||
|
var slider = settings.slider;
|
||||||
|
$(slider, dtRow.child()).hide();
|
||||||
|
dtRow.child.remove();
|
||||||
|
showingRow.removeClass('shown');
|
||||||
|
slider.empty();
|
||||||
|
settings.onHidden(dtRow);
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
function showChild(dtRow, data) {
|
SlidingChild.defaults = {
|
||||||
var selectedRow = $(dtRow.node());
|
selector: "tr",
|
||||||
dtRow.child(data, opts.childClass).show();
|
childClass: 'child',
|
||||||
|
source: function() {},
|
||||||
|
toggle: true,
|
||||||
|
animateShow: true,
|
||||||
|
animateHide: true,
|
||||||
|
animationSpeed: 200,
|
||||||
|
onShown: function() {},
|
||||||
|
onHidden: function() {}
|
||||||
|
};
|
||||||
|
|
||||||
$(opts.sliderSelector, dtRow.child()).slideDown(opts.animationSpeed, function () {
|
|
||||||
selectedRow.addClass('shown');
|
|
||||||
|
|
||||||
if (opts.onShown !== null) {
|
$.fn.dataTable.SlidingChild = SlidingChild;
|
||||||
opts.onShown(dtRow);
|
$.fn.DataTable.SlidingChild = SlidingChild;
|
||||||
}
|
|
||||||
});
|
// Automatic initialisation listener
|
||||||
}
|
$(document).on( 'init.dt', function ( e, settings ) {
|
||||||
};
|
if ( e.namespace !== 'dt' ) {
|
||||||
|
return;
|
||||||
SlidingChild.defaults = {
|
}
|
||||||
selector: "tr",
|
|
||||||
toggleChild: true,
|
var api = new $.fn.dataTable.Api( settings );
|
||||||
animationSpeed: 200,
|
|
||||||
ajax: {
|
if ( $( api.table().node() ).hasClass( 'slidingChild' ) ||
|
||||||
requestType: "GET",
|
settings.oInit.slidingChild ||
|
||||||
requestDataCallback: null,
|
$.fn.dataTable.defaults.slidingChild )
|
||||||
requestUrl: null,
|
{
|
||||||
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
|
new SlidingChild( api, settings.oInit.slidingChild );
|
||||||
dataType: "json"
|
}
|
||||||
},
|
} );
|
||||||
dataFormatCallback: null,
|
|
||||||
sliderSelector: 'div.slider',
|
|
||||||
childClass: null,
|
}));
|
||||||
onShown: null,
|
|
||||||
onHidden: null
|
|
||||||
};
|
|
||||||
|
|
||||||
$.fn.dataTable.SlidingChild = SlidingChild;
|
|
||||||
$.fn.DataTable.SlidingChild = SlidingChild;
|
|
||||||
}(jQuery));
|
|
||||||
|
Loading…
Reference in new issue