简体   繁体   English

laravel 5.3和Vuejs Ajax在选择框上调用

[英]laravel 5.3 and Vuejs Ajax call on select box

i want a newly added record to be viewed on a select box once a new record has been inserted in the table, here is my laravel html code 我希望在表中插入新记录后在选择框上查看新添加的记录,这是我的Laravel html代码

 <div class="form gorup">
        <select  class="form-control" >

                <option @click="called"  v-for="task in list" >@{{task.body}}</option>

       </select> 
</div>

Here is the vue js code that fetches the data using ajax. 这是使用ajax提取数据的vue js代码。

<script>
    });
    new Vue({
        el: 'body',

       data: function(){
        return{
            list:[]
         };
        },
        methods: {

           called: function(){

               $.getJSON('api/tasks', function (tasks) {
                   this.list = tasks;
                   console.error();
               }.bind(this));

           },
        },
    });

</script>

here is the code from laravel route function that produces the task items 这是产生任务项的laravel route函数的代码

Route::get('api/tasks', function(){

return App\Task::latest()->get();

}); });

So my idea is that if a user clicks on the select box it executes the ajax code, fetches the records and displays the data in the <option></option> tags 所以我的想法是,如果用户单击选择框,它将执行ajax代码,获取记录并在<option></option>标记中显示数据

is there a better way to make the code work better because currently i have to click the the select box twice for it to pull new data. 有没有更好的方法来使代码更好地工作,因为当前我必须单击两次选择框才能提取新数据。 ?

why not after you mounted(ready) or insert a new data(new list) and just pull the new records there and insert it to your this.list = tasks; 为什么不挂载(准备好)或插入新数据(新列表),然后将新记录拉到那里并将其插入到您的this.list =任务中呢?

new Vue({
data : {
list  = []
},

ready : function(){
  this.initialize();
},
methods : {

initialize : function(){
   let self = this;
   $.get('url',function(response)
      {
          self.list = response.task
      }

},
createNewTask: function(){
       let self = this;
       $.post('url',{data},function(response)
          {
              self.list = response.task
          }

    }

}
})

you should not put your ajax pull from your select box as it will consume a time to pull new data from server and put it to your list array 您不应该将ajax拉出您的选择框,因为这将花费一些时间从服务器拉出新数据并将其放入列表数组

.

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

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