<div class="box">
<p>列表</p>
<div class="operation-box">
<div>
<label>ID:</label>
<input type="number" disabled="disabled" />
</div>
<div>
<label>姓名:</label>
<input type="text" v-model="name"/>
<button @click="add">OK</button>
</div>
<div>
<label>搜索</label>
<input type="text" v-model="keyWords"/>
</div>
</div>
<table>
<tr>
<th>ID</th>
<th>name</th>
<th>operation</th>
</tr>
<tr v-for="item in search(keyWords)" :key="item.id">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td><a href="#" @click="del(item.id)">刪除</a></td>
</tr>
</table>
</div>
var vm = new Vue({
el: ".box",
data: {
name: "",
keyWords:"",
list: [
{ id: 1, name: "tim" },
{ id: 2, name: "korn" },
{ id: 3, name: "leo" }
]
},
methods: {
add: function () {
this.list.push({ id: this.list.length+1, name: this.name });
},
del: function (id) {
var index = this.list.findIndex((item) => {
if (item.id === id) return true;
});
this.list.splice(index, 1);
},
search: function (keystr) {
if (keystr.trim() === "") {
return this.list;
}
else {
return this.list.filter(item => {
if (item.name.includes(keystr)) return item;
});
}
}
}
});
<div id="box">
{{msg}}
<button @click="sendGet">GetInfo</button> //發起get請求
<button @click="sendPost">PostInfo</button> //發起post請求
</div>
</body>
</html>
<script type="text/javascript">
var vm = new Vue({
el: "#box",
data: {
msg: "wait data",
sendData: {
name: "sam",
age:18
}
},
methods: {
sendGet: function () {
var self=this;
var getObj=axios.get("http://localhost:53385/Handler.ashx", {
params: self.sendData
});
var endObj= getObj.then(function (response) {
self.msg = response.data;
});
endObj.catch(function (error) {
self.msg = error.status + error.statusText;
});
},
sendPost: function () {
var self = this;
var postObj = axios.post("http://localhost:53385/Handler.ashx", self.sendData);
var endObj=postObj.then(function (response) {
self.msg = response.data;
});
endObj.catch(function (error) {
self.msg = error.status + error.statusText;
});
}
}
});
</script>
ublic class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("OK");
context.Response.End();
}
}
export
default
{
data
:
function
()
{
return
{
productList
:
[]
};
},
methods
:
{
get
:
function
()
{
var
self
=
this
;
self
.
$ajax
.get
(
"http://localhost:3000/src/json/productList.json"
)
.then
(
function
(
response
)
{
self
.
productList
=
response
.data
; //不用轉換數據格式,直接賦值給本地變量便可
})
.catch
(
function
(
error
)
{
Toast
(
"數據加載失敗"
+
error
);
});
}
},
created
:
function
()
{
this
.get
();
}
};