`
sillycat
  • 浏览: 2560397 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Front End Start(4)knockoutjs Paged List and Creation

    博客分类:
  • UI
 
阅读更多

Front End Start(4)knockoutjs Paged List and Creation

knockoutjs CRUD
http://sillycat.iteye.com/blog/2152090
http://sillycat.iteye.com/blog/2152092

1. Paged List
http://tech.pro/tutorial/1235/handling-paged-datasets-in-knockoutjs
http://jsfiddle.net/rniemeyer/5Xr2X/

Here is the Key features for Pager
function Metro(data) {
        this.name = ko.observable(data.name);
        this.path = ko.observable(data.path);
        this.latitude = ko.observable(data.latitude);
        this.longitude = ko.observable(data.longitude);
    }

    function MetroListViewModel() {
        // Data
        var self = this;
        self.items = ko.observableArray([]);

        //Loading
        Auth.request({
            type: 'GET',
            url:  "/data/metros.json",
            success: function(allData) {
                var mappedItems = $.map(allData, function(item) {
                    return new Metro(item)
                });
                self.items(mappedItems);
            }
        });

        // pager related stuff
        // ---------------------------------------------
        this.currentPage = ko.observable(1);
        this.perPage = 2;
        this.pagedItems = ko.computed(function(){
            var pg = this.currentPage(),
            start = this.perPage * (pg-1),
            end = start + this.perPage;
            return this.items().slice(start,end);
        }, this);
       
        this.nextPage = function(){
            if(this.nextPageEnabled())
                this.currentPage(this.currentPage()+1);
            };
            this.nextPageEnabled = ko.computed(function(){
            return this.items().length > this.perPage * this.currentPage();
        },this);
   
        this.previousPage = function(){
            if(this.previousPageEnabled())
                this.currentPage(this.currentPage()-1);
            };
            this.previousPageEnabled = ko.computed(function(){
            return this.currentPage() > 1;
        },this);
    }

    ko.applyBindings(new MetroListViewModel());

The HTML using bootstrap

      <h1>Metros list
        <a href="#" class="btn btn-primary pull-right">
          Create
        </a>
      </h1>

      <ul class="pager">
        <li data-bind="css: {'disabled': !previousPageEnabled()}" class="previous">
          <a data-bind="click: previousPage" href="#">← Previous</a>
        </li>
        <li data-bind="css: {'disabled': !nextPageEnabled()}" class="next">
          <a data-bind="click: nextPage" href="#">Next →</a>
        </li>
      </ul>

      <table class="table table-bordered table-striped">
        <thead>
          <tr>
            <th>Name</th>
            <th>Path</th>
            <th>Latitude</th>
            <th>Longitude</th>
            <th>Actions</th>
          </tr>
        </thead>
   
        <tbody data-bind="foreach: pagedItems, visible: items().length > 0">

          <tr>
            <td class="col-md-3">
                <a href="#" data-bind="click: $parent.viewItem">
                  <span data-bind="text: name" />
                </a>
            </td>
            <td class="col-md-3"><span data-bind="text: path" /></td>
            <td class="col-md-2"><span data-bind="text: latitude" /></td>
            <td class="col-md-2"><span data-bind="text: longitude" /></td>
            <td class="col-md-6">
                <div class="btn-group">
                    <a class="btn btn-primary" href="#" data-bind="click: $parent.editItem">  Edit
                    </a>
                    <button class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
                      <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                        <li><a href="#"
                               data-bind="click: $parent.delItem">
                               Delete
                            </a>
                        </li>
                    </ul>
                </div>
            </td>
          </tr>
           
        </tbody>
      </table>

      <ul class="pager">
        <li data-bind="css: {'disabled': !previousPageEnabled()}" class="previous">
          <a data-bind="click: previousPage" href="#">← Previous</a>
        </li>
        <li data-bind="css: {'disabled': !nextPageEnabled()}" class="next">
          <a data-bind="click: nextPage" href="#">Next →</a>
        </li>
      </ul>

2. Other CRUD Operation
Binding
http://knockoutjs.com/documentation/foreach-binding.html
http://knockoutjs.com/documentation/with-binding.html

Confirm and Popup Window
http://jsfiddle.net/J4UWX/
http://jsfiddle.net/rniemeyer/CLxsV/
http://stackoverflow.com/questions/25034120/knockout-js-implementing-delete-confirmation-box-on-delete-click

Edit and Popup Window
http://jsfiddle.net/rniemeyer/WpnTU/
http://cgeers.com/2012/10/06/single-page-application-with-knockout-js-part-1/

For Create
        <a data-bind="click: $parent.editItem" href="#" class="btn btn-primary pull-right">
          Create
        </a>

        <div data-bind="with: itemCreateView">
       
        <form data-bind="submit: $parent.itemSave" class="form-horizontal" role="form">
          <div class="form-group">
            <label for="input1" class="col-sm-1 control-label">&nbsp;</label>
            <h1 class="text-left">Metro Creation</h1>
          </div>
          <div class="form-group">
            <label for="input2" class="col-sm-1 control-label">Name</label>
            <div class="col-sm-5">
              <input data-bind="value: name, enable: actionType != 'view'"
                     class="form-control" id="input2" placeholder="name">
              <input data-bind="value: id" type="hidden"/>
            </div>
          </div>
          <div class="form-group">
            <label for="input3" class="col-sm-1 control-label">Path</label>
            <div class="col-sm-5">
              <input data-bind="value : path,enable: actionType != 'view'"
                     class="form-control" id="input3" placeholder="path">
            </div>
          </div>
          <div class="form-group">
            <label for="input4" class="col-sm-1 control-label">Latitude</label>
            <div class="col-sm-5">
              <input data-bind="value : latitude, enable: actionType != 'view'"
                     class="form-control" id="input4" placeholder="latitude">
            </div>
          </div>
          <div class="form-group">
            <label for="input5" class="col-sm-1 control-label">Longitude</label>
            <div class="col-sm-5">
              <input data-bind="value : longitude, enable: actionType != 'view'"
                     class="form-control" id="input5" placeholder="longitude">
            </div>
          </div>

          <div class="form-group">
            <div data-bind="ifnot: actionType == 'view'" class="col-sm-offset-1 col-sm-1">
              <button type="submit" class="btn btn-primary btn-lg active">
                <span data-bind="if: actionType == 'create'">
                  Create
                </span>
                <span data-bind="if: actionType == 'update'">
                  Update
                </span>
              </button>
            </div>
            <div class="col-sm-offset-1 col-sm-1">
               <button data-bind="click: $parent.itemList"type="button" class="btn btn-default btn-sm active">Back</button>
            </div>
          </div>
        </form>
      </div>

    function MetroListViewModel() {
        var self = this;
        …snip...

        self.id = ko.observable();
        self.name = ko.observable();
        self.path = ko.observable();
        self.latitude = ko.observable();
        self.longitude = ko.observable();
        self.actionType = 'create';

        self.editItem = function(data) {
            self.itemListView(null);
            if(data.id() == undefined){
                data.actionType = 'create';
            }else{
                data.actionType = 'update';
            }
            self.itemCreateView(data);
        };

        self.itemSave = function() {
            var id = this.id();
            if(id == undefined){
                $.ajax({
                    type: 'POST',
                    url:  parameters.api.host + "/api/v1/metros.json",
                    data: {
                        "campeasy_campeasyshared_metrotype[name]": this.name(),
                        "campeasy_campeasyshared_metrotype[path]": this.path(),
                        "campeasy_campeasyshared_metrotype[latitude]": this.latitude(),
                        "campeasy_campeasyshared_metrotype[longitude]": this.longitude()
                    },
                    success: function() {
                        self.itemList();
                    }
                });   
            }else{
                …for UPDATE...
            }
        };

3. Sammy.js
http://learn.knockoutjs.com/#/?tutorial=webmail
http://sammyjs.org/
TODO...

4. jquery-deparam
https://github.com/AceMetrix/jquery-deparam
TODO...

5. momentjs
http://momentjs.com/
TODO...

References:
https://github.com/AceMetrix/jquery-deparam
http://momentjs.com/

 

分享到:
评论

相关推荐

    Asp.Net MVC 4 KnockoutJS 框架实例

    Asp.Net MVC 4 和 KnockoutJS 是两个在Web开发领域广泛应用的技术,它们结合使用能够构建出高效、动态且用户友好的Web应用。Asp.Net MVC 4 是Microsoft推出的一个强大的MVC(Model-View-Controller)框架,用于构建...

    KnockoutJS Web Development(PACKT,2015)

    By the end of the book, you will have knowledge of all the different functionalities KnockoutJS has to offer. ***** Who This Book Is For ***** This book is for web developers and designers who work ...

    KnockoutJS

    4. **依赖跟踪**:KnockoutJS内置了依赖跟踪机制,能够自动检测哪些视图依赖于哪个模型,因此在模型更新时,只更新相关的部分,而不是整个页面。 5. **模板引擎**:KnockoutJS提供了一种简洁的模板系统,允许开发者...

    KnockoutJS Essentials(PACKT,2015)

    KnockoutJS Essentials gives you an insight into the best practices when designing and building MVVM web applications using the KnockoutJS framework. Packed with real-world tasks, this book starts ...

    KnockoutJs 帮助文档 中文手册

    **KnockoutJS** 是一个轻量级的JavaScript库,专为构建动态用户界面而设计。它使用MVVM(Model-View-ViewModel)模式,帮助开发者实现数据绑定和依赖跟踪,使得视图与数据模型之间的交互变得更加简单。中文手册提供...

    [KnockoutJS] KnockoutJS 入门教程 (英文版)

    [Packt Publishing] KnockoutJS 入门教程 (英文版) [Packt Publishing] KnockoutJS Starter (E-Book) ☆ 出版信息:☆ [作者信息] Eric M. Barnard [出版机构] Packt Publishing [出版日期] 2012年11月23日 ...

    knockoutjs2.1.0

    knockoutjs,前台MVVM模式实现框架。knockoutjs能使前台数据源与html页面分离开来,让程序员专注于数据的变化。详情请参照knockoutjs.com。本文件于2012-7-31下载于knockout官方网站,版本号2.1.0

    knockoutjs.chm 网站原版API and 示例

    从knockoutjs官网制作的可离线使用的API和示例。

    KnockoutJS中文文档新

    **KnockoutJS** 是一个轻量级的MVVM(Model-View-ViewModel)JavaScript库,主要用于构建富交互的Web应用程序。它通过数据绑定和依赖跟踪机制,使得开发者能够更轻松地管理DOM(Document Object Model)与应用程序...

    KnockoutJS 官方文档汉化版

    KnockoutJs官方文档汉化完整版+Knockout精品文章,绝对超值

    ASP.NET MVC 4, EF, Knockoutjs and Bootstrap

    ASP.NET MVC 4、Entity Framework (EF)、Knockout.js 和 Bootstrap 是现代Web开发中的四个关键技术,它们各自在构建高效、响应式且数据驱动的Web应用中扮演着重要角色。 ASP.NET MVC 4 是微软提供的一个开源框架,...

    KnockoutJs_帮助文档_中文手册 chm

    **KnockoutJS** 是一个轻量级的JavaScript库,专为构建富客户端应用程序而设计。它使用MVVM(Model-View-ViewModel)模式,帮助开发者实现数据绑定和动态界面更新,大大简化了DOM操作。这个"KnockoutJs_帮助文档_...

    WebApi+Bootstrap+KnockoutJs打造单页面程序

    3. KnockoutJs是一个让开发者可以使用模型-视图-视图模型(Model-View-ViewModel,简称MVVM)模式的JavaScript库,这样可以通过声明式的绑定数据来构建富交互的Web应用程序。 SPA的优点包括: - 用户体验更好:...

    js knockoutjs 全国省市区三级联动

    JavaScript 和 KnockoutJS 是两种广泛应用于前端开发的技术。在网页应用中,经常需要实现省市区三级联动的效果,即用户选择一个省份后,市和区的数据会根据所选省份动态加载和更新。这种功能提升了用户体验,使得...

    KnockoutJS_3.xx版本

    Knockout(简称KO)是一个JavaScript库,可以帮助您用干净的底层数据模型创建丰富的反应迅速显示和编辑用户界面。任何时候你有UI的部分是动态更新(例如,根据用户的行为或者外部数据源的变化而变化),用KO可以帮助...

    KnockoutJS学习文档中文版

    网上这方面的资料很少,找了大半天才找到这么一个.希望对大家有用

    asp.net mvc4+easyui+knockoutjs(建筑材料管理系统)

    总结来说,这个压缩包提供了一个基于ASP.NET MVC4、EasyUI和KnockoutJS的建筑材料管理系统实例,对于学习和理解这三者之间的协作以及如何构建企业级Web应用具有很高的参考价值。开发者可以从中学到如何设计高效的...

Global site tag (gtag.js) - Google Analytics