Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.19 KB | None | 0 0
  1. /*! CellEdit 1.0.19
  2. * ©2016 Elliott Beaty - datatables.net/license
  3. */
  4.  
  5. /**
  6. * @summary CellEdit
  7. * @description Make a cell editable when clicked upon
  8. * @version 1.0.19
  9. * @file dataTables.editCell.js
  10. * @author Elliott Beaty
  11. * @contact elliott@elliottbeaty.com
  12. * @copyright Copyright 2016 Elliott Beaty
  13. *
  14. * This source file is free software, available under the following license:
  15. * MIT license - http://datatables.net/license/mit
  16. *
  17. * This source file is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  19. * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
  20. *
  21. * For details please refer to: http://www.datatables.net
  22. */
  23.  
  24. jQuery.fn.dataTable.Api.register('MakeCellsEditable()', function (settings) {
  25. var table = this.table();
  26.  
  27. jQuery.fn.extend({
  28. // UPDATE
  29. updateEditableCell: function (callingElement) {
  30. // Need to redeclare table here for situations where we have more than one datatable on the page. See issue6 on github
  31. var table = $(callingElement).closest("table").DataTable().table();
  32. var row = table.row($(callingElement).parents('tr'));
  33. var cell = table.cell($(callingElement).parents('td, th'));
  34. var columnIndex = cell.index().column;
  35. var inputField =getInputField(callingElement);
  36.  
  37. // Update
  38. var newValue = inputField.val();
  39. if (!newValue && ((settings.allowNulls) && settings.allowNulls != true)) {
  40. // If columns specified
  41. if (settings.allowNulls.columns) {
  42. // If current column allows nulls
  43. if (settings.allowNulls.columns.indexOf(columnIndex) > -1) {
  44. _update(newValue);
  45. } else {
  46. _addValidationCss();
  47. }
  48. // No columns allow null
  49. } else if (!newValue) {
  50. _addValidationCss();
  51. }
  52. //All columns allow null
  53. } else if (newValue && settings.onValidate) {
  54. if (settings.onValidate(cell, row, newValue)) {
  55. _update(newValue);
  56. } else {
  57. _addValidationCss();
  58. }
  59. }
  60. else {
  61. _update(newValue);
  62. }
  63. function _addValidationCss() {
  64. // Show validation error
  65. if (settings.allowNulls.errorClass) {
  66. $(inputField).addClass(settings.allowNulls.errorClass);
  67. } else {
  68. $(inputField).css({ "border": "red solid 1px" });
  69. }
  70. }
  71. function _update(newValue) {
  72. var oldValue = cell.data();
  73. cell.data(newValue);
  74. //Return cell & row.
  75. settings.onUpdate(cell, row, oldValue);
  76. }
  77. // Get current page
  78. var currentPageIndex = table.page.info().page;
  79.  
  80. //Redraw table
  81. table.page(currentPageIndex).draw(false);
  82. },
  83. // CANCEL
  84. cancelEditableCell: function (callingElement) {
  85. var table = $(callingElement.closest("table")).DataTable().table();
  86. var cell = table.cell($(callingElement).parents('td, th'));
  87. // Set cell to it's original value
  88. cell.data(cell.data());
  89.  
  90. // Redraw table
  91. table.draw();
  92. }
  93. });
  94.  
  95. // Destroy
  96. if (settings === "destroy") {
  97. $(table.body()).off("click", "td");
  98. table = null;
  99. }
  100.  
  101. if (table != null) {
  102. // On cell click
  103. $(table.body()).on('click', 'td', function () {
  104.  
  105. var currentColumnIndex = table.cell(this).index().column;
  106.  
  107. // DETERMINE WHAT COLUMNS CAN BE EDITED
  108. if ((settings.columns && settings.columns.indexOf(currentColumnIndex) > -1) || (!settings.columns)) {
  109. var row = table.row($(this).parents('tr'));
  110. editableCellsRow = row;
  111.  
  112. var cell = table.cell(this).node();
  113. var oldValue = table.cell(this).data();
  114. // Sanitize value
  115. oldValue = sanitizeCellValue(oldValue);
  116.  
  117. // Show input
  118. if (!$(cell).find('input').length && !$(cell).find('select').length && !$(cell).find('textarea').length) {
  119. // Input CSS
  120. var input = getInputHtml(currentColumnIndex, settings, oldValue);
  121. $(cell).html(input.html);
  122. if (input.focus) {
  123. $('#ejbeatycelledit').focus();
  124. }
  125. }
  126. }
  127. });
  128. }
  129.  
  130. });
  131.  
  132. function getInputHtml(currentColumnIndex, settings, oldValue) {
  133. var inputSetting, inputType, input, inputCss, confirmCss, cancelCss, startWrapperHtml = '', endWrapperHtml = '', listenToKeys = false;
  134.  
  135. input = {"focus":true,"html":null};
  136.  
  137. if(settings.inputTypes){
  138. $.each(settings.inputTypes, function (index, setting) {
  139. if (setting.column == currentColumnIndex) {
  140. inputSetting = setting;
  141. inputType = inputSetting.type.toLowerCase();
  142. }
  143. });
  144. }
  145.  
  146. if (settings.inputCss) { inputCss = settings.inputCss; }
  147. if (settings.wrapperHtml) {
  148. var elements = settings.wrapperHtml.split('{content}');
  149. if (elements.length === 2) {
  150. startWrapperHtml = elements[0];
  151. endWrapperHtml = elements[1];
  152. }
  153. }
  154.  
  155. if (settings.confirmationButton) {
  156. if (settings.confirmationButton.listenToKeys) { listenToKeys = settings.confirmationButton.listenToKeys; }
  157. confirmCss = settings.confirmationButton.confirmCss;
  158. cancelCss = settings.confirmationButton.cancelCss;
  159. inputType = inputType + "-confirm";
  160. }
  161. switch (inputType) {
  162. case "list":
  163. input.html = startWrapperHtml + "<select class='" + inputCss + "' onchange='$(this).updateEditableCell(this);'>";
  164. $.each(inputSetting.options, function (index, option) {
  165. if (oldValue == option.value) {
  166. input.html = input.html + "<option value='" + option.value + "' selected>" + option.display + "</option>"
  167. } else {
  168. input.html = input.html + "<option value='" + option.value + "' >" + option.display + "</option>"
  169. }
  170. });
  171. input.html = input.html + "</select>" + endWrapperHtml;
  172. input.focus = false;
  173. break;
  174. case "list-confirm": // List w/ confirm
  175. input.html = startWrapperHtml + "<select class='" + inputCss + "'>";
  176. $.each(inputSetting.options, function (index, option) {
  177. if (oldValue == option.value) {
  178. input.html = input.html + "<option value='" + option.value + "' selected>" + option.display + "</option>"
  179. } else {
  180. input.html = input.html + "<option value='" + option.value + "' >" + option.display + "</option>"
  181. }
  182. });
  183. input.html = input.html + "</select>&nbsp;<a href='javascript:void(0);' class='" + confirmCss + "' onclick='$(this).updateEditableCell(this);'>Confirm</a> <a href='javascript:void(0);' class='" + cancelCss + "' onclick='$(this).cancelEditableCell(this)'>Cancel</a>" + endWrapperHtml;
  184. input.focus = false;
  185. break;
  186. case "datepicker": //Both datepicker options work best when confirming the values
  187. case "datepicker-confirm":
  188. // Makesure jQuery UI is loaded on the page
  189. if (typeof jQuery.ui == 'undefined') {
  190. alert("jQuery UI is required for the DatePicker control but it is not loaded on the page!");
  191. break;
  192. }
  193. jQuery(".datepick").datepicker("destroy");
  194. input.html = startWrapperHtml + "<input id='ejbeatycelledit' type='text' name='date' class='datepick " + inputCss + "' value='" + oldValue + "'></input> &nbsp;<a href='javascript:void(0);' class='" + confirmCss + "' onclick='$(this).updateEditableCell(this)'>Confirm</a> <a href='javascript:void(0);' class='" + cancelCss + "' onclick='$(this).cancelEditableCell(this)'>Cancel</a>" + endWrapperHtml;
  195. setTimeout(function () { //Set timeout to allow the script to write the input.html before triggering the datepicker
  196. var icon = "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif";
  197. // Allow the user to provide icon
  198. if (typeof inputSetting.options !== 'undefined' && typeof inputSetting.options.icon !== 'undefined') {
  199. icon = inputSetting.options.icon;
  200. }
  201. var self = jQuery('.datepick').datepicker(
  202. {
  203. showOn: "button",
  204. buttonImage: icon,
  205. buttonImageOnly: true,
  206. buttonText: "Select date"
  207. });
  208. },100);
  209. break;
  210. case "text-confirm": // text input w/ confirm
  211. input.html = startWrapperHtml + "<input id='ejbeatycelledit' class='" + inputCss + "' value='"+oldValue+"'" + (listenToKeys ? " onkeyup='if(event.keyCode==13) {$(this).updateEditableCell(this);} else if (event.keyCode===27) {$(this).cancelEditableCell(this);}'" : "") + "></input>&nbsp;<a href='javascript:void(0);' class='" + confirmCss + "' onclick='$(this).updateEditableCell(this)'>Confirm</a> <a href='javascript:void(0);' class='" + cancelCss + "' onclick='$(this).cancelEditableCell(this)'>Cancel</a>" + endWrapperHtml;
  212. break;
  213. case "undefined-confirm": // text input w/ confirm
  214. input.html = startWrapperHtml + "<input id='ejbeatycelledit' class='" + inputCss + "' value='" + oldValue + "'" + (listenToKeys ? " onkeyup='if(event.keyCode==13) {$(this).updateEditableCell(this);} else if (event.keyCode===27) {$(this).cancelEditableCell(this);}'" : "") + "></input>&nbsp;<a href='javascript:void(0);' class='" + confirmCss + "' onclick='$(this).updateEditableCell(this)'>Confirm</a> <a href='javascript:void(0);' class='" + cancelCss + "' onclick='$(this).cancelEditableCell(this)'>Cancel</a>" + endWrapperHtml;
  215. break;
  216. case "textarea":
  217. case "textarea-confirm":
  218. input.html = startWrapperHtml + "<textarea id='ejbeatycelledit' class='" + inputCss + "'>"+oldValue+"</textarea><a href='javascript:void(0);' class='" + confirmCss + "' onclick='$(this).updateEditableCell(this)'>Confirm</a> <a href='javascript:void(0);' class='" + cancelCss + "' onclick='$(this).cancelEditableCell(this)'>Cancel</a>" + endWrapperHtml;
  219. break;
  220. default: // text input
  221. input.html = startWrapperHtml + "<input id='ejbeatycelledit' class='" + inputCss + "' onfocusout='$(this).updateEditableCell(this)' value='" + oldValue + "'></input>" + endWrapperHtml;
  222. break;
  223. }
  224. return input;
  225. }
  226.  
  227. function getInputField(callingElement) {
  228. // Update datatables cell value
  229. var inputField;
  230. switch ($(callingElement).prop('nodeName').toLowerCase()) {
  231. case 'a': // This means they're using confirmation buttons
  232. if ($(callingElement).siblings('input').length > 0) {
  233. inputField = $(callingElement).siblings('input');
  234. }
  235. if ($(callingElement).siblings('select').length > 0) {
  236. inputField = $(callingElement).siblings('select');
  237. }
  238. if ($(callingElement).siblings('textarea').length > 0) {
  239. inputField = $(callingElement).siblings('textarea');
  240. }
  241. break;
  242. default:
  243. inputField = $(callingElement);
  244. }
  245. return inputField;
  246. }
  247.  
  248. function sanitizeCellValue(cellValue) {
  249. if (typeof (cellValue) === 'undefined' || cellValue === null || cellValue.length < 1) {
  250. return "";
  251. }
  252.  
  253. // If not a number
  254. if (isNaN(cellValue)) {
  255. // escape single quote
  256. cellValue = cellValue.replace(/'/g, "&#39;");
  257. }
  258. return cellValue;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement