Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

Highcharts Select menu from csv file and select option programmatically

I built a line chart with Highcharts: see fiddle

The visible series can be chosen by a dropdown menu. This drop down menu is based on a csv file. Now I want to select a specific entry of the list by name, e.g. "Gro?britannien". It should not be chosen by value because the value can change from time to time.

Here is my code for getting the drop down list from csv file:

function insertCountryInList(country) {
    var list = $('select#countries');
    var newOption = 
        $('<option></option>')
        .attr('value', country.value)
        .text(country.name);

    list.append(newOption);

  }

  var CSV_URL = 'https://johanneschrist.de/grafiken/dropdown.csv';

  $.get(CSV_URL, function (data) {
    var lines = data.split("
");
    lines.shift();

    var countries = lines.map(function (line) {
      var fields = line.split(",");
      return {
        value: fields[0], 
        name: fields[1]
      };
    });

    countries.forEach(insertCountryInList);
  });

I tried this but it did not work:

    $('#countries').val('22');
    $('#countries').trigger('change');

How can I do that?

question from:https://stackoverflow.com/questions/65887053/highcharts-select-menu-from-csv-file-and-select-option-programmatically

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change value and call change:

$('#countries').val(22).change();

Live demo: https://jsfiddle.net/BlackLabel/e7q0xL4z/

API Reference: https://api.jquery.com/change/

Related question: How to trigger jQuery change event in code


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...