如何在结果表中只显示选定的记录 [英] How to display only selected records in the result table

查看:23
本文介绍了如何在结果表中只显示选定的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个表使用相同的源.这些表使用剑道模板的源绑定.目前这两个表的来源都是employees.这两个表都显示相同的数据.

There are two tables using the same source. These tables are using source binding of kendo templates. At present the source for both these tables are employees. Both these tables are displaying the same data.

现在,我们需要修改它以在结果表中只显示复选框选中的记录.此外,当用户单击结果表上的删除按钮时,应取消选择节表中的复选框.

Now, we need to modify it to show only check-box selected records in the result table. Also, when user clicks on the delete button on the result table, the check-box should be un-selected in the section table.

我们需要做哪些修改才能使其在MVVM中工作?

What modification do we need to do to make it work in MVVM?

<head>
    <title>MVVM Test</title>
    <script type="text/javascript" src="lib/kendo/js/jquery.min.js"></script>
    <script type="text/javascript" src="lib/kendo/js/kendo.web.min.js"></script>

    <!----Kendo Templates-->
    <script id="row-template" type="text/x-kendo-template">
          <tr>
                <td data-bind="text: name"></td>
                <td data-bind="text: age"></td>
                <td><button type="button" data-bind="click: deleteEmployee">Delete</button></td>
          </tr>
    </script>
    <script id="selection-table-template" type="text/x-kendo-template">
          <tr>
                <td data-bind="text: name"></td>
                <td data-bind="text: age"></td>
                <td>
                  <input type="checkbox" name="selection" value="a">             
                </td>

          </tr>

    </script>

    <!--MVVM Wiring using Kendo Binding-->
    <script type="text/javascript">

        $(document).ready(function () {
            kendo.bind($("body"), viewModel);
        });

    </script>

</head>

MVVM

    <script type="text/javascript">
        var viewModel = kendo.observable({

            // model definition
            employees: [
        { name: "Lijo", age: "28" },
        { name: "Binu", age: "33" },
        { name: "Kiran", age: "29" }
        ],

            personName: "",
            personAge: "",

            //Note: Business functions  does not access any DOM elements using jquery.
            //They are referring only the objects in the view model.

            //business functions  (uses "this" keyword - e.g. this.get("employees"))
            addEmployee: function () {
                this.get("employees").push({
                    name: this.get("personName"),
                    age: this.get("personAge")
                });

                this.set("personName", "");
                this.set("personAge", "");
            },

            deleteEmployee: function (e) {

                //person object is created using "e"
                var person = e.data;

                var employees = this.get("employees");
                var index = employees.indexOf(person);
                employees.splice(index, 1);
            }

        });

    </script>

身体

<body>


    <table id="selectionTable">
        <thead>
            <tr>
                <th>
                    Name
                </th>
                <th>
                    Age
                </th>
            </tr>
        </thead>
        <tbody data-template="selection-table-template" data-bind="source: employees">
        </tbody>
    </table>

  <br />
  <hr />

    <table id="resultTable">
        <thead>
            <tr>
                <th>
                    Name
                </th>
                <th>
                    Age
                </th>
            </tr>
        </thead>
        <!--The data-template attribute tells Kendo UI that the employees objects should be formatted using a Kendo UI template. -->
        <tbody data-template="row-template" data-bind="source: employees">
        </tbody>
    </table>


</body>

参考资料

  1. 设置方法 - ObservableObject - Kedo API 参考
  2. 设置方法 - kendo 模型 - Kedo API 参考
  3. 在剑道模板中过滤源
  4. Kendo-UI 网格使用 Javascript 在网格中设置值

推荐答案

第一件事.

如果在删除对象时将其从 viewModel 中删除,它也会从源表中删除.如果您希望它是您描述的方式,则需要两个数组来处理此问题.但根据你问题的第一部分,我想我会发布一个解决方案.

If you remove the object from the viewModel when you delete it, it will be removed from your source table as well. You would need two arrays to handle this if you wanted it to be the way you describe. But based on the first part of your question, I thought I would post a solution.

<script id="row-template" type="text/x-kendo-template">
    <tr data-bind="visible: isChecked">
        <td data-bind="text: name"></td>
        <td data-bind="text: age"></td>
        <td>
            <button type="button" data-bind="click: deleteEmployee">Delete</button>
        </td>
    </tr>
</script>

<script id="selection-table-template" type="text/x-kendo-template">
    <tr>
        <td data-bind="text: name"></td>
        <td data-bind="text: age"></td>
        <td>
            <input type="checkbox" name="selection" data-bind="checked: isChecked"/>
        </td>
    </tr>
</script>

<table id="selectionTable">
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tbody data-template="selection-table-template" data-bind="source: employees"/>
</table>

<br />
<hr />

<table id="resultTable">
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tbody data-template="row-template" data-bind="source: employees"/>
</table>

JAVASCRIPT

var viewModel = kendo.observable({
    employees: [
        { name: "Lijo", age: "28", isChecked: true },
        { name: "Binu", age: "33", isChecked: true },
        { name: "Kiran", age: "29", isChecked: true }
    ],

    personName: "",
    personAge: "",

    addEmployee: function () {
        this.get("employees").push({
            name: this.get("personName"),
            age: this.get("personAge")
        });

        this.set("personName", "");
        this.set("personAge", "");
    },

    deleteEmployee: function (e) {
        var person = e.data;
        var employees = this.get("employees");
        var index = employees.indexOf(person);
        var employee = employees[index];

        //set
        employee.set('isChecked', false);
    }
});


$(document).ready(function () {
    kendo.bind($("body"), viewModel);
});

JSFiddle

小提琴

在row-template"中使用 data-bind="visible: isChecked" 仅显示底部表格中的选定记录.

Use data-bind="visible: isChecked" in "row-template" to show only selected records in the bottom table.

为复选框制作模板

    <input type="checkbox" name="selection" data-bind="checked: isChecked"/>

在删除函数中,使用如下

In the delete function, use following

    employee.set('isChecked', false);

这篇关于如何在结果表中只显示选定的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