vue中<div class="question-type">{{ getQuestionType( PDFTiInfo.tiCard[tiIndex] ) + tiIndex }}</div>tiIndex是题号从0开始 怎么把1 2 3 4...变成01 02 03 04 ...11
时间: 2024-03-07 11:46:45 浏览: 24
你可以使用padStart()方法来实现将数字转换成指定格式的字符串。具体实现如下:
```
<div class="question-type">{{ getQuestionType( PDFTiInfo.tiCard[tiIndex] ) + (tiIndex+1).toString().padStart(2,'0') }}</div>
```
在这个代码中,我们使用了toString()方法将数字转换成字符串,然后使用padStart()方法来将字符串填充成指定长度的格式,其中第一个参数是字符串的目标长度,第二个参数是用于填充的字符。在这里,我们将目标长度设置为2,用0来填充。这样,当tiIndex的值为0-9时,就会自动补充一个0在前面,变成01-09,当tiIndex的值为10-99时,就不会再进行填充,直接显示为10-99。
相关问题
<!-- 结算模块 --> <div class="cart-floatbar"> <div class="select-all"> <input type="checkbox" name="" id="d" class="checkall">全选 </div> <div class="operation"> <a href="javascript:;" class="remove-batch"> 删除选中的商品</a> <a href="javascript:;" class="clear-all">清理购物车</a> </div> <div class="toolbar-right"> <div class="amount-sum">已经选<em>1</em>件商品</div> <div class="price-sum">总价: <em> </em></div> <div class="btn-area">去结算</div> </div> </div>改成组件,把父组件shop-cart
可以将上述代码封装成一个名为 "CartFloatBar" 的子组件,然后在父组件 "ShopCart" 中引用该组件,代码如下所示:
CartFloatBar.vue:
```
<template>
<div class="cart-floatbar">
<div class="select-all">
<input type="checkbox" name="" id="d" class="checkall">全选
</div>
<div class="operation">
<a href="javascript:;" class="remove-batch"> 删除选中的商品</a>
<a href="javascript:;" class="clear-all">清理购物车</a>
</div>
<div class="toolbar-right">
<div class="amount-sum">已经选<em>{{ selectedCount }}</em>件商品</div>
<div class="price-sum">总价: <em>{{ totalPrice }}</em></div>
<div class="btn-area">去结算</div>
</div>
</div>
</template>
<script>
export default {
name: 'CartFloatBar',
props: {
selectedCount: {
type: Number,
default: 0
},
totalPrice: {
type: Number,
default: 0
}
}
}
</script>
```
ShopCart.vue:
```
<template>
<div class="shop-cart">
<!-- 购物车列表 -->
<!-- ... -->
<!-- 结算模块 -->
<cart-float-bar :selectedCount="1" :totalPrice="99"></cart-float-bar>
</div>
</template>
<script>
import CartFloatBar from './CartFloatBar.vue'
export default {
name: 'ShopCart',
components: {
'cart-float-bar': CartFloatBar
}
}
</script>
```
在 "ShopCart" 中引用 "CartFloatBar" 组件时,使用 kebab-case 的组件名称 "cart-float-bar",因为在 HTML 中,只能使用 kebab-case 的组件名称。同时,在引用组件时,需要将组件文件引入,并在 components 中注册组件。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图书商城-图书列表</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <link rel="stylesheet" href="/BookShopSystem_war/layui/css/layui.css"> <link rel="stylesheet" href="/BookShopSystem_war/static/css/index.css"> <script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id="app"> <div class="layui-container"> <div class="hot-sell" v-if="bookAll.length > 0"> <div class="layui-row layui-col-space20"> <table> <tr> <td v-for="(bk,index) in currentBookAll" :key="index" class="layui-col-xs2" style="margin-left: 30px"> <a :href="'detail?id='+bk.bookId" class="text"> <div><img :src="'/BookShopSystem_war/' + bk.image1"></div> <p>{{bk.bookName}}</p> <p class="price">¥{{bk.bookPrice}}</p> </a> </td> </tr> </table> </div> </div> </div> <div id="houseList"></div> </div> <!-- 引入尾部footer.html --> <iframe src="footer.html" frameborder="0" scrolling="no" width="100%" height="245px"></iframe> </div> <script src="/BookShopSystem_war/layui/layui.js"></script>这是完整的html代码,请结合你给出的分页功能整合出完整的代码
好的,以下是结合分页功能的完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图书商城-图书列表</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="/BookShopSystem_war/layui/css/layui.css">
<link rel="stylesheet" href="/BookShopSystem_war/static/css/index.css">
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<div class="layui-container">
<div class="hot-sell" v-if="bookAll.length > 0">
<div class="layui-row layui-col-space20">
<table>
<tr>
<td v-for="(bk,index) in currentBookAll" :key="index" class="layui-col-xs2" style="margin-left: 30px">
<a :href="'detail?id='+bk.bookId" class="text">
<div><img :src="'/BookShopSystem_war/' + bk.image1"></div>
<p>{{bk.bookName}}</p>
<p class="price">¥{{bk.bookPrice}}</p>
</a>
</td>
</tr>
</table>
</div>
</div>
<div class="layui-row">
<div class="layui-col-md12">
<div id="pagination"></div>
</div>
</div>
</div>
<div id="houseList"></div>
<!-- 引入尾部footer.html -->
<iframe src="footer.html" frameborder="0" scrolling="no" width="100%" height="245px"></iframe>
</div>
<script src="/BookShopSystem_war/layui/layui.js"></script>
<script>
layui.use('laypage', function(){
var laypage = layui.laypage;
var vm = new Vue({
el: '#app',
data: {
bookAll: [],
current: 1,
totalPage: 1,
currentBookAll: []
},
mounted: function() {
var _this = this;
axios.get('/BookShopSystem_war/book/all')
.then(function(response) {
_this.bookAll = response.data;
_this.totalPage = Math.ceil(_this.bookAll.length / 10);
_this.currentBookAll = _this.bookAll.slice(0, 10);
laypage.render({
elem: 'pagination',
count: _this.bookAll.length,
curr: _this.current,
limit: 10,
layout: ['prev', 'page', 'next', 'skip'],
jump: function(obj, first){
if(!first){
_this.currentBookAll = _this.bookAll.slice((obj.curr-1)*obj.limit, obj.curr*obj.limit);
}
}
});
})
.catch(function(error) {
console.log(error);
});
}
});
});
</script>
</body>
</html>
注意:这里的分页功能使用了 layui 的 laypage 组件,需要在页面中引入 layui.js 和 layui.css,并且需要在 Vue 的 mounted 钩子函数中调用 laypage.render() 方法来渲染分页组件。同时,需要注意在分页回调函数中更新当前显示的数据。