javascript - 加载 Vue.js 表需要很长时间(加载两次)
问题描述
这是一个网络应用程序,使用 Django 作为后端,Vue.js 作为前端。
我已经使用 Vue.js 构建了一个页面,包括一个表格。 我确实需要 jquery 数据表来进行分页,因为该表有很多数据。
但是加载需要很长时间,并且表格似乎渲染了两次(第一次显示普通表格,然后显示 Vue.js 表格)。
如何通过让表格显示最终结果而不是加载两次来缩短表格加载时间?
<div id="app">
<br>
<p></p>
<form ref="form" id="myform" method="post" action="/tag_course/">
<table id="myTable">
<thead>
<tr>
<th>Author/Editor</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in filteredRows" :key="`isbn-${index}`">
<td v-html="highlightMatches(row.author)">{{ row.author }}</td>
<td><button type="button" class="button2" v-on:click="check( $event, row.title)">Tag & Submit</button></td>
</tr>
</tbody>
</table>
</form>
</div>
<script>
Vue.prototype.$http = axios;
$(document).ready(function() {
$('#myTable').DataTable({info: false, bLengthChange: false});
$(".dataTables_filter input").attr("placeholder", "By title, iSBN, VBID, publisher, Author");
} );
var query_results_book = {{ query_results_book_json|safe}};
var book_rows = [{author: query_results_book[0].fields.author}];
for (var i=1; i < query_results_book.length; i += 1){
author: query_results_book[i].fields.author});
}
const app = new Vue({
el: '#app',
data:() => ({
filter: '',
rows: book_rows
}),
methods: {
highlightMatches(text)
{
if (typeof text === 'string' || text instanceof String){
text = (text?text:'None');
const matchExists = text.toLowerCase().includes(this.filter.toLowerCase());
if (!matchExists) return text;
const re = new RegExp(this.filter, 'ig');
return text.replace(re, matchedText => `<strong>${matchedText}</strong>`);
}
},
submit : function(){
this.$refs.form.submit()
},
computed: {
filteredRows() {
return this.rows.filter(row => {
const author = String(row.author).toString().toLowerCase();
const title = String(row.title).toLowerCase();
const searchTerm = this.filter.toLowerCase();
return title.includes(searchTerm) ||
author.includes(searchTerm);
});
}
},
});
解决方案
推荐阅读
- svg - 我怎样才能顺利地扩展一个半圆?
- android - OnCnangeMethod My HomeFragment 中返回的实时数据
- reactjs - 如何使用 Material-UI 将表单数据发布到 API
- f# - F# 将字符串拆分为元组的最简单方法
- database - 为什么我不能在我的开关盒中使用我的投影场?
- reactjs - 如何正确将 react localStorage 转换为 react-native asyncStorage?
- python - 结合使用 map 和 lower 函数
- google-sheets - Google 表格上的多个查询过滤器
- java - Android Networking API 添加 Bearer 令牌
- javascript - 如何在反应中从css文件继承