简体   繁体   English

我如何在单击按钮时使用 vue 在 json 文件中显示结果?

[英]How do i when click button show results in json file with vue?

Yes me again!又是我!

This is my project这是我的项目

I want to pull the data from the json file when I fill in the required fields and press the button.当我填写必填字段并按下按钮时,我想从 json 文件中提取数据。

For example, let's write the developer part to the jobs section.例如,让我们将开发人员部分写入作业部分。 then select istanbul as an option and click Find!.然后选择伊斯坦布尔作为选项并单击查找!。

 var app = new Vue({ el: "#app", data: { founded: [], search: "" }, created() { fetch("job.json") .then(res => { return res.json(); }) .then(res => { this.founded = res.items; }); }, computed:{ filteredFounded: function(){ return this.founded.filter((items)=> { return items.positionName.match(this.search) }); } } });
 <div class="header"> <h4>Get Job</h4> </div> <div id="app" class="nested"> <div class="card w-50"> <div class="search"> <input type="text" class="job" v-model="search" placeholder="Job..."> <select name="" class="city" id=""> <option value="Seçiniz">Seçiniz</option> <option value="İstanbul">İstanbul</option> <option value="Ankara">Ankara</option> <option value="İzmir">İzmir</option> <option value="Çanakkale">Çanakkale</option> </select> </div> <div class="find"> <button>Find!</button> </div> <div class="card-body" v-for="items in filteredFounded"> <h5 class="card-title">{{items.companyName}}</h5> <p class="card-text">{{items.positionName | to-uppercase}}</p> <p class="card-text">{{items.cityName}}</p> <p class="card-text">{{items.townName}}</p> <p class="card-text">{{items.distance}}</p> <a href="#" class=" btn-primary">Go!</a> </div> </div> </div> <script src="script.js"></script>

If I understand your issue:如果我理解你的问题:

  • the view updates on each form change since you bound the card-body repeating div directly to the filtering process, so the "Find!"由于您将card-body重复div直接绑定到过滤过程,因此每个表单更改的视图都会更新,因此“查找!” button isn't used按钮未使用
  • you don't consider the city selection你不考虑城市选择

To fix these, bind a model to the city selector, and declare separate variables for the JSON data and for the selected data:要解决这些问题,请将模型绑定到城市选择器,并为 JSON 数据和所选数据声明单独的变量:

<select name="" class="city" id="" v-model="city">

and:和:

data: {
  search: "",
  sourceJobs: [],
  selectedJobs: [],
  city: ""
}

Then put you JSON data in sourceJobs on creation:然后在创建时将 JSON 数据放入sourceJobs中:

fetch("job.json").then(function (res) {
  this.sourceJobs = res.json();
});

Side note: this architecture will not be viable for large JSON data, maybe you should consider filtering data through a call to your back-end API... but that's not the current question.旁注:这种架构不适用于大型 JSON 数据,也许您应该考虑通过调用后端 API 来过滤数据……但这不是当前的问题。

Now that your form data is bound to data.search and data.city , and that your pool of jobs is stored in data.sourceJobs , you want to have a method (no more computed ) to filter data.sourceJobs and copy the resulting subset in data.selectedJobs :现在您的表单数据绑定到data.searchdata.city ,并且您的作业池存储在data.sourceJobs ,您希望有一个方法(不再computed )来过滤data.sourceJobs并复制结果子集在data.selectedJobs

methods: {
  selectJobs: function () {
    this.selectedJobs = this.sourceJobs
      .filter((job) => {
        return job.cityName === this.city && job.positionName.match(this.search);
      })
  }
}

Finally, call this method when the "Find!"最后,在“Find!”时调用此方法。 button is clicked:按钮被点击:

<button v-on:click="selectJobs">Find!</button>

In case you change you architecture to go for an API call to do the filtering, then you just need to remove that created part and do the API call from within the selectJobs method.如果您更改架构以进行 API 调用来执行过滤,那么您只需删除created部分并从selectJobs方法中执行 API 调用。

Side, unrelated note: find/found/found (successful result of searching) vs. found/founded/founded (create, build, set a base for something - a city, a company...).边上,无关的注释:find/found/found(搜索的成功结果)vs.found/founded/founded(创建、建造、为某物建立基础——一座城市、一家公司......)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM