国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Knockout結合Bootstrap創建動態UI實現產品列表管理

2019-11-20 09:00:03
字體:
來源:轉載
供稿:網友

本篇文章結合Bootstrap創建一個比較完整的應用,對產品列表進行管理,包括產品的增加、刪除、修改。 

需要的引用 

<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><!-- 動態生成產品列表 --><table class="table table-bordered"> <thead>  <tr>   <th>ID</th>   <th>產品名稱</th>   <th>原價</th>   <th>促銷價</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><!-- 產品添加form --><form class="form-horizontal" data-bind="submit:$root.create">  <fieldset>   <legend>添加產品</legend>   <div class="control-group">   <label class="control-label" for="input01">產品名稱</label>   <div class="controls">    <input type="text" name="Name" class="input-xlarge">   </div>   </div>   <div class="control-group">   <label class="control-label" for="input01">原價</label>   <div class="controls">    <input type="text" name="Price" class="input-xlarge">   </div>   </div>   <div class="control-group">   <label class="control-label" for="input01">促銷價</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':'聯想K900','Price':'3299','ActualCost':'3000'},{'Id':'222','Name':'HTC one','Price':'4850','ActualCost':'4500'}]);   self.products = ko.observableArray();  $.getJSON(baseUri + "list", self.products);//加載產品列表 //添加產品 self.create = function (formElement) {      $.post(baseUri + "add", $(formElement).serialize(), function(data) {    if(data.success){//服務器端添加成功時,同步更新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");  }  //修改產品 self.update = function (product) {  $.post(baseUri + "update", product, function(data) {    if(data.success){     alert("修改成功");    }  },"json");  }   //刪除產品 self.remove = function (product) {   $.post(baseUri + "delete", "productID="+product.Id, function(data) {    if(data.success){    //服務器端刪除成功時,UI中也刪除    self.products.remove(product);    }  },"json");    } }ko.applyBindings(new ProductsViewModel());</script>

如果大家還想深入學習,可以點擊這里進行學習,再為大家附3個精彩的專題:

Bootstrap學習教程

Bootstrap實戰教程

Bootstrap插件使用教程

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沁源县| 南昌市| 札达县| 鹤峰县| 庆元县| 布拖县| 嘉兴市| 柳江县| 加查县| 布拖县| 光山县| 泉州市| 乌兰浩特市| 香格里拉县| 麟游县| 水富县| 广宁县| 金坛市| 阿瓦提县| 千阳县| 云霄县| 海原县| 当涂县| 龙里县| 肥西县| 繁昌县| 泰安市| 花垣县| 尖扎县| 涟源市| 资源县| 临澧县| 陆丰市| 邳州市| 巢湖市| 诸城市| 宽城| 济源市| 依兰县| 芒康县| 台南市|