gpt4 book ai didi

javascript - 如何在结果表中只显示选定的记录

转载 作者:行者123 更新时间:2023-12-02 17:43:28 25 4
gpt4 key购买 nike

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

现在,我们需要修改它以仅在结果表中显示复选框选定的记录。此外,当用户单击结果表上的删除按钮时,应在部分表中取消选中该复选框。

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

enter image description here

<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. set method - ObservableObject - Kedo API Reference
  2. set method - kendo Model - Kedo API Reference
  3. Filtering source in a Kendo Template
  4. Kendo-UI grid Set Value in grid with Javascript

最佳答案

要事第一。

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

HTML

<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

Fiddle

摘要

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

将复选框模板制作为

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

在删除函数中,使用以下内容

    employee.set('isChecked', false);

关于javascript - 如何在结果表中只显示选定的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21982668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com