我有一个显示我所有数据的简单表:
主文件.php
<table class="table table-bordered table-hover" id="all-jobs">
<thead>
<tr>
<th>{{ __('Job Name') }}</th>
<th>{{ __('Job Description') }}</th>
<th>{{ __('Job Status') }}</th>
<th>{{ __('Job Applications') }}</th>
<th>{{ __('Manage') }}</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td class="non_searchable"></td>
<td class="non_searchable"></td>
</tr>
</thead>
</table>
<div id="app">
<div id="editJob" class="modal fade in" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<edit-job id=""></edit-job>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
现在,我有一个编辑按钮,我试图打开该特定行的编辑模式:
<a href='' data-id='{$job->id}' class='btn btn-xs btn-danger' data-toggle='modal' data-target='#editJob'><i class='fa fa-close'></i></a>";
Run Code Online (Sandbox Code Playgroud)
href是我的一个数据表中的位置,我试图将它传递给我的.vue文件,所以我可以将它用于我的get和post请求:
myfile.vue
<template>
<div>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Job</h4>
</div>
<div class="modal-body">
<form method="post" @submit.prevent="signIn" @keydown="errors.clear($event.target.name)">
<!-- Removed code, it's just inputs !-->
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info btn-fill btn-wd" v-on:click="addJob">Save</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</template>
<script>
export default
{
props: ['id'],
data: function ()
{
return {
countries: [],
name: '',
summary: '',
salarytype: '',
salaryfrom: '',
salaryto: '',
location: '',
contactemail: '',
contactphone: '',
errors: new Errors()
}
},
methods:
{
addJob: function()
{
axios.post('/jobs/edit', this.$data)
.then(response => {
if(response.data.status === true){
$('#editJob').modal('hide');
getJobTable();
}
else{
formError = response.data.message;
}
})
.catch(error => this.errors.record(error.data))
}
},
mounted: function()
{
console.log($(this).data('id'));
axios.get('/jobs/my-job/')
.then(response => {
this.name = response.data.name
this.summary = response.data.summary
this.salarytype = response.data.salary_type
this.salaryfrom = response.data.salary_from
this.salaryto = response.data.salary_to
this.location = response.data.location
this.contactemail = response.data.contact
this.contactphone = response.data.phone
})
axios.get('/countries')
.then(response => {
this.countries = response.data;
})
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
我怎样才能将我的href id传递给我用于我的请求?谢谢
我的结构:
创建-jobs.blade.php
编辑Job.vue
app.js
该表只填充数据,并添加下拉列表,如下所示:
<ul class='icons-list'>
<li class='dropdown'>
<a href='#' class='dropdown-toggle' data-toggle='dropdown' aria-expanded='false'>
<i class='icon-menu9'></i>
</a>
<ul class='dropdown-menu dropdown-menu-right'>
<li>
<a data-id='{$job->id}' onclick='getID({$job->id})' data-toggle='modal' data-target='#editJob'>
<i class='icon-file-pdf'></i> Edit Job
</a>
</li>
<li>
<a href='javascript:void();' data-id='{$job->id}' onclick='deleteJob({$job->id})'>
<i class='icon-cross'></i> Delete Job
</a>
</li>
</ul>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
您没有提供有关应用程序结构的大量信息,但看起来您正在使用至少一个文件组件来显示模式中的数据,该模式完全通过 Bootstrap 显示。它还看起来包含您想要传递给 Vue 的值的表id位于 Vue 本身之外。
在这种情况下,将所需数据传递给单个文件组件的方法是在变量中捕获 Vue,然后在id单击表中的链接时设置 。
让我们假设你的main.jsorapp.js看起来像这样:
import Vue from 'vue'
import EditJob from './EditJob.vue'
Vue.component('edit-job', EditJob)
const app = new Vue({
el: '#app',
data:{
id: null
}
})
// Add a click handler for the links with the `data-id` property.
// This is using jQuery (because you are using Bootstrap) but you
// can do this any way you want.
$("[data-id]").on("click", function(){
// Set the Vue's data to the data-id property in the link.
app.id = this.dataset.id
})
Run Code Online (Sandbox Code Playgroud)
请注意代码如何捕获new Vue(...)变量 in的结果app。然后,我将 data 属性添加id到 Vue 中,并为所有链接添加了一个单击处理程序,该处理程序设置app.id为this.dataset.id每当单击链接时。这样,每次点击链接时,Vue 中的 data 属性都会被设置为被id点击链接的 。
然后,您需要做的就是将 id 属性绑定到您的组件。
<edit-job :id="id"></edit-job>
Run Code Online (Sandbox Code Playgroud)
并且您的EditJob组件将始终获得更新id。
这是一个工作示例。
编辑
在添加到示例中的代码中,您在Created-jobs.blade.php. 在这种情况下,由于正常的 javascript 作用域规则,您编写的函数getID无法访问您在 webpack 包中定义的变量。app要使app您的 jQuery 代码可访问,请将其添加到window.
window.app = new Vue({
el: '#app',
data:{
id: null
}
});
Run Code Online (Sandbox Code Playgroud)
其次,尽管您定义了该getID函数,但没有任何东西调用它。单击链接时需要调用它。将其添加到您的 jQuery 代码中的某个位置Created-jobs.blade.php(最好是在文档ready函数中)。
$("[data-id]").on("click", getID)
Run Code Online (Sandbox Code Playgroud)