2011-03-30 22 views
2

正在使用jquery購物車http://plugins.jquery.com/project/smartcart2 任何人都可以幫助我實現onAdded,onRemoved,onUpdated方法,以使Ajax調用來維護服務器端的會話。 無論何時添加/刪除/更新對象並以JSON格式獲取數據,我都會在服務器上發出Ajax請求。我更新數量像obj.attr("qty", eval(msg)[1]);與來自服務器的更新。 但是,如果我點擊刷新或表格數據重新填充,購物車項目不再存在。所以,真正的問題是如何填充從會話的數據,這樣的產品仍然在刷新等Jquery購物車插件:實現會話管理

$('#SmartCart').smartCart({ 
onAdded: function(pObj,quantity){ cartAdded(pObj,quantity);}, 
onRemoved: function(pObj){ cartRemoved(pObj);}, 
onUpdated: function(pObj,quantity){ cartUpdated(pObj,quantity); }, 
}); 

function cartAdded(obj,qty){ 
var product_id = obj.attr("pid"); 
var quantity = qty; 
// Ajax calls for adding product to cart 
function(pObj,quantity){ 
    cartAdded(pObj,quantity);} 
} 

function cartRemoved(obj){ 
var product_id = obj.attr("pid"); 
// Ajax call for removing product from cart 
} 

function cartUpdated(obj,qty){ 
var product_id = obj.attr("pid"); 
var quantity = qty; 
// Ajax call for updating product on cart 
} 

function cartAdded(obj,qty){ 
      var partNum = obj.attr("partNumber"); 
      var quantity = qty;     
     $.ajax({ 
     type: 'POST', 
     url: "json/sessionManager", 
     data : "partNum=" + partNum + "&qty=" + quantity, 
     dataType: "text/json", 
     success: function(msg){ 
      obj.attr("qty", msg[1]); 
     }, 
     error: function(httpRequest, textStatus, errorThrown) { 
      alert("status=" + textStatus + ",error=" + errorThrown); 
     } 
    }); 
     } 

我將高度讚賞大約在同一建議留在購物車。

回答

4
  1. 在服務器端實現您的購物車。
  2. 保持您的購物車對象(產品)在一個集合或其他東西。
  3. 每當一個Ajax請求到達服務器端:從請求

    • 獲取數據(產品編號,數量,...等)。
    • 獲取操作類型(添加,刪除..)。
    • 檢查數據有效性。
    • 做必需的服務器端業務(更新數據庫,調用Web服務等)。
    • 如果一切正常,請將指定產品添加到/從您的收藏中刪除。
    • 將您的服務器端購物車轉換爲JSON並寫入Ajax調用的響應。
  4. 每次JSON響應到達客戶端:

    • 更新您的新的數據車。

對於阿賈克斯的一部分。使用jQuery Ajax()功能

$.ajax({ 
    type: "POST", 
    url: "cart.jsp", 
    data: "p_id=SKU001&quantity=4", 
    success: function(msg){ 
    alert("FOUR SKU001 ADDED TO CART"); 
    } 
}); 

編輯:噢,我看到的。 product_id未定義表示您的obj.attr("pid");不起作用。

此插件爲產品定義使用隱藏的HTML輸入。 (如果你問我,很簡單) 這個輸入有一些屬性,你可以通過obj.attr("pid");得到。 如果您的表單中沒有輸入或您的輸入沒有屬性,則代碼失敗。

請確保你隱藏的HTML輸入有這個PSEUDO屬性。

例如:

<div id="SmartCart" class="scMain"> 
    <input type="hidden" pimage="products/product1.jpg" pprice="2299.99" pdesc="" 
    pcategory="Computers" pname="Apple MacBook Pro MA464LL/A 15.4" pid="100"> 

    <input type="hidden" pimage="products/product6.jpg" pprice="2699.99" pdesc="" 
    pcategory="Computers" pname="Sony VAIO 11.1&quot; Notebook PC" pid="101"> 
    <input type="hidden" pimage="products/product3.jpg" pprice="550.00" pdesc="" 
    pcategory="Cameras" pname="Canon Digital Rebel" pid="102"> 
</div> 

從作者的文檔:

說明:文本標記大膽是描述了關於產品的屬性。如產品名稱,價格,描述等

  • PID:產品ID
  • PNAME:產品名稱產品
  • pdesc:產品
  • pprice的說明:產品價格
  • pimage:產品圖片s烏爾斯河
  • pcategory:產品類別

您可以通過添加新的屬性到輸入元素添加更多的產品細節,所以您可以通過編輯模板展示他們的產品清單或購物車的。 您可以自定義僞屬性名稱,並在「屬性設置」部分的插件文件中對其進行配置。

編輯2:我認爲您在本部分建議中遇到困難。

每次JSON響應到達客戶端:

更新您的新的數據車。

以下隱藏的輸入是您的數據。 在計算器打開另一個問題,並要求

我如何修改(創建,更新,刪除)該輸入jQuery中

<div id="SmartCart" class="scMain"> 
    <input type="hidden" pimage="products/product1.jpg" pprice="2299.99" pdesc="" 
    pcategory="Computers" pname="Apple MacBook Pro MA464LL/A 15.4" pid="100"> 

    <input type="hidden" pimage="products/product6.jpg" pprice="2699.99" pdesc="" 
    pcategory="Computers" pname="Sony VAIO 11.1&quot; Notebook PC" pid="101"> 
    <input type="hidden" pimage="products/product3.jpg" pprice="550.00" pdesc="" 
    pcategory="Cameras" pname="Canon Digital Rebel" pid="102"> 
</div> 

因爲解決你的第一個問題,問另一個在同一職位後是不好的做法

+0

您正在使用SmartCart或SmartCart2的哪一個? – 2011-03-31 00:34:54

+0

智能Cart2。需要更多與步驟4相關的輸入。 – 2011-03-31 00:43:15

+0

你可以檢查我更新的帖子。刷新或重新填充數據後,數據將丟失。所以,真正的問題是如何從會話中填充購物車數據,以便它不會丟失。將非常感謝相同的建議。 – 2011-03-31 03:06:05