angular js 配合ajax 實現購物車功能
阿新 • • 發佈:2019-01-02
<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Gowu extends Controller
{
public function index()
{
$data=db('shuju')
->field("name,price,number,done")
->select();
return view('index');
}
public function ajaxData(){
$data=db('shuju')
->field("name,price,number,done")
->select();
echo json_encode($data);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的購物車</title>
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js" ></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body ng-app="myapp" ng-controller="myCtrl">
<h1>我的購物車</h1>
<p ng-show="shopping">您的購物車為空<a href="#" ng-click="showS()">去逛商城</a></p>
<div class="nav">
<button ng-click="clearShpooing()" ng-show="show">清空購物車</button>
</div>
<!--建立購物車列表-->
<table ng-show="show">
<thead>
<th><input type="checkbox" ng-model="check" ng-click="delAll(check)"></th>
<th>name</th>
<th>price</th>
<th>number</th>
<th>totalprice</th>
<th>option</th>
</thead>
<tbody>
<!--ng-repeat渲染資料到介面-->
<tr ng-repeat="item in data">
<td><input type="checkbox" ng-model="item.done" ng-click="checkS()"></td>
<td>{{item.name}}</td>
<td>{{item.price | currency:"¥"}}</td>
<td>
<button ng-click="jian($index)">-</button>
<input type="text" ng-model="item.number" ng-checked="dianji()">
<button ng-click="add($index)">+</button>
</td>
<td>{{item.price*item.number |currency:"¥"}}</td>
<td>
<button ng-click="remove($index)">刪除</button>
</td>
</tbody>
<tfoot>
<tr>
<!--價錢起前面加"¥"-->
<td colspan="6">總價為:{{allMoney | currency:"¥"}}</td>
</tr>
</tfoot>
</table>
<script>
var myapp = angular.module("myapp", []);
myapp.controller("myCtrl", function ($scope,$http) {
$http({
method: 'POST',
url: "{:url('Gowu/ajaxData')}",
dataType: 'JSON'
}).then(function successCallback(response) {
//自擬商品資訊
$scope.data = response.data;
//點選加號的方法
$scope.add = function (index) {
$scope.data[index].number++;
$scope.sum();
}
//點選減號的方法
$scope.jian = function (index) {
//點選-操作時,當商品數量為1時,彈出對話方塊,詢問是否移除
if ($scope.data[index].number == 1) {
if (confirm("您是否將該商品移除購物車?")) {
$scope.data[index].number--;
$scope.data.splice(index,1);
$scope.sum();
}
} else if ($scope.data[index].number > 1) {
$scope.data[index].number--;
$scope.sum();
}
}
//計算商品總價的方法
$scope.sum = function () {
$scope.allMoney = 0;
for (var i = 0; i < $scope.data.length; i++) {
$scope.allMoney += $scope.data[i].price * $scope.data[i].number;
}
}
$scope.sum();
//點選輸入框的方法
$scope.dianji = function () {
$scope.sum();
}
$scope.shopping = false;
//預設全選是不選的
$scope.check = false;
//刪除全部商品的方法
$scope.delAll = function (check) {
$scope.checkD(check);
}
$scope.checkD = function (state) {
for (var i = 0; i < $scope.data.length; i++) {
$scope.data[i].done = state;
}
}
// //下面的選框代表
$scope.checkSS = false;
$scope.checkS = function () {
$scope.flag = 0;
for (var i = 0; i < $scope.data.length; i++) {
if ($scope.data[i].done == true) {
$scope.flag++;
$scope.checkSS = true;
}
}
//實現當下面全部選中,全選框選中的效果
if ($scope.flag == $scope.data.length) {
$scope.check = true;
} else {
$scope.check = false;
}
}
//判斷全選框下面的多選框有幾個的方法
//清空購物車的方法
$scope.clearShpooing = function () {
if ($scope.check == true || $scope.checkSS == true) {
for (var i = 0; i < $scope.data.length; i++) {
if ($scope.data[i].done == true) {
$scope.data.splice(i--, 1);
if ($scope.data.length == 0) {
$scope.show = false;
$scope.shopping = true;
}
$scope.sum();
}
}
if ($scope.check == true) {
$scope.data.length = 0;
$scope.show = false;
$scope.shopping = true;
}
} else {
alert("請先選擇要操作的商品");
}
}
$scope.show = true;
$scope.remove = function (index) {
$scope.data.splice(index, 1);
if ($scope.data.length == 0) {
$scope.show = false;
$scope.shopping = true;
}
}
//點選去逛商城的程式碼
$scope.showS=function () {
$scope.show = true;
$scope.check=false;
}
}, function errorCallback(response) {
alert('0');
});
});
</script>
</body>
</html>
動圖展示: