0

web page Hi guys, I am writing some JavaScript code in the script element on a PUG file, how can I access the data (client_list, see below code) that transferred from the Controller, Thanks!

extends layout

block content
  // here the client_list can be accessed
  - client_list.sort(function(a, b) {let textA = a.company_name.toUpperCase(); let textB = b.company_name.toUpperCase(); return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;});
  input(type='text', id='search_content')
  button(type='button', id='search_button') Search
  hr
  table(id='product_css')
    tr
      th Company Name
      th Person Name
    each client in client_list
      tr
        td #{client.company_name} 
        td #{client.person_name}

  script(type='text/javascript').
    $(document).ready(function() {
      const content = $('#search_content').val();
      // where client_list is undefined
      client_list.forEach(function(client, index, array) {
         console.log(client, index)
      })

    });
  • You must declare a new javascript variable within the `script` tag, and then use Pug interpolation to set it's value equal to the variable/object passed from the controller. – Sean Jun 13 '20 at 16:17
  • Thanks Sean, finally the issued got solved by: var client_list = JSON.parse('!{JSON.stringify(client_list)}'); – JesseWei Li Jun 14 '20 at 01:27

0 Answers0