在选定的索引更改时调用函数

发布于 2024-12-01 07:09:37 字数 155 浏览 1 评论 0原文

如何将函数绑定到选定的索引更改,类似于将函数绑定到按钮上的单击事件?

我需要这个的原因是我有这个模板必须重复“n”次。这个“n”是从组合框中选择的。

我如何使用 knockoutJS 库来做到这一点,因为它只需要模板结构中 foreach 属性中的列表/数组对象?

How do I bind a function to the selected index change, similar to binding a function to the click event on a button?

The reason I'd need this is that I have this template that has to repeat 'n' number of times. This 'n' is selected from the combobox.

How can I do this using the knockoutJS library as it takes only lists/array objects in its foreach attribute in a template structure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞清仓 2024-12-08 07:09:37

这可能对你有用。 html 看起来像:

<select id="mySelect">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="4">4</option>
</select>

<table>
    <thead>
        <tr>
            <th></th>
            <th>name</th>
            <th>price</th>
        </tr>
    </thead>
    <tbody data-bind="template: {name:'tempTemplate', foreach:  tempCollection}">
    </tbody>
</table>

对于 javascript:

<script type="text/javascript">
    function temp(name, price ){
        return {name: ko.observable(name),
                price: ko.observable(price)
        };
    }

    $(document).ready(function () {
        var viewModel = {
            tempCollection : ko.observableArray([{ name: "Tall Hat", price: "39.95" }]),
            addTemp: function () { this.tempCollection.push(temp("new","price")) },
            removeTemp: function (temp) { this.tempCollection.remove(temp) }

        };
        ko.applyBindings(viewModel);

        $("#mySelect").change(function() {
            var len = viewModel.tempCollection().length;
            for (var i = 0; i < len; i++) {
                viewModel.removeTemp(viewModel.tempCollection()[0]);
            }
            for (var i = 0; i < $(this).val(); i++) {
                viewModel.addTemp();
            }
        });
});
</script>

<script id="tempTemplate" type="text/html">
    <tr>
        <td><span data-bind="text: name" /></td>
        <td><span data-bind="text: price" /></td>
    </tr>
</script>

This could be working for you. The html looks like:

<select id="mySelect">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="4">4</option>
</select>

<table>
    <thead>
        <tr>
            <th></th>
            <th>name</th>
            <th>price</th>
        </tr>
    </thead>
    <tbody data-bind="template: {name:'tempTemplate', foreach:  tempCollection}">
    </tbody>
</table>

and for the javascript:

<script type="text/javascript">
    function temp(name, price ){
        return {name: ko.observable(name),
                price: ko.observable(price)
        };
    }

    $(document).ready(function () {
        var viewModel = {
            tempCollection : ko.observableArray([{ name: "Tall Hat", price: "39.95" }]),
            addTemp: function () { this.tempCollection.push(temp("new","price")) },
            removeTemp: function (temp) { this.tempCollection.remove(temp) }

        };
        ko.applyBindings(viewModel);

        $("#mySelect").change(function() {
            var len = viewModel.tempCollection().length;
            for (var i = 0; i < len; i++) {
                viewModel.removeTemp(viewModel.tempCollection()[0]);
            }
            for (var i = 0; i < $(this).val(); i++) {
                viewModel.addTemp();
            }
        });
});
</script>

<script id="tempTemplate" type="text/html">
    <tr>
        <td><span data-bind="text: name" /></td>
        <td><span data-bind="text: price" /></td>
    </tr>
</script>
明天过后 2024-12-08 07:09:37

现在最好的选择是使用范围函数,该函数接受开始值和停止值并返回包含这些数字的数组。例如,我使用 underscore 库中的 range 函数。

var numArray = _.range(0, 5); //returns [0, 1, 2, 3, 4]

像这样使用淘汰赛。

<div data-bind="template: { name: 'myTemplate', foreach: _.range(0, 5) }">
</div>

在模板内部,您可以使用“$data”捕获当前数字并将其用作索引。

<div>Index: <span data-bind="text: $data"></span></div>
<div>My Object Prop: <span data-bind="text: viewModel.MyObjects[$data].MyProp"></span></div>

如果您想像前面的示例一样进行简单的迭代,则应该直接迭代对象,而不是使用这种数组索引方法。然而,如果你需要做一些奇特的事情,这个技巧就可以了。

例如,如果您需要显示一组 2 个对象的列表,您可以这样做。

<div data-bind="foreach: _.range(0, viewModel.MyObjects().length, 2)">
  <div>
    <div data-bind="template: { name: 'myTemplate', data: viewModel.MyObjects()[$data] }"></div>
    <div data-bind="template: { name: 'myTemplate', data: viewModel.MyObjects()[$data + 1] }"></div>
  </div>
</div>

The best choice right now is to use a range function that takes a start and stop value and returns an array with those numbers. I, for instance, use the range function in the underscore library.

var numArray = _.range(0, 5); //returns [0, 1, 2, 3, 4]

Use this with knockout like so.

<div data-bind="template: { name: 'myTemplate', foreach: _.range(0, 5) }">
</div>

Inside of the template, you can capture the current number using '$data' and use it like an index.

<div>Index: <span data-bind="text: $data"></span></div>
<div>My Object Prop: <span data-bind="text: viewModel.MyObjects[$data].MyProp"></span></div>

If you want to do a simple iteration as in the previous example, you should iterate over the object directly instead of this array indexing approach. However, if you need to do something fancy, this technique will do the trick.

For instance, if you need to display a list of objects in sets of 2, you could do this.

<div data-bind="foreach: _.range(0, viewModel.MyObjects().length, 2)">
  <div>
    <div data-bind="template: { name: 'myTemplate', data: viewModel.MyObjects()[$data] }"></div>
    <div data-bind="template: { name: 'myTemplate', data: viewModel.MyObjects()[$data + 1] }"></div>
  </div>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文