Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

javascript - 在Vue中的复选框中使用V-for(Using V-for in checkboxes in vue)

I am able to list values and save them to the database without any problem using axios>>vue JS>>spring boot.

(我可以使用axios >> vue JS >> spring boot列出值并将其保存到数据库中。)

My problem I want to trigger my save method only when checkbox is selected or true in Vue JS.

(我的问题是我只想在Vue JS中选中复选框或为true时才触发保存方法。)

In my case am using v-on:change in my checkbox but the method get invoked every time i click the checkbox.

(就我而言,我在复选框中使用v-on:change,但是每次我单击复选框时都会调用该方法。)

is there a way I can invoke my save method only when the checkbox is selected(true).

(有没有一种方法可以仅在选中复选框(true)时调用我的save方法。)

my data from the database get displayed as shown

(我的数据库数据显示如下)

在此处输入图片说明

here is my code:

(这是我的代码:)

      <thead>
          <tr>  <th>ID</th>
                <th>Subject Name</th>
                <th>Action</th>
         </tr>
    </thead>
          <tbody>
          <tr  v-for="item in subjectData">
                 <td>{{item.id}}</td> <td>{{item.subjectName}}</td>
        <td><input type="checkbox" v-on:change.prevent="saveMarks(item)" /></td>
                         </tr>  
    </tbody>
           </table>
   </div>
   <script>
var subjectStudentsVM=new Vue({
el:"#subjectStudentsSelection",
data:function(){    
    return {
        id: '',
        studid:'',
        subjectData:Array(),
    }
},

created:function (){
this.getAllSubjects();
},
methods:{
        saveMarks: function(item) {
        var self=this;
        axios({
          method:"POST",
          url:"/Student/"+this.studid+"/"+item.id, 
         // url:"/Student/2/2", 
              headers: {
                  'content-type': 'application/json',
              },
              data:{
                    }
            })
    },
    getAllSubjects:function(){
        var self=this;
          axios.get("/Subject/list/").then(function (response) {
                this.subjectData = response.data;
          }.bind(this)).catch(function (error) {
            console.log('Error while fetching subject data: ' + error)
          })
    },
}
})

  ask by user3736334 translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to check inside your function saveMarks what the state of the checkbox is and only then save.

(你需要检查你函数内部saveMarks什么复选框的状态,然后保存。)
Like so:

(像这样:)

 saveMarks: function(item,event) {
            var self=this;
            if (event.target.checked === true){
            axios({
              method:"POST",
              url:"/Student/"+this.studid+"/"+item.id, 
             // url:"/Student/2/2", 
                  headers: {
                      'content-type': 'application/json',
                  },
                  data:{

                        }

                })

                console.log("record  saved");
               }else{

                    console.log("record not saved");
                    //return

                }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...