English 中文(简体)
• 如何在RactiveJS的每一组点点点点击事件上打一个字塔子,以示理由为功能?
原标题:How to call a function with arguments for a button on click event in each block RactiveJS?

I m 采用RactiveJS 1.4.1 和p.net mvc 核心raz页。

我在网页上将“html”放在了一个文字上。 并要求RactiveJS这样做。 “ToCard”方法没有使用。 我试图在数据栏下增加ToCard功能,而不是发挥作用。 我也试图将ToCard的职能称为“活跃的......”(添加“ToCart......”)。 它也没有工作。

这里是法典;

@{
    ViewData["Title"] = "Ractive Examples - ForEach";
}

<div class="card-body">

    <script id="testTemplate" type="text/html">
        <h1>Let s shop!</h1>
        <ul>
            {{#each items: i}}
            <li>
                <p>{{i+1}}: {{description}}</p>
                <label><input value= {{qty}} > Quantity</label>
                <!-- when the user clicks this button, add {{qty}} of this item -->
                <button on-click="addToCart:{{this}},{{qty}}" type="button">Add to cart</button>
            </li>
            {{/each}}
        </ul>
    </script>

    <div id="container"></div>

</div>

@section Scripts
{
    <script>
        // Predefined data, items to be added to cart.
        const items_data = [
            { description: "Asset 1", qty: 1 },
            { description: "Asset 2", qty: 21 },
            { description: "Asset 3", qty: 35 },
            { description: "Asset 4", qty: 0 },
            { description: "Asset 5", qty: 5 }
        ];
        
        const ractive = new Ractive({
            el:  #container ,
            template: "#testTemplate",
            data: {
                items: items_data
            },
            on: {
                addToCart: function ( event, item, qty ) {
                    // Do something with the item and qty
                    console.log( item );
                    console.log( qty );
                }
            }
        });
        
    </script>

}

我直接引用RactiveJS文件; 但是,这并不奏效(但并未显示任何情况)。

@{
    ViewData["Title"] = "Ractive Examples - ForEach";
}

<div class="card-body">

    <script id="testTemplate" type="text/html">
        <h1>Let s shop!</h1>
        <ul>
            {{#each items: i}}
            <li>
                <p>{{i+1}}: {{description}}</p>
                <label><input value= {{qty}} > Quantity</label>
                <!-- when the user clicks this button, add {{qty}} of this item -->
                <button on-click= addToCart:{{this}},{{qty}} >Add to cart</button>            
            </li>
            {{/each}}
        </ul>
    </script>

    <div id="container"></div>

</div>

@section Scripts
{
    <script>
        // Predefined data, items to be added to cart.
        const items_data = [
            { description: "Asset 1", qty: 1 },
            { description: "Asset 2", qty: 21 },
            { description: "Asset 3", qty: 35 },
            { description: "Asset 4", qty: 0 },
            { description: "Asset 5", qty: 5 }
        ];
        
        const ractive = new Ractive({
            el:  #container ,
            template: "#testTemplate",
            data: {
                items: items_data
            }
        });
        
        ractive.on(  addToCart , function ( event, item, qty ) {
          console.log(  Adding   + qty +   of   + item.description +   to cart );
        });
        
    </script>

}

What am I missing?

Update: I can use data- attribute, i know but I m searching for better way to do it.*

const ractive = Ractive({
          el:  #container ,
          template: `
          <ul>
            {{#each items: i}}
            <li>
                <p>{{i+1}}: {{description}}</p>
                <label><input value= {{qty}} > Quantity</label>
                <button type="button" data-id="{{i}}" on-click="addToCart">Push me!</button>       
            </li>
            {{/each}}
          </ul>
          `,
            data: {
                items: items_data
            },
            on: {
              addToCart (event) {
                  const id = event.node.getAttribute( data-id );
                  console.log("Item of the id is: " + id);
              }
            }    
        });
问题回答

I dont know asp.net so I haven t tested it but you could do this:

@{
    ViewData["Title"] = "Ractive Examples - ForEach";
}

<div class="card-body">

    <script id="testTemplate" type="text/html">
        <h1>Let s shop!</h1>
        <ul>
            {{#each items: i}}
            <li>
                <p>{{i+1}}: {{.description}}</p>
                <label><input value= {{.qty}} > Quantity</label>
                <!-- when the user clicks this button, add {{qty}} of this item -->
                <button on-click="@this.addToCart({{this}},{{.qty}})" type="button">Add to cart</button>
            </li>
            {{/each}}
        </ul>
    </script>

    <div id="container"></div>

</div>

@section Scripts
{
    <script>
        // Predefined data, items to be added to cart.
        const items_data = [
            { description: "Asset 1", qty: 1 },
            { description: "Asset 2", qty: 21 },
            { description: "Asset 3", qty: 35 },
            { description: "Asset 4", qty: 0 },
            { description: "Asset 5", qty: 5 }
        ];
        
        const ractive = new Ractive({
            el:  #container ,
            template: "#testTemplate",
            data: {
                items: items_data
            },
            addToCart: function ( item, qty ) {
                    // Do something with the item and qty
                    console.log( item );
                    console.log( qty );
            }
            
        });
        
    </script>

}

tip: as you see you need the dot before the key to access the key in the {{#each array}} item that is an object. e.g.: (.qty) (.description)





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签