From 4065be9e4eedd947bf87337f511e102058917d88 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Thu, 22 Dec 2016 12:57:49 +0100 Subject: [PATCH] Fix marking of selected option [SCI-819] --- .../samples/sample_datatable.js.erb | 7 ++++- app/assets/javascripts/sitewide/utils.js | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js.erb b/app/assets/javascripts/samples/sample_datatable.js.erb index fec699502..bdd205888 100644 --- a/app/assets/javascripts/samples/sample_datatable.js.erb +++ b/app/assets/javascripts/samples/sample_datatable.js.erb @@ -780,6 +780,9 @@ function createSampleTypeSelect(data, selected) { $.each(data, function(i, val) { var $option = $('') .attr('value', val.id).text(val.name); + if((i + 1) === selected) { + $option.attr('selected', true); + } $selectType.append($option); }); return $selectType; @@ -806,7 +809,9 @@ function createSampleGroupSelect(data, selected) { var $option = $('') .attr('value', val.id).text(val.name) .attr('data-content', $span.prop('outerHTML') + ' ' + val.name); - + if((i + 1) === selected) { + $option.attr('selected', true); + } $selectGroup.append($option); }); return $selectGroup; diff --git a/app/assets/javascripts/sitewide/utils.js b/app/assets/javascripts/sitewide/utils.js index f42f515c2..50224bb57 100644 --- a/app/assets/javascripts/sitewide/utils.js +++ b/app/assets/javascripts/sitewide/utils.js @@ -215,3 +215,32 @@ function initPageTutorialSteps(pageFirstStepN, pageLastStepN, nextPagePath, }); } } + +/** + * Add redirection links on dropdown elements. You must specify 'href' + * attribute yourself, and the dropdown elments which don't have them, will get + * '#' by default. + * @param {number} selectedIdx Index of element to be selected + * @param {string} urlParam URL parameter to pass to the link URLs + * @return {Object} This + */ +$.fn.makeDropdownOptionsLinks = function(selectedIdx, urlParam) { + selectedIdx = _.isUndefined(selectedIdx) ? 1 : selectedIdx; + + $(this).change(function() { + window.location.href = addParam($(this).find('option:selected') + .attr('href'), urlParam); + }); + + $(this).find('option') + .each(function() { + if ($(this).is('[href]')) { + $(this).addClass('link-look'); + } else { + $(this).attr('href', '#'); + } + }) + .eq(selectedIdx).attr('selected', true); + + return this; +};