本篇文章結(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 href="http://www.see-source.com/bootstrap/css/bootstrap.css" 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>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點(diǎn)
疑難解答