javascript 将数据从节点传递给玉?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13581023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:12:10  来源:igfitidea点击:

Passing data from node to jade?

javascriptnode.jspugrender

提问by paynestrike

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

我知道产品应该是一个数组,否则

    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?

我做错了什么?

回答by Alex

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 pdwill now be a comma separated list of product Ids

的值pd现在将是一个逗号分隔的产品 ID 列表

Not particularly elegant, but it solves the problem.

不是特别优雅,但它解决了问题。

回答by Praveen

Best way to show comma separated string Example data = abc,pqr,lmn;

显示逗号分隔字符串的最佳方式 Example 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}