[Vue.js] 在 ajax 上傳後取得 data 的 Object
最近在學 Vue.js,遇到一些問題解決了做點小筆記。
寫了一個 ajax 上傳的功能,上傳 csv 後會自動產生圖表,並可以藉由左邊的列表增加刪減項目,上傳 csv 的程式碼如下。
html 則為這樣
寫了一個 ajax 上傳的功能,上傳 csv 後會自動產生圖表,並可以藉由左邊的列表增加刪減項目,上傳 csv 的程式碼如下。
- new Vue({
- el: '#app',
- data: {
- list: [
- ]
- },
- methods: {
- uploadCsv: function(){
- csv = $("#csv_file")[0].files;
- var data = new FormData();
- data.append("csv_file", csv[0]);
- var self = this;
- $.ajax({
- url: 'upload-csv.php',
- type: 'POST',
- data: data,
- cache: false,
- processData: false, // Don't process the files
- contentType: false, // Set content type to false as jQuery will tell the server its a query string request
- success: function(data, textStatus, jqXHR)
- {
- $csvData=data; //return string
- d3.csv($csvData, function(data) {
- self.list=data;
- arrayLength=self.list.length;
- for($i=0;$i<arrayLength;$i++){
- self.list[$i]["color"]=color($i);
- }
- svg.remove();
- svg = d3.select("#chart").append("svg").attr("width",400).attr("height",300);
- svg.append("g").attr("id", "salesDonut");
- Donut3D.draw("salesDonut", self.list, 150, 150, 130, 100, 30, 0.4);
- var info = svg.selectAll("g.info")
- .data(self.list)
- .enter()
- .append("g")
- .attr("class", "info")
- .attr("transform", "translate(" + 300 + "," + 50 + ")");
- info.append("text")
- .attr("x", 10)
- .attr("y", function(d, i){
- return i*24;
- })
- .attr("fill", function(d, i) {
- return color(i);
- })
- .text(function(d, i) {
- return d.label+": "+d.value;
- });
- });
- if(typeof data.error === 'undefined')
- {
- // Success so call function to process the form
- }
- else
- {
- // Handle errors here
- console.log('ERRORS: ' + data.error);
- }
- },
- error: function(jqXHR, textStatus, errorThrown)
- {
- // Handle errors here
- console.log('ERRORS: ' + textStatus);
- // STOP LOADING SPINNER
- }
- });
- }
- }
html 則為這樣
- <div class="row" id="app">
- <div class="col-md-6">
- <form class="form-inline">
- <div class="form-group">
- <label for="">上傳 csv: </label><input name="csv_file" type="file" id="csv_file">
- </div>
- <a role="button" class="btn btn-primary" id="upload_csv" v-on:click="uploadCsv">確認上傳</a>
- </form>
- <form class="form-inline">
- <div class="form-group">
- <input type="text" class="form-control" id="d_item" placeholder="項目" v-model="label">
- <div class="input-group">
- <input type="text" class="form-control" id="d_val" placeholder="值" v-model="value" number>
- <span class="input-group-btn">
- <button class="btn btn-primary" id="add_data" type="button" v-on:click="addItem">新增</button>
- </span>
- </div><!-- /input-group -->
- </div>
- </form>
- <ul class="list-group" id="item_list" >
- <li v-for="item in list" data-item='{{ item.label }}' data-val='{{ item.value }}' class='list-group-item'>{{ item.label }}: {{ item.value }}<span v-on:click="removeItem($index)" class='badge'>x</span></li>
- </ul>
- <a role="button" class="btn btn-primary" id="btn_generate" v-on:click="generateChart()">Generate</a> <a role="button" class="btn btn-primary">Save</a>
- </div><!-- col-md-6 -->
- <div class="col-md-6">
- <div id="chart"></div>
- </div>
- </div><!-- row -->
留言