HTML

<div id="modalAreaID01">
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal"
            data-bind="attr: { 'data-target': modalWindowId },click:modalWindowLoad">
        Launch demo modal
    </button>
</div>
<script src="Content/jquery-2.1.1.min.js"></script>
<script src="Content/bootstrap-3.2.0-dist/js/bootstrap.min.js"></script>
<link href="Content/bootstrap-3.2.0-dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="Content/knockout-3.1.0.min.js"></script>
<script src="Content/knockout.mapping.js"></script>
<script src="index.js"></script>

JS

var buidModelWindow = function (showWindowId,dataSorce, htmlAreaID, selectAllKOAction, saveKOAction, closeKOAction) {
    var html = "<div class='modal fade' id='" + showWindowId + "' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>"
    + "    <div class='modal-dialog'>"
    + "        <div class='modal-content'>"
    + "            <div class='modal-header'>"
    + "                <button type='button' class='close' data-dismiss='modal' data-bind='click:" + closeKOAction + "'><span aria-hidden='true'>&times;</span><span class='sr-only'>Close</span></button>"
    + "                <h4 class='modal-title' id='myModalLabel'>Modal title</h4>"
    + "            </div>"
    + "            <div class='modal-body'>"
    + "<!"+"-- ko foreach:"
    + dataSorce
    + " -->"
    + "<label><input type='checkbox' data-bind='checked:Enabled' /> <span data-bind='text:Value' /></label>"
    + "<!-- /ko -->"
    + "            </div>"
    + "            <div class='modal-footer'>"
    + "                <button type='button' class='btn btn-primary' data-bind='click:" + selectAllKOAction + "'>SelectAll</button>"
    + "                <button type='button' class='btn btn-default' data-dismiss='modal' data-bind='click:" + closeKOAction + "'>Close</button>"
    + "                <button type='button' class='btn btn-primary' data-bind='click:" + saveKOAction + "'>Save changes</button>"
    + "            </div>"
    + "        </div>"
    + "    </div>"
    + "</div>"
    $("#" + htmlAreaID + "").append(html);
}
var ViewModel = function (data) {
    var self = this;
    self.modelList = ko.mapping.fromJS(data);
    self.modalAreaID = ko.observable("modalAreaID01");
    self.modalWindowId = ko.observable("#modalWindowId01");
    self.modalWindowForeachID = ko.observable("modelList");
    self.isAll = ko.observable(false);
    self.save = ko.observable(false);
    var indexAndEnabled = new Array();
    self.selectAll = function () {
        if (self.isAll() == false) {
            ; i < self.modelList().length; i++) {
                self.modelList()[i].Enabled(true);
            }
            self.isAll(true);
        }
        else {
            ; i < self.modelList().length; i++) {
                self.modelList()[i].Enabled(false);
            }
            self.isAll(false);
        }
    }
    self.saveKOAction = function () {
        self.save(true);
        ; i < self.modelList().length; i++) {
            indexAndEnabled[i] = self.modelList()[i].Enabled();
        }
    }
    self.closeKOAction = function () {
        if (self.save() == false) {
            for (i in indexAndEnabled)
            {
                self.modelList()[i].Enabled(indexAndEnabled[i]);
            }
        } else {
        }
    }
    self.modalWindowLoad = function () {
        self.save(false);
        ; i < self.modelList().length; i++) {
            indexAndEnabled[i] = self.modelList()[i].Enabled();
        }
    }
    buidModelWindow(self.modalWindowId().substring(),
                    self.modalWindowForeachID(),
                    self.modalAreaID(),
                    "selectAll", "saveKOAction", "closeKOAction");
}
$(function () {
    var data =jQuery.parseJSON('{"KVPEList":[{"Key":null,"Value":"阿富汗","UIID":"uiid_Country_101","Enabled":false,"Pattern":"101","Percentage":-1},{"Key":null,"Value":"巴林","UIID":"uiid_Country_102","Enabled":false,"Pattern":"102","Percentage":-1},{"Key":null,"Value":"孟加拉国","UIID":"uiid_Country_103","Enabled":false,"Pattern":"103","Percentage":-1}],"Enabled":false}');
    ko.applyBindings(new ViewModel(data.KVPEList));

});

代码下载:http://files.cnblogs.com/badnewfish/modalWindowKO.rar

