Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
How to call a vue method from inside the xAxis.labels.formatter() highcharts object

So what i am trying to achieve is that, there should be a checkbox before the value in the xAxis and it should call my test() vue method when the checkbox is changed by clicking on it. Right now it is calling test() when buildChart() method is called.

buildChart() {
      const context = this;
      return {
        chart: {
          height: 500,
          type: 'columnrange',
          inverted: true,
          spacingLeft: 10,
          spacingRight: 10,
        },
        xAxis: {
        showEmpty: false,
        title: null,
        type: 'category',
          labels: {
            useHTML: true,
            formatter() {
              // This is where i am rendering the checkbox, would like it to trigger my vue function which is within the current vue context.
              return `<input type='checkbox' onchange='${context.test(this.value)}'> ${this.value}`;
            },
          },
        },
     };
}
test(value) {
console.log(value);
}
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This won't work, returned value from a formatter function is treated as a string.

As a solution, you can store a label's value in a data-* attribute and add change event after the chart is created.

export default {
  methods: {
    test(value) {
      console.log(value); 
    }
  },
  data() {
    const context = this;
    return {
      chartOptions: {
        chart: {
          events: {
            load: function() {
              const labelGroup = this.xAxis[0].labelGroup;
              const checkboxes = labelGroup.div.getElementsByTagName('input');
              
              for (const checkbox of checkboxes) {
                checkbox.addEventListener('change', function(e){
                  context.test(this.dataset.value);
                })
              }
            }
          }
        },
        xAxis: {
          showEmpty: false,
          title: null,
          type: 'category', 
          labels: {
            useHTML: true,
            formatter() {
              return `<input type='checkbox' data-value=${this.value}> ${this.value}`;
            }
          }
        }
      }
    };
  }
};

Live demo: https://codesandbox.io/s/highcharts-vue-demo-fork-j4ty16?file=/src/components/Chart.vue

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda