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

141
Visualizações
Loading dataTable values into DOM id fields

In my admin area I have over 50 modules that are used by volunteers to maintain database tables. In each module I have a form for editing records and a dataTable for listing all the records. To edit a record the user finds the record in the dataTable and selects/clicks the row. The click activates a table click event when then causes the record information to populate the form as illustrated in the following code.

    let rowData = oTable.row(this).data();
    $('#bio_name').val(rowData.bio_name);
    $('#bio_title').val(rowData.bio_title);
    $('#bio_sub_fk').val(rowData.bio_sub_fk);
    $('#bio_text_top').val(rowData.bio_text_top);
    $('#bio_text_main').val(rowData.bio_text_main);
    $('#bio_text_bot').val(rowData.bio_text_bot);

I would like to update the process so that I could call a function to populate the form rather than have to maintain 50+ modules. The call would be

    load_Form( prefix, rowData );

and the function would be

    function load_Form( prefix, rowData ) {
        $('[id^=' + prefix + '_]').each( function() {
            //  What goes here?
        });
    }

But I'm at a loss as to what would go in the .each( function() {. I would think something like

    $(this).val(rowData.this);

But I don't think the 2nd 'this' is correct. Any help would be greatly appreciated.

TIA James A Wilson www.txfannin.org

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'm not as familiar with jQuery, but I imagine the .each function takes arguments representing the current item.

Check out the documentation for .each to see the arguments it accepts

Here is an implementation in vanilla js. Note the element portion in the .forEach

We can then get the id of the element that we matched, and remove the prefix to get the property to match on our data source.

const dataSource = {
  firstName: 'John',
  lastName: 'Doe',
  age: 45,
};

function populateForm(prefix, data) {
  [...document.querySelectorAll(`[id^=${prefix}`)].forEach((element, index, array) => {
    const field = element.getAttribute('id').replace(prefix, '');
    element.value= data[field];
  })
}

populateForm('bio_', dataSource);
<input id="bio_firstName"/>
<input id="bio_lastName"/>
<input id="bio_age"/>

about 4 years ago · Juan Pablo Isaza 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