Skip to content

Commit 4f4e658

Browse files
author
dtbuild
committed
927745f3bd0ed891a69e8c006d0de368bf115a85 New: Buttons with a split can now be enabled and disabled like normal buttons
New: Split button will disable automatically if there are no enabled buttons inside it, and then enable itself if a button is enabled (e.g. from an action such as selecting a row in the table). 7db9c8c69204a0391179fa7bcac67116df10a470 Fix: Clicking on a button from a split list will now cause the collection to hide. 70c236b07d2fb7c41887b178e59c10c3258fe9ab Merge branch 'master' of github.com:DataTables/Buttons Sync to source repo @70c236b07d2fb7c41887b178e59c10c3258fe9ab
1 parent dc05fc0 commit 4f4e658

5 files changed

+169
-15
lines changed

datatables.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
],
2626
"src-repo": "http://github.com/DataTables/Buttons",
2727
"last-tag": "3.1.2",
28-
"last-sync": "822bb21ef766a967c1d99819d00aa273d2001b30"
28+
"last-sync": "70c236b07d2fb7c41887b178e59c10c3258fe9ab"
2929
}

js/dataTables.buttons.js

+83-6
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,20 @@ $.extend(Buttons.prototype, {
302302
disable: function (node) {
303303
var button = this._nodeToButton(node);
304304

305-
$(button.node)
306-
.addClass(this.c.dom.button.disabled)
307-
.prop('disabled', true);
305+
if (button.isSplit) {
306+
$(button.node.childNodes[0])
307+
.addClass(this.c.dom.button.disabled)
308+
.prop('disabled', true);
309+
}
310+
else {
311+
$(button.node)
312+
.addClass(this.c.dom.button.disabled)
313+
.prop('disabled', true);
314+
}
315+
316+
button.disabled = true;
317+
318+
this._checkSplitEnable();
308319

309320
return this;
310321
},
@@ -355,9 +366,21 @@ $.extend(Buttons.prototype, {
355366
}
356367

357368
var button = this._nodeToButton(node);
358-
$(button.node)
359-
.removeClass(this.c.dom.button.disabled)
360-
.prop('disabled', false);
369+
370+
if (button.isSplit) {
371+
$(button.node.childNodes[0])
372+
.removeClass(this.c.dom.button.disabled)
373+
.prop('disabled', false);
374+
}
375+
else {
376+
$(button.node)
377+
.removeClass(this.c.dom.button.disabled)
378+
.prop('disabled', false);
379+
}
380+
381+
button.disabled = false;
382+
383+
this._checkSplitEnable();
361384

362385
return this;
363386
},
@@ -946,6 +969,7 @@ $.extend(Buttons.prototype, {
946969
.append(button);
947970

948971
var dropButtonConfig = $.extend(config, {
972+
autoClose: true,
949973
align: dropdownConf.dropdown.align,
950974
attr: {
951975
'aria-haspopup': 'dialog',
@@ -1024,6 +1048,57 @@ $.extend(Buttons.prototype, {
10241048
};
10251049
},
10261050

1051+
/**
1052+
* Spin over buttons checking if splits should be enabled or not.
1053+
* @param {*} buttons Array of buttons to check
1054+
*/
1055+
_checkSplitEnable: function (buttons) {
1056+
if (! buttons) {
1057+
buttons = this.s.buttons;
1058+
}
1059+
1060+
for (var i=0 ; i<buttons.length ; i++) {
1061+
var button = buttons[i];
1062+
1063+
// Check if the button is a split one and if so, determine
1064+
// its state
1065+
if (button.isSplit) {
1066+
var splitBtn = button.node.childNodes[1];
1067+
1068+
if (this._checkAnyEnabled(button.buttons)) {
1069+
// Enable the split
1070+
$(splitBtn)
1071+
.removeClass(this.c.dom.button.disabled)
1072+
.prop('disabled', false);
1073+
}
1074+
else {
1075+
$(splitBtn)
1076+
.addClass(this.c.dom.button.disabled)
1077+
.prop('disabled', false);
1078+
}
1079+
}
1080+
else if (button.isCollection) {
1081+
// Nest down into collections
1082+
this._checkSplitEnable(button.buttons);
1083+
}
1084+
}
1085+
},
1086+
1087+
/**
1088+
* Check an array of buttons and see if any are enabled in it
1089+
* @param {*} buttons Button array
1090+
* @returns true if a button is enabled, false otherwise
1091+
*/
1092+
_checkAnyEnabled: function (buttons) {
1093+
for (var i=0 ; i<buttons.length ; i++) {
1094+
if (! buttons[i].disabled) {
1095+
return true;
1096+
}
1097+
}
1098+
1099+
return false;
1100+
},
1101+
10271102
/**
10281103
* Get the button object from a node (recursive)
10291104
* @param {node} node Button node
@@ -1304,6 +1379,8 @@ $.extend(Buttons.prototype, {
13041379
inOpts
13051380
);
13061381

1382+
console.log(inOpts, options);
1383+
13071384
var containerSelector =
13081385
options.tag + '.' + options.containerClassName.replace(/ /g, '.');
13091386
var hostNode = hostButton.node();

js/dataTables.buttons.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/dataTables.buttons.min.mjs

+1-1
Large diffs are not rendered by default.

js/dataTables.buttons.mjs

+83-6
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,20 @@ $.extend(Buttons.prototype, {
262262
disable: function (node) {
263263
var button = this._nodeToButton(node);
264264

265-
$(button.node)
266-
.addClass(this.c.dom.button.disabled)
267-
.prop('disabled', true);
265+
if (button.isSplit) {
266+
$(button.node.childNodes[0])
267+
.addClass(this.c.dom.button.disabled)
268+
.prop('disabled', true);
269+
}
270+
else {
271+
$(button.node)
272+
.addClass(this.c.dom.button.disabled)
273+
.prop('disabled', true);
274+
}
275+
276+
button.disabled = true;
277+
278+
this._checkSplitEnable();
268279

269280
return this;
270281
},
@@ -315,9 +326,21 @@ $.extend(Buttons.prototype, {
315326
}
316327

317328
var button = this._nodeToButton(node);
318-
$(button.node)
319-
.removeClass(this.c.dom.button.disabled)
320-
.prop('disabled', false);
329+
330+
if (button.isSplit) {
331+
$(button.node.childNodes[0])
332+
.removeClass(this.c.dom.button.disabled)
333+
.prop('disabled', false);
334+
}
335+
else {
336+
$(button.node)
337+
.removeClass(this.c.dom.button.disabled)
338+
.prop('disabled', false);
339+
}
340+
341+
button.disabled = false;
342+
343+
this._checkSplitEnable();
321344

322345
return this;
323346
},
@@ -906,6 +929,7 @@ $.extend(Buttons.prototype, {
906929
.append(button);
907930

908931
var dropButtonConfig = $.extend(config, {
932+
autoClose: true,
909933
align: dropdownConf.dropdown.align,
910934
attr: {
911935
'aria-haspopup': 'dialog',
@@ -984,6 +1008,57 @@ $.extend(Buttons.prototype, {
9841008
};
9851009
},
9861010

1011+
/**
1012+
* Spin over buttons checking if splits should be enabled or not.
1013+
* @param {*} buttons Array of buttons to check
1014+
*/
1015+
_checkSplitEnable: function (buttons) {
1016+
if (! buttons) {
1017+
buttons = this.s.buttons;
1018+
}
1019+
1020+
for (var i=0 ; i<buttons.length ; i++) {
1021+
var button = buttons[i];
1022+
1023+
// Check if the button is a split one and if so, determine
1024+
// its state
1025+
if (button.isSplit) {
1026+
var splitBtn = button.node.childNodes[1];
1027+
1028+
if (this._checkAnyEnabled(button.buttons)) {
1029+
// Enable the split
1030+
$(splitBtn)
1031+
.removeClass(this.c.dom.button.disabled)
1032+
.prop('disabled', false);
1033+
}
1034+
else {
1035+
$(splitBtn)
1036+
.addClass(this.c.dom.button.disabled)
1037+
.prop('disabled', false);
1038+
}
1039+
}
1040+
else if (button.isCollection) {
1041+
// Nest down into collections
1042+
this._checkSplitEnable(button.buttons);
1043+
}
1044+
}
1045+
},
1046+
1047+
/**
1048+
* Check an array of buttons and see if any are enabled in it
1049+
* @param {*} buttons Button array
1050+
* @returns true if a button is enabled, false otherwise
1051+
*/
1052+
_checkAnyEnabled: function (buttons) {
1053+
for (var i=0 ; i<buttons.length ; i++) {
1054+
if (! buttons[i].disabled) {
1055+
return true;
1056+
}
1057+
}
1058+
1059+
return false;
1060+
},
1061+
9871062
/**
9881063
* Get the button object from a node (recursive)
9891064
* @param {node} node Button node
@@ -1264,6 +1339,8 @@ $.extend(Buttons.prototype, {
12641339
inOpts
12651340
);
12661341

1342+
console.log(inOpts, options);
1343+
12671344
var containerSelector =
12681345
options.tag + '.' + options.containerClassName.replace(/ /g, '.');
12691346
var hostNode = hostButton.node();

0 commit comments

Comments
 (0)