[英]Passing data from node to jade?
The problem is I render a view and send some data 问题是我渲染视图并发送一些数据
console.log(products); // shows an array
res.render('seller/sell',{'shop_id':req.user.shop_id ,'products':products});
and I save the data like this in jade 我在玉器中保存这样的数据
input(id='shop_id',type='hidden',name='shop_id',value='#{shop_id}')
input(id='pd',type='hidden',name='pd',value='#{products}')
if(products !='')
each val , key in products
a(href!='home/sell/edit?id=#{val.id} ',class='product')
img(class='product_thum',src!='#{ val.product_thum}',alt!='#{ val.product_name}',title!='#{ val.product_name}')
p.product_name #{ val.product_name}
and then I try to get the products 然后我尝试获得产品
var d = $('#pd').val();
console.log(typeof d); //shows string
I know that products shuld be a array otherwise 我知道产品shuld是一个阵列否则
if(products !='')
each val , key in products
a(href!='home/sell/edit?id=#{val.id} ',class='product')
img(class='product_thum',src!='#{ val.product_thum}',alt!='#{ val.product_name}',title!='#{ val.product_name}')
p.product_name #{ val.product_name}
Wont work, but why did I get a string when I need the array? 不工作,但为什么我需要数组时得到一个字符串?
What did I do wrong? 我做错了什么?
You cannot 'store' an array in a hidden input field, but what you could do is store a list of the product id's, something like this: 您不能在隐藏的输入字段中“存储”数组,但您可以做的是存储产品ID的列表,如下所示:
var productIds = products.map(function(product){return product.id}).toString();
res.render('seller/sell',
{'shop_id':req.user.shop_id ,'products':products, productIds: productIds});
Then, in your jade view: 然后,在您的玉视图中:
input(id='shop_id',type='hidden',name='shop_id',value='#{shop_id}')
input(id='pd',type='hidden',name='pd',value='#{productIds}')
if(products !='')
each val , key in products
a(href!='home/sell/edit?id=#{val.id} ',class='product')
img(class='product_thum',src!='#{ val.product_thum}',alt!='#{ val.product_name}',title!='#{ val.product_name}')
p.product_name #{ val.product_name}
The value of pd
will now be a comma separated list of product Ids pd
的值现在是逗号分隔的产品ID列表
Not particularly elegant, but it solves the problem. 不是特别优雅,但它解决了这个问题。
Best way to show comma separated string Example data = abc,pqr,lmn; 显示逗号分隔字符串的最佳方式示例data = abc,pqr,lmn;
below code in js file
test = data.toString().split('",");
res.render("show.jade",{list: test});
display the code in jade as
table
tbody
each item in list
tr
td #{item}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.