Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

editor.select2.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * [Select2](http://ivaynberg.github.io/select2) is a replacement for HTML
  3. * `-tag select` elements, enhancing standard `-tag select`'s with searching,
  4. * remote data sets and more. This plug-in provides the ability to use Select2
  5. * with Editor very easily.
  6. *
  7. * Note for when using Select2's remote Ajax data source option: In order to have
  8. * the label correctly render for an item being edited, the server-side script
  9. * that is responding to the Select2 requests must be able to accept a request
  10. * with the parameters: `initialValue:true` and `value:...` (i.e. the value). The
  11. * server _must_ respond with `{ "id": value, "text": "Label to show" }`.
  12. *
  13. * @name Select2
  14. * @summary Use the Select2 library with Editor for complex select input options.
  15. * @requires [Select2](http://ivaynberg.github.io/select2)
  16. * @depcss //cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css
  17. * @depjs //cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js
  18. *
  19. * @opt `e-type object` **`options`**: - The values and labels to be used in the Select2 list. This can be given in a number of different forms:
  20. * * `e-type object` - Name / value pairs, where the property name is used as the label and the value as the field value. For example: `{ "Edinburgh": 51, "London": 76, ... }`.
  21. * * `e-type array` - An array of objects with the properties `label` and `value` defined. For example: `[ { label: "Edinburgh", value: 51 }, { label: "London", value: 76 } ]`.
  22. * * `e-type array` - An array of values (e.g. numbers, strings etc). Each element in the array will be used for both the label and the value. For example: `[ "Edinburgh", "London" ]`.
  23. * @opt `e-type object` **`optionsPair`**: If `options` is given as an array of objects, by default
  24. * Editor will read the information for the label and value properties of the select input
  25. * from the `label` and `value` properties of the data source object. This option provides the
  26. * ability to alter those parameters by giving it as an object containing the properties `label
  27. * and `value` itself, the values of which indicate the properties that should be read from the
  28. * data source object. For example you might use `{ label: 'name', value: 'id' }`.
  29. * @opt `e-type object` **`opts`**: Select2 initialisation options object.
  30. * Please refer to the Select2 documentation for the full range
  31. * of options available.
  32. * @opt `e-type object` **`attr`**: Attributes that are applied to the
  33. * `-tag select` element before Select2 is initialised
  34. * @opt `-type string` - **`separator`** when the `multiple` and `tags` options are used for Select2 (`opts` parameter) this can be used to use a string value to represent the multiple values that are selected. The character given for this parameter is the separator character that will be used.
  35. * @opt `-type string` - **`onFocus`**: Action to take when the Select2 field is focused. This can be `open` or `undefined`. If `open` the dropdown list will automatically show when the field is focused.
  36. * @pot `-type string` - **`urlDataType`**: Format in which to send the data for an Ajax initial request (if required). Can be `json` (default) or `param` to have the value encoded with `jQuery.param()`.
  37. *
  38. * @method **`inst`**: Execute a Select2 method, using the arguments given. The
  39. * return value is that returned by the Select2 method. For example you could
  40. * use `editor.field('priority').inst('val')` to get the value from Select2
  41. * directly.
  42. * @method **`update`**: Update the list of options that are available in the
  43. * Select2 list. This uses the same format as `options` for the
  44. * initialisation.
  45. *
  46. * @example
  47. * // Create an Editor instance with a Select2 field and data
  48. * new $.fn.dataTable.Editor( {
  49. * "ajax": "php/todo.php",
  50. * "table": "#example",
  51. * "fields": [ {
  52. * "label": "Item:",
  53. * "name": "item"
  54. * }, {
  55. * "label": "Priority:",
  56. * "name": "priority",
  57. * "type": "select2",
  58. * "options": [
  59. * { "label": "1 (highest)", "value": "1" },
  60. * { "label": "2", "value": "2" },
  61. * { "label": "3", "value": "3" },
  62. * { "label": "4", "value": "4" },
  63. * { "label": "5 (lowest)", "value": "5" }
  64. * ]
  65. * }, {
  66. * "label": "Status:",
  67. * "name": "status",
  68. * "type": "radio",
  69. * "default": "Done",
  70. * "options": [
  71. * { "label": "To do", "value": "To do" },
  72. * { "label": "Done", "value": "Done" }
  73. * ]
  74. * }
  75. * ]
  76. * } );
  77. *
  78. * @example
  79. * // Add a Select2 field to Editor with Select2 options set
  80. * editor.add( {
  81. * "label": "State:",
  82. * "name": "state",
  83. * "type": "select2",
  84. * "opts": {
  85. * "placeholder": "Select State",
  86. * "allowClear": true
  87. * }
  88. * } );
  89. *
  90. */
  91. (function( factory ){
  92. if ( typeof define === 'function' && define.amd ) {
  93. // AMD
  94. define( ['jquery', 'datatables.net', 'datatables.net-editor'], factory );
  95. }
  96. else if ( typeof exports === 'object' ) {
  97. // Node / CommonJS
  98. module.exports = function ($, dt) {
  99. if ( ! $ ) { $ = require('jquery'); }
  100. factory( $, dt || $.fn.dataTable || require('datatables') );
  101. };
  102. }
  103. else if ( jQuery ) {
  104. // Browser standard
  105. factory( jQuery, jQuery.fn.dataTable );
  106. }
  107. }(function( $, DataTable ) {
  108. 'use strict';
  109. if ( ! DataTable.ext.editorFields ) {
  110. DataTable.ext.editorFields = {};
  111. }
  112. var _fieldTypes = DataTable.Editor ?
  113. DataTable.Editor.fieldTypes :
  114. DataTable.ext.editorFields;
  115. _fieldTypes.select2 = {
  116. _addOptions: function ( conf, opts ) {
  117. var elOpts = conf._input[0].options;
  118. elOpts.length = 0;
  119. if ( opts ) {
  120. DataTable.Editor.pairs( opts, conf.optionsPair, function ( val, label, i ) {
  121. elOpts[i] = new Option( label, val );
  122. } );
  123. }
  124. },
  125. create: function ( conf ) {
  126. conf._input = $('<select/>')
  127. .attr( $.extend( {
  128. id: DataTable.Editor.safeId( conf.id )
  129. }, conf.attr || {} ) );
  130. var options = $.extend( {
  131. width: '100%'
  132. }, conf.opts );
  133. _fieldTypes.select2._addOptions( conf, conf.options || conf.ipOpts );
  134. conf._input.select2( options );
  135. var open;
  136. conf._input
  137. .on( 'select2:open', function () {
  138. open = true;
  139. } )
  140. .on( 'select2:close', function () {
  141. open = false;
  142. } );
  143. // On open, need to have the instance update now that it is in the DOM
  144. this.one( 'open.select2-'+DataTable.Editor.safeId( conf.id ), function () {
  145. conf._input.select2( options );
  146. if ( open ) {
  147. conf._input.select2( 'open' );
  148. }
  149. } );
  150. return conf._input[0];
  151. },
  152. get: function ( conf ) {
  153. var val = conf._input.val();
  154. val = conf._input.prop('multiple') && val === null ?
  155. [] :
  156. val;
  157. return conf.separator ?
  158. val.join( conf.separator ) :
  159. val;
  160. },
  161. set: function ( conf, val ) {
  162. if ( conf.separator && ! $.isArray( val ) ) {
  163. val = val.split( conf.separator );
  164. }
  165. // Clear out any existing tags
  166. if ( conf.opts && conf.opts.tags ) {
  167. conf._input.val([]);
  168. }
  169. // The value isn't present in the current options list, so we need to add it
  170. // in order to be able to select it. Note that this makes the set action async!
  171. // It doesn't appear to be possible to add an option to select2, then change
  172. // its label and update the display
  173. var needAjax = false;
  174. if ( conf.opts && conf.opts.ajax ) {
  175. if ( $.isArray( val ) ) {
  176. for ( var i=0, ien=val.length ; i<ien ; i++ ) {
  177. if ( conf._input.find('option[value="'+val[i]+'"]').length === 0 ) {
  178. needAjax = true;
  179. break;
  180. }
  181. }
  182. }
  183. else {
  184. if ( conf._input.find('option[value="'+val+'"]').length === 0 ) {
  185. needAjax = true;
  186. }
  187. }
  188. }
  189. if ( needAjax && val ) {
  190. $.ajax( $.extend( {
  191. beforeSend: function ( jqXhr, settings ) {
  192. // Add an initial data request to the server, but don't
  193. // override `data` since the dev might be using that
  194. var initData = conf.urlDataType === undefined || conf.urlDataType === 'json'
  195. ? 'initialValue=true&value='+JSON.stringify(val)
  196. : $.param({initialValue: true, value: val});
  197. if ( typeof conf.opts.ajax.url === 'function' ) {
  198. settings.url = conf.opts.ajax.url();
  199. }
  200. if ( settings.type === 'GET' ) {
  201. settings.url += settings.url.indexOf('?') === -1 ?
  202. '?'+initData :
  203. '&'+initData;
  204. }
  205. else {
  206. settings.data = settings.data ?
  207. settings.data +'&'+ initData :
  208. initData;
  209. }
  210. },
  211. success: function ( json ) {
  212. var addOption = function ( option ) {
  213. if ( conf._input.find('option[value="'+option.id+'"]').length === 0 ) {
  214. $('<option/>')
  215. .attr('value', option.id)
  216. .text( option.text )
  217. .appendTo( conf._input );
  218. }
  219. }
  220. if ( $.isArray( json ) ) {
  221. for ( var i=0, ien=json.length ; i<ien ; i++ ) {
  222. addOption( json[i] );
  223. }
  224. }
  225. else if ( json.results && $.isArray( json.results ) ) {
  226. for ( var i=0, ien=json.results.length ; i<ien ; i++ ) {
  227. addOption( json.results[i] );
  228. }
  229. }
  230. else {
  231. addOption( json );
  232. }
  233. conf._input
  234. .val( val )
  235. .trigger( 'change', {editor: true} );
  236. }
  237. }, conf.opts.ajax ) );
  238. }
  239. else {
  240. conf._input
  241. .val( val )
  242. .trigger( 'change', {editor: true} );
  243. }
  244. },
  245. enable: function ( conf ) {
  246. $(conf._input).removeAttr( 'disabled' );
  247. },
  248. disable: function ( conf ) {
  249. $(conf._input).attr( 'disabled', 'disabled' );
  250. },
  251. // Non-standard Editor methods - custom to this plug-in
  252. inst: function ( conf ) {
  253. var args = Array.prototype.slice.call( arguments );
  254. args.shift();
  255. return conf._input.select2.apply( conf._input, args );
  256. },
  257. update: function ( conf, data ) {
  258. var val = _fieldTypes.select2.get( conf );
  259. _fieldTypes.select2._addOptions( conf, data );
  260. // Restore null value if it was, to let the placeholder show
  261. if ( val === null ) {
  262. _fieldTypes.select2.set( conf, null );
  263. }
  264. $(conf._input).trigger('change', {editor: true} );
  265. },
  266. focus: function ( conf ) {
  267. if ( conf._input.is(':visible') && conf.onFocus === 'focus' ) {
  268. conf._input.select2('open');
  269. }
  270. },
  271. owns: function ( conf, node ) {
  272. if ( $(node).closest('.select2-container').length || $(node).closest('.select2').length || $(node).hasClass('select2-selection__choice__remove') ) {
  273. return true;
  274. }
  275. return false;
  276. },
  277. canReturnSubmit: function() {
  278. return false;
  279. }
  280. };
  281. }));