KO+bootstrap 模态窗全选绑定的更多相关文章

  1. BootStrap iCheck插件全选与获取value值的解决方法

    这篇文章主要介绍了BootStrap iCheck插件全选与获取value值的解决方法,解决方法其实很简单,下面小编给大家分享下这方面的知识 在使用jQuery iCheck 插件的时候遇到了一个问题 ...

  2. 解决jquery-ui-autocomplete选择列表被Bootstrap模态窗遮挡的问题

    最近在一个ASP.NET MVC5项目中使用Bootstrap的模态窗(弹出层)来让用户填写内容,其中的一个编辑框提供了自动完成功能,用jQuery UI Autocomplete来实现. 因为我是W ...

  3. jQuery ui autocomplete选择列表被Bootstrap模态窗遮挡的完美解决方法

    转:http://www.cnblogs.com/wiseant/p/4553837.html 最近在一个ASP.NET MVC5项目中使用Bootstrap的模态窗(弹出层)来让用户填写内容,其中的 ...

  4. bootstrap实现checkbox全选、取消全选

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <!-- 最新版本的 ...

  5. 使用AngularJS实现简单:全选和取消全选功能

    这里用到AngularJS四大特性之二----双向数据绑定 注意:没写一行DOM代码!这就是ng的优点,bootstrap.css为了布局,JS代码也只是简单创建ng模块和ng控制器 效果: < ...

  6. MVC身份验证.MVC过滤器.MVC6关键字Task,Async.前端模拟表单验证,提交.自定义匿名集合.Edge导出到Excel.BootstrapTree树状菜单的全选和反选.bootstrap可搜索可多选可全选下拉框

    1.MVC身份验证. 有两种方式.一个是传统的所有控制器继承自定义Control,然后再里面用MVC的过滤器拦截.所以每次网站的后台被访问时.就会先走入拦截器.进行前端和后端的验证 一个是利用(MVC ...

  7. Bootstrap, 模态框实现值传递,自动勾选

    目录 Bootstrap,模态框自动勾选,值传递 1.父页面 2. 子页面(modal) 模态框 Bootstrap,模态框自动勾选,值传递 场景: ​ 有一个这样的需求, 在父页面有一个table, ...

  8. FlexiGrid 使用 全选、自动绑定

    1.介绍 Flexigrid是一个类似于Ext Gird,但基于jQuery开发的Grid.它具有的功能包括:可以调整列宽,合并列标题,分页,排序,显示/隐藏表格等.Flexigrid显示的数据能够通 ...

  9. Bootstrap模态弹出窗

    Bootstrap模态弹出窗有三种方式: 1.href触发模态弹出窗元素: <a class="btn btn-primary" data-toggle="moda ...

随机推荐

  1. redhat网络基础配置

    添加浮动IP: ifconfig eth0:1 192.168.1.106 IP配置文件: BOOTPROTO: 采用的启动协议,有三种选择: (1) none:不使用启动协议 (2) static: ...

  2. Windows Store App 偏移特效

    通过前面讲解的内容,读者已经了解了如何在三维空间中使旋转对象绕指定的旋转中心旋转一定的角度.接下来在这个基础上进一步讲解如何对旋转对象进行平移.下面首先介绍一下用到的几个属性. q  LocalOff ...

  3. WCF初探-17:WCF数据协定之等效性

    数据协定等效性特点概述 对于客户端要将某种类型的数据成功发送到服务,或者服务要将数据成功发送到客户端的情况,接收端上并不一定必须存在此发送数据类型. 唯一的要求是两种类型的数据协定应该等效. 要使数据 ...

  4. SQLSERVER存储过程基本语法

    一.定义变量 --简单赋值 declare @a int set @a=5 print @a --使用select语句赋值 declare @user1 nvarchar(50) select @us ...

  5. 谈谈UIView的几个layout方法

    谈谈UIView的几个layout方法-layoutSubviews.layoutIfNeeded.setNeedsLayout...   最近在学习swift做动画,用到constraint的动画, ...

  6. 单例模式(Singleton Pattern)

    动机: 在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例,才能确保它们的逻辑正确性.以及良好的效率. 如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例? 这应该是类设 ...

  7. MS Sql server 2008 学习笔记

    数据库中常用的概念 Sql本身是一个服务器,没有界面,Management Studio  只是一个SQL Server管理工具而已,不是服务器. Sql server 在管理工具下面的服务SQL S ...

  8. 绑定hosts

    测试过程中需绑定hosts.将测试环境IP绑定域名,输入域名即可连接到测试环境. 1  hosts文件地址:C:\WINDOWS\system32\drivers\etc 2  用记事本打开hosts ...

  9. Java实现字符串反转的8种方法

    /** * */ package com.wsheng.aggregator.algorithm.string; import java.util.Stack; /** * 8 种字符串反转的方法, ...

  10. How to implement a custom type for NHibernate property

    http://blog.miraclespain.com/archive/2008/Mar-18.html <?xml version="1.0" encoding=&quo ...