【发布时间】:2019-07-11 16:54:06
【问题描述】:
简单情况:使用 Laravel 控制器从 DB 中获取所有行并使用 Vue 显示。
Laravel 从模型中生成正确排序(按名称)的结果,但是当通过 Vue 获取并循环显示在 HTML 表格中时,它们会显示为它们在数据库中存储的顺序。
控制器:
public function readCountryAll()
{
$data = World::Countries()->sortBy('name');
//return response()->json($data);
return $data;
}
Vue:
<thead>
<tr>
<th>Code</th>
<th>Name</th>
<th>Currency</th>
</tr>
</thead>
<tr v-for="country in countryList" :key="country.code">
<td>{{ country.code }}</td>
<td>{{ country.full_name }}</td>
<td>{{ country.currency_code }}</td>
</tr>
</table>
<script>
export default {
mounted: function() {
this.read();
},
data() {
return {
countryList: [],
}
},
methods: {
read() {
window.axios.get('/readCountry')
.then(response => this.countryList = response.data)
},
},
components: {
},
}
</script>
【问题讨论】:
-
您能否通过手动访问网址来检查您的回复?所以通过浏览器并检查它的顺序
-
你确定vue中的url应该是readCountry而不是readCountryAll吗?
-
为什么要评论 //return response()->json($data)// 部分?
-
如果你 dd($data) 你看它们的顺序是否正确?