Knockout結(jié)合Bootstrap創(chuàng)建動(dòng)態(tài)UI實(shí)現(xiàn)產(chǎn)品列表管理
本篇文章結(jié)合Bootstrap創(chuàng)建一個(gè)比較完整的應(yīng)用,對(duì)產(chǎn)品列表進(jìn)行管理,包括產(chǎn)品的增加、刪除、修改。
需要的引用
<script type='text/javascript' src='http://www.see-source.com/js/knockout-2.2.0.js'></script> <script type='text/javascript' src='http://www.see-source.com/js/jquery-1.6.2.min.js'></script> <link rel="stylesheet">
Html代碼
<body>
<!-- 動(dòng)態(tài)生成產(chǎn)品列表 -->
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>產(chǎn)品名稱</th>
<th>原價(jià)</th>
<th>促銷價(jià)</th>
<th>操作</th>
</tr>
</thead>
<tbody data-bind="foreach: products">
<tr >
<td>
<span data-bind="text: $data.Id"></span>
</td>
<td>
<input type="text" data-bind="value: $data.Name"/>
</td>
<td>
<input type="text" data-bind="value: $data.Price"/>
</td>
<td>
<input type="text" data-bind="value: $data.ActualCost"/>
</td>
<td>
<input type="button" class="btn" value="修改" data-bind="click: $root.update"/>
<input type="button" class="btn" value="刪除" data-bind="click: $root.remove"/>
</td>
</tr>
</tbody>
</table>
<!-- 產(chǎn)品添加form -->
<form class="form-horizontal" data-bind="submit:$root.create">
<fieldset>
<legend>添加產(chǎn)品</legend>
<div class="control-group">
<label class="control-label" for="input01">產(chǎn)品名稱</label>
<div class="controls">
<input type="text" name="Name" class="input-xlarge">
</div>
</div>
<div class="control-group">
<label class="control-label" for="input01">原價(jià)</label>
<div class="controls">
<input type="text" name="Price" class="input-xlarge">
</div>
</div>
<div class="control-group">
<label class="control-label" for="input01">促銷價(jià)</label>
<div class="controls">
<input type="text" name="ActualCost" class="input-xlarge">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">保存</button>
<button class="btn">取消</button>
</div>
</fieldset>
</form>
</body>
js代碼
<script type="text/javascript">
function ProductsViewModel() {
var baseUri = 'http://localhost:8080/knockout/';
var self = this;
//self.products = ko.observableArray([{'Id':'111','Name':'聯(lián)想K900','Price':'3299','ActualCost':'3000'},{'Id':'222','Name':'HTC one','Price':'4850','ActualCost':'4500'}]);
self.products = ko.observableArray();
$.getJSON(baseUri + "list", self.products);//加載產(chǎn)品列表
//添加產(chǎn)品
self.create = function (formElement) {
$.post(baseUri + "add", $(formElement).serialize(), function(data) {
if(data.success){//服務(wù)器端添加成功時(shí),同步更新UI
var newProduct = {};
newProduct.Id = data.ID;
newProduct.Name = formElement.Name.value;
newProduct.Price = formElement.Price.value;
newProduct.ActualCost = formElement.ActualCost.value;
self.products.push(newProduct);
}
},"json");
}
//修改產(chǎn)品
self.update = function (product) {
$.post(baseUri + "update", product, function(data) {
if(data.success){
alert("修改成功");
}
},"json");
}
//刪除產(chǎn)品
self.remove = function (product) {
$.post(baseUri + "delete", "productID="+product.Id, function(data) {
if(data.success){
//服務(wù)器端刪除成功時(shí),UI中也刪除
self.products.remove(product);
}
},"json");
}
}
ko.applyBindings(new ProductsViewModel());
</script>
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊這里進(jìn)行學(xué)習(xí),再為大家附3個(gè)精彩的專題:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Bootstrap源碼解讀媒體對(duì)象、列表組和面板(10)
- Bootstrap CSS布局之列表
- Bootstrap基本組件學(xué)習(xí)筆記之列表組(11)
- 基于jQuery和Bootstrap框架實(shí)現(xiàn)仿知乎前端動(dòng)態(tài)列表效果
- BootStrap實(shí)現(xiàn)郵件列表的分頁和模態(tài)框添加郵件的功能
- bootstrap下拉列表與輸入框組結(jié)合的樣式調(diào)整
- jQuery ui autocomplete選擇列表被Bootstrap模態(tài)窗遮擋的完美解決方法
- 利用ASP.NET MVC和Bootstrap快速搭建個(gè)人博客之后臺(tái)dataTable數(shù)據(jù)列表
- 深入淺析Bootstrap列表組組件
- Bootstrap列表組學(xué)習(xí)使用
相關(guān)文章
JS SetInterval 代碼實(shí)現(xiàn)頁面輪詢
setInterval 是一個(gè)實(shí)現(xiàn)定時(shí)調(diào)用的函數(shù),可按照指定的周期(以毫秒計(jì))來調(diào)用函數(shù)或計(jì)算表達(dá)式。下面通過本文給大家分享JS SetInterval 代碼實(shí)現(xiàn)頁面輪詢,感興趣的朋友一起看看吧2017-08-08
原生js實(shí)現(xiàn)Flappy Bird小游戲
這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)Flappy Bird小游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
JavaScript中的事件監(jiān)聽詳細(xì)介紹
這篇文章主要給大家介紹了關(guān)于JavaScript中事件監(jiān)聽的相關(guān)資料,在前端開發(fā)過程中我們經(jīng)常會(huì)遇到給頁面元素添加事件的問題,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
一直都需要的復(fù)制到系統(tǒng)剪貼板之IE,firefox兼容版
一直都需要的復(fù)制到系統(tǒng)剪貼板之IE,firefox兼容版...2007-09-09
Bootstrap 附加導(dǎo)航(Affix)插件實(shí)例詳解
附加導(dǎo)航(Affix)插件允許某個(gè) <div> 固定在頁面的某個(gè)位置。接下來通過本文給大家介紹Bootstrap 附加導(dǎo)航(Affix)插件實(shí)例詳解,感興趣的朋友一起看看吧2016-06-06
微信小程序網(wǎng)絡(luò)請(qǐng)求的封裝與填坑之路
本文主要介紹了關(guān)于小程序網(wǎng)絡(luò)請(qǐng)求的封裝的相關(guān)資料。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04
Javascript實(shí)現(xiàn)返回上一頁面并刷新的小例子
這篇文章主要介紹了Javascript實(shí)現(xiàn)返回上一頁面并刷新的小例子,有需要的朋友可以參考一下2013-12-12

