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

首頁(yè) > 開(kāi)發(fā) > JS > 正文

Knockout結(jié)合Bootstrap創(chuàng)建動(dòng)態(tài)UI實(shí)現(xiàn)產(chǎn)品列表管理

2024-05-06 16:31:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本篇文章結(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)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 柳河县| 石门县| 长海县| 利津县| 佛山市| 若羌县| 阜阳市| 永定县| 民县| 安化县| 沂南县| 龙泉市| 瑞昌市| 永德县| 陆河县| 莎车县| 铜鼓县| 永丰县| 宝清县| 区。| 大安市| 五华县| 西和县| 汶上县| 高陵县| 江阴市| 沅陵县| 搜索| 嘉善县| 福建省| 万源市| 离岛区| 克什克腾旗| 沾益县| 房产| 花莲市| 长宁区| 沁水县| 平山县| 雷波县| 南昌县|