记录一次使用knockout实现表格中select绑定,并选中默认值。
2018-03-23 23:33
435 查看
代码如下,注释的也比较清楚。<body>
<div id="maindiv">
<div>
<div>
<table class="table table-striped">
<thead>
<tr>
<th>id</th>
<th>候选</th>
<th> </th>
<th> </th>
</tr>
</thead>
<tbody data-bind="foreach: aList">
<tr>
<th data-bind="text:id"></th>
<th>
<select data-bind="options:aoptionlist, value: a, optionsText: 'Name' ,optionsText='Value'" ></select>
</th>
<th data-bind="click:aSave">保存</th>
<th data-bind="click:aDel">删除</th>
</tr>
</tbody>
</table>
</div>
<button type="button" class="btn btn-info" id="adda" data-bind="click:addafun">新增</button>
</div>
</div>
<script type="text/javascript">
function myViewModel() {
var self = this;
/**
* 一行的数据格式
* @param id
* @param aindex 默认选项在aoptionlist的下标
*/
function aVO(id,aindex){
this.id=id;
this.a=ko.observable(aoptionlist[aindex]); //这种可以绑定默认值
// this.a=ko.observable(aoptionlist);//夏斌啊这俩句一起也可以
// this.a(aoptionlist[aindex]);
console.info("this :");
console.info(this);
}
//展现的所有数据
self.aList = ko.observableArray();
// a 某列的的select中的所有 option 用一个数组
this.aoptionlist =getaOptionList();
this.aSave = function ($data) {
console.info("进入aSave id是"+$data.id);
console.info("进入aSave a");
console.info($data.a);
console.info("进入aSave a.Value");
console.info($data.a().Value);//vo 不加括号是undefined
}
this.aDel = function ($data) {
console.info("进入aDel id是"+$data.id);
console.info("进入aDel a");
console.info($data.a);
console.info("进入aDel vo.Value");
console.info($data.a().Value);//vo 不加括号是undefined
console.info("$data : ");
console.info($data);
self.aList.remove($data);//经过各种实验前边啥也不加就获取到了
}
this.getaList = function () {
}
this.addafun = function () {
var i = Math.ceil(Math.random()*100);
var id = i;
tmp = new aVO(id,i%3);//传入所在数组的索引
this.aList.push(tmp);
}
function getaOptionList() {
//todo 这个是需要后台配置文件里可配的
return [
{ Name: "AA", Value:"11" },
{ Name: "BB", Value: "22" },
{ Name: "CC", Value: "33" }
];
}
getaList();
}
ko.applyBindings(myViewModel(),$("#maindiv")[0]);
</script>
</body>使用踩坑记录 ,要让<select data-bind="options:aoptionlist, value: a, optionsText: 'Name' ,optionsText='Value'" ></select>中的a 要绑定的值是对应的optionsValue的值(目前体现为一个字符串,而不是一个对象),不然可能出现下拉列表无法选中我们想要的被选选项。
还有不要没有ptionsValue 这样弄不规范的,可能出现莫名的问题。
<div id="maindiv">
<div>
<div>
<table class="table table-striped">
<thead>
<tr>
<th>id</th>
<th>候选</th>
<th> </th>
<th> </th>
</tr>
</thead>
<tbody data-bind="foreach: aList">
<tr>
<th data-bind="text:id"></th>
<th>
<select data-bind="options:aoptionlist, value: a, optionsText: 'Name' ,optionsText='Value'" ></select>
</th>
<th data-bind="click:aSave">保存</th>
<th data-bind="click:aDel">删除</th>
</tr>
</tbody>
</table>
</div>
<button type="button" class="btn btn-info" id="adda" data-bind="click:addafun">新增</button>
</div>
</div>
<script type="text/javascript">
function myViewModel() {
var self = this;
/**
* 一行的数据格式
* @param id
* @param aindex 默认选项在aoptionlist的下标
*/
function aVO(id,aindex){
this.id=id;
this.a=ko.observable(aoptionlist[aindex]); //这种可以绑定默认值
// this.a=ko.observable(aoptionlist);//夏斌啊这俩句一起也可以
// this.a(aoptionlist[aindex]);
console.info("this :");
console.info(this);
}
//展现的所有数据
self.aList = ko.observableArray();
// a 某列的的select中的所有 option 用一个数组
this.aoptionlist =getaOptionList();
this.aSave = function ($data) {
console.info("进入aSave id是"+$data.id);
console.info("进入aSave a");
console.info($data.a);
console.info("进入aSave a.Value");
console.info($data.a().Value);//vo 不加括号是undefined
}
this.aDel = function ($data) {
console.info("进入aDel id是"+$data.id);
console.info("进入aDel a");
console.info($data.a);
console.info("进入aDel vo.Value");
console.info($data.a().Value);//vo 不加括号是undefined
console.info("$data : ");
console.info($data);
self.aList.remove($data);//经过各种实验前边啥也不加就获取到了
}
this.getaList = function () {
}
this.addafun = function () {
var i = Math.ceil(Math.random()*100);
var id = i;
tmp = new aVO(id,i%3);//传入所在数组的索引
this.aList.push(tmp);
}
function getaOptionList() {
//todo 这个是需要后台配置文件里可配的
return [
{ Name: "AA", Value:"11" },
{ Name: "BB", Value: "22" },
{ Name: "CC", Value: "33" }
];
}
getaList();
}
ko.applyBindings(myViewModel(),$("#maindiv")[0]);
</script>
</body>使用踩坑记录 ,要让<select data-bind="options:aoptionlist, value: a, optionsText: 'Name' ,optionsText='Value'" ></select>中的a 要绑定的值是对应的optionsValue的值(目前体现为一个字符串,而不是一个对象),不然可能出现下拉列表无法选中我们想要的被选选项。
还有不要没有ptionsValue 这样弄不规范的,可能出现莫名的问题。
相关文章推荐
- knockout select绑定,选中默认值
- 使用bootstraptable插件实现表格记录的查询、分页、排序操作
- jface databinding:使用CheckboxTableViewer实现表中(Set)对象与CheckTable中选中条目数据绑定
- 实现类似EXTJS表格插件 Flexgrid.js GridView中使用的记录
- 基于Metronic的Bootstrap开发框架经验总结(16)-- 使用插件bootstrap-table实现表格记录的查询、分页、排序等处理
- 使用cookie实现记录上一次的登录名和密码
- 使用插件bootstrap-table实现表格记录的查询、分页、排序等处理
- 使用Hibernate criteria进行分页查询时,如何实现一次查询取得总记录数和分页后结果集
- 使用C1TrueDBGrid表格控件实现数据绑定、分组和过滤功能
- 使用插件bootstrap-table实现表格记录的查询、分页、排序等处理
- 豆瓣电台WP7客户端 MVVM重构记录之使用MVVM Light实现数据绑定
- 使用(jquery) 实现select 对option 的动态选中
- 在ASP.NET MVC中使用Knockout实践02,组合View Model成员、Select绑定、通过构造器创建View Model,扩展View Model方法
- 使用word中的宏实现选中指定类型表格,并且批量修改样式结构
- ASP.NET Web API实践系列07,获取数据, 使用Ninject实现依赖倒置,使用Knockout实现页面元素和视图模型的双向绑定
- knockout页面绑定下拉列表select选中项错误问题
- 使用 Jquery-UI 实现一次拖拽多个选中的元素操作
- jsp中使用select,并实现通过request中的参数进行默认选中
- 【iOS】一种仿京东搜索历史记录的表格的实现(sqlite3的使用)
- 豆瓣电台WP7客户端 MVVM重构记录之使用MVVM Light实现Event绑定