Node.js 如何在Jade模板中预填充编辑页面(Node+;Express+;Jade)

Node.js 如何在Jade模板中预填充编辑页面(Node+;Express+;Jade),node.js,express,Node.js,Express,我有一个查看页面,列出了所有产品,每个产品都有编辑和删除按钮。我试图在编辑页面中预先填充输入字段,以显示所选产品的数据 我想知道如何用所选产品数据预填充编辑页面 查看page.js的代码 router.get("/", async (req, res) => { const listdata = await sapview.listall(); const pricedata = listdata.T_PRICE; res.render('viewpage', {title:

我有一个查看页面,列出了所有产品,每个产品都有编辑和删除按钮。我试图在编辑页面中预先填充输入字段,以显示所选产品的数据

我想知道如何用所选产品数据预填充编辑页面

查看page.js的代码

router.get("/", async (req, res) => {
  const listdata = await sapview.listall();
  const pricedata = listdata.T_PRICE;
  res.render('viewpage', {title: 'Products', data: pricedata})
  // res.send(pricedata)
});
viewpage.jade的代码

extends layout

block content
  h1(style='text-align:center')= title
  button(type='submit', onClick='add()') Add a Material
  script.
    function add() {
      window.location.href = '/addpage'
    }
    function edit() {
      window.location.href = '/editpage'
    }
    function removed() {
      window.location.href = '/removepage'
    }
  br
  br
  -var product = data
  div
  table.table.table-hover(border='1', style='width:100%')
    tr
        th Sl.No
        th Plant
        th Material
        th Currency
        th Rate
        th Price_Unit
        th Cond_Unit
        th Customer
        th Portal User
        th Options
    tbody
      each value in product
        tr
          td(style='text-align:center')
          td(style='text-align:center', id='p_plant')= value.PLANT
          td(style='text-align:left', id='p_material')= value.MATERIAL
          td(style='text-align:left', id='p_currency')= value.CURRENCY
          td(style='text-align:right', id='p_rate')= value.RATE
          td(style='text-align:center', id='p_price_unit')= value.PRICE_UNIT
          td(style='text-align:left', id='p_cond_unit')= value.COND_UNIT
          td(style='text-align:left', id='p_customer')= value.CUSTOMER
          td(style='text-align:left', id='p_portal_user')= value.PORTAL_USER
          td(style='text-align:left')
            ul
              button(type='submit', onClick='edit()') Edit
              button(type='submit', onClick='removed()') Delete
extends layout

block content
  h1= title
  form(action="/editpage" method="PUT")
    label(for='plant') Plant:
    input(placeholder = 'P_PLANT', id='P_PLANT', value='value.PLANT')
    br
    label(for='material') Material:
    input(placeholder = 'P_MATERIAL', id='P_MATERIAL')
    br
    label(for='customer') Customer:
    input(placeholder = 'P_CUSTOMER', id='P_CUSTOMER')
    br
    label(for='rate') Rate:
    input(placeholder = 'P_RATE', id='P_RATE')
    br
    label(for='currency') Currency:
    input(placeholder = 'P_CURRENCY', id='P_CURRENCY')
    br
    label(for='price_unit') Price_Unit:
    input(placeholder = ' P_PRICE_UNIT', id='P_PRICE_UNIT')
    br
    label(for='cond_unit') Cond_Unit:
    input(placeholder = 'P_COND_UNIT', id='P_COND_UNIT')
    br
    label(for='portal_user') Portal_User:
    input(placeholder = 'P_PORTAL_USER', id='P_PORTAL_USER')
    br
    br
    button(type='submit') Save Changes
    br
    br
  button(type='submit', onclick='nav()') Cancel
  script.
    function nav() {
      window.location.href = '/'
    }
编辑page.js的代码

router.put('/editpage', 

(req, res) => {
  console.log("Hello", req.body);
  client.connect(() => {
    console.log('Connecting');
    client.invoke("ZSD_CP_PRICE_CHANGE", 
    {P_PLANT: req.body.P_PLANT, 
    P_MATERIAL: req.body.P_MATERIAL, 
    P_CUSTOMER: req.body.P_CUSTOMER,
    P_RATE: req.body.P_RATE,  
    P_CURRENCY: req.body.P_CURRENCY,
    P_PRICE_UNIT: req.body.P_PRICE_UNIT,
    P_COND_UNIT: req.body.P_COND_UNIT,
    P_PORTAL_USER: req.body.P_PORTAL_USER
    },
    (err, result) => {
      console.log('Invoking')
      if (err) {
        console.log(err)
        return err;
      }
      console.log(result);
    });
    res.redirect('/');
  });
});
用于编辑page.jade的代码

extends layout

block content
  h1(style='text-align:center')= title
  button(type='submit', onClick='add()') Add a Material
  script.
    function add() {
      window.location.href = '/addpage'
    }
    function edit() {
      window.location.href = '/editpage'
    }
    function removed() {
      window.location.href = '/removepage'
    }
  br
  br
  -var product = data
  div
  table.table.table-hover(border='1', style='width:100%')
    tr
        th Sl.No
        th Plant
        th Material
        th Currency
        th Rate
        th Price_Unit
        th Cond_Unit
        th Customer
        th Portal User
        th Options
    tbody
      each value in product
        tr
          td(style='text-align:center')
          td(style='text-align:center', id='p_plant')= value.PLANT
          td(style='text-align:left', id='p_material')= value.MATERIAL
          td(style='text-align:left', id='p_currency')= value.CURRENCY
          td(style='text-align:right', id='p_rate')= value.RATE
          td(style='text-align:center', id='p_price_unit')= value.PRICE_UNIT
          td(style='text-align:left', id='p_cond_unit')= value.COND_UNIT
          td(style='text-align:left', id='p_customer')= value.CUSTOMER
          td(style='text-align:left', id='p_portal_user')= value.PORTAL_USER
          td(style='text-align:left')
            ul
              button(type='submit', onClick='edit()') Edit
              button(type='submit', onClick='removed()') Delete
extends layout

block content
  h1= title
  form(action="/editpage" method="PUT")
    label(for='plant') Plant:
    input(placeholder = 'P_PLANT', id='P_PLANT', value='value.PLANT')
    br
    label(for='material') Material:
    input(placeholder = 'P_MATERIAL', id='P_MATERIAL')
    br
    label(for='customer') Customer:
    input(placeholder = 'P_CUSTOMER', id='P_CUSTOMER')
    br
    label(for='rate') Rate:
    input(placeholder = 'P_RATE', id='P_RATE')
    br
    label(for='currency') Currency:
    input(placeholder = 'P_CURRENCY', id='P_CURRENCY')
    br
    label(for='price_unit') Price_Unit:
    input(placeholder = ' P_PRICE_UNIT', id='P_PRICE_UNIT')
    br
    label(for='cond_unit') Cond_Unit:
    input(placeholder = 'P_COND_UNIT', id='P_COND_UNIT')
    br
    label(for='portal_user') Portal_User:
    input(placeholder = 'P_PORTAL_USER', id='P_PORTAL_USER')
    br
    br
    button(type='submit') Save Changes
    br
    br
  button(type='submit', onclick='nav()') Cancel
  script.
    function nav() {
      window.location.href = '/'
    }


尝试使用不带引号的value。比如说

input(placeholder = 'P_PLANT', id='P_PLANT', value=PLANT)

value='PLANT'显示带有植物文本的输入字段。我希望它显示苹果绿的价值,请注意,您正在使用引号。这使它成为一根弦。删除引号,看看它是否正常工作。这应该是可以解决的。所以,如果它是空的,那么让我们确保工厂不是空的。将其直接打印到页面上的任意位置,以查看是否显示值。如果没有,则确认在页面上下文中分配了值。确保render方法具有像res.render('editpage',{title:'Products',PLANT:'value of PLANT'})那样分配的PLANT值,同时单击编辑按钮,我将href分配给了'/editpage/'+value.PLANT'。(将数据作为url参数从查看页面传递到编辑页面)。在渲染时的编辑页面中,我使用了res.render('editpage',{plant:req.params.plant}),然后我就可以在输入字段的value属性中将plant作为变量传递了,谢谢