diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-05-25 09:30:37 +0300 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-05-25 09:30:37 +0300 |
| commit | a3dc1f0f55003ca2fb325ed2e3f974094f6cad2e (patch) | |
| tree | 09aa5f8208460c5c2883209d7139d59ba2d623e3 /public/app/views/js/select2-supplementary.php | |
| parent | 2b1f4aed56c5bfbf014f3f871e9dcc336b5a31b7 (diff) | |
| download | gyraf1gov-a3dc1f0f55003ca2fb325ed2e3f974094f6cad2e.tar.gz gyraf1gov-a3dc1f0f55003ca2fb325ed2e3f974094f6cad2e.tar.bz2 gyraf1gov-a3dc1f0f55003ca2fb325ed2e3f974094f6cad2e.zip | |
break form scripts in parts; first pdf example
Diffstat (limited to 'public/app/views/js/select2-supplementary.php')
| -rw-r--r-- | public/app/views/js/select2-supplementary.php | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/public/app/views/js/select2-supplementary.php b/public/app/views/js/select2-supplementary.php new file mode 100644 index 0000000..a7aa7e2 --- /dev/null +++ b/public/app/views/js/select2-supplementary.php @@ -0,0 +1,101 @@ +<script> + + // supplementary functions to select2 control + //////////////////////////////////////////////////////////////////////////// + + /** getValues + * --- -- -- - - - + * Get selected value(s) of Select2 control + * + * @param arg: source-element + * @return (string) value(s) comma-separated + */ + function getValues(srcElm) { + var res; + var obj = srcElm.select2('data'); // get delected data array + + if (obj.length === 0) return ''; // if empty list then nothig is selected + else { + var i = 0; + obj.forEach(function(elm){ // loop through object elements array + + if (i) res += ',' + elm.id; // if not first item, prepend ',' befor value(id) + else res = elm.id; // else set (first) checked value + + i++; + }); + } + return res; + } + + /** select2_set_text + * --- -- -- - - - + * select an (select2-)option by text + * onto a (single) select2 control + * + * @param control = jquery selector, + * @param t (string) = text of option + */ + function select2_set_text(control, t) { + opts = control.select2()[0].options; + var id; + for(i=0; i<opts.length; i++) { if (opts[i].text == t) id = i; } + control.val(id).trigger('change'); + } + + /** get_id_of_text + * --- -- -- - - - + * get id (=value) of an option with `t` as text + * onto a select2 control; + * + * Can be used on single or multiple select2 contols + * + * @param control = jquery selector, + * @param t (string) = text of option + */ + function get_id_of_text( control , t) { + opts = control.select2()[0].options; + var id = false; + for(i=0; i<opts.length; i++) { if (opts[i].text == t) id = i; } + return id; + } + + /** select2_set_multi_text + * --- -- -- - - - + * select several (select2-)options by text + * + * @param control = jquery selector, + * @param t_list (string) = list of selected texts + */ + function select2_set_multi_text(control, t_list) { /* define function */ + var ids = []; + t_list.forEach (t => { let id = get_id_of_text(control, t); + if (id !== false) ids.push(id); + }) + control.val(ids).change(); + } + + /** selected_text + * --- -- -- - - - + * get selected text of a select2 control + */ + function selected_text(control) { + if (typeof control.select2('data') === 'undefined') return false; + else if (control.select2('data').length == 0) return false; + else return control.select2('data')[0].text; + } + + /** + * get all selected texts from a multi-select2 contol + */ + function selected_multitexts(control) { + if (typeof control.select2('data') === 'undefined') return false; + else if (control.select2('data').length == 0) return false; + else { + var result = []; + control.select2('data').forEach( item => { result.push(item.text); }); + return result; + } + } + +</script>
\ No newline at end of file |
