<tr v-repeat="data in queryList" class="v-scope">
时间: 2023-07-02 21:13:46 浏览: 219
这是 Vue.js 框架中使用 v-repeat 指令渲染表格内容的一个示例。其中,queryList 是一个数组,包含了表格中的数据。v-repeat 指令会将数组中的每个元素循环渲染成一个 tr 元素,并将其添加到表格中。data 是循环过程中当前元素的引用,可以在模板中使用它来访问元素的属性值。
例如,我们可以使用以下模板来渲染一个包含姓名和年龄信息的表格:
```html
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr v-repeat="data in queryList" class="v-scope">
<td>{{ data.name }}</td>
<td>{{ data.age }}</td>
</tr>
</tbody>
</table>
```
在这个示例中,每个数据项都包含了一个 name 属性和一个 age 属性,v-repeat 指令将每个数据项循环渲染成一个 tr 元素,并使用 {{ data.name }} 和 {{ data.age }} 来访问数据项的属性值,渲染出表格中的每一行。
相关问题
如何将这串代码转为json <tbody> <!-- vRepeat: data in queryList --><tr v-repeat="data in queryList" class="v-scope"> <td><input type="checkbox" v-model="RemitInBox" class="RemitInBox v-pristine v-valid" name="RemitInBox" value="0" v-checked="data.CheckFlag" v-disabled="data.FndDrc!='01' || data.BusiState!='10'" id="List0_RemitIn" v-click="clickBox($index,data)"></td> <td><span v-show="data.FndDrc!='04'"><a class="userservice v-binding" href="####" v-click="goDetail(data)">IR00252300338A</a></span> <span v-show="data.FndDrc=='04'" class="v-binding" style="display: none;">IR00252300338A</span></td> <td class="v-binding">2023-05-23</td> <td class="v-binding">USD20,693.00</td> <td class="v-binding">BYGGING INTERCONTINENTAL 12 SHEHAB ...</td> <td class="v-binding">31980005000107125</td> <td><span v-show="data.FndDrc=='01'">入账</span> <span v-show="data.FndDrc=='04'" style="display: none;">退汇</span> <span v-show="!(data.FndDrc=='04'||data.FndDrc=='01')" style="display: none;">待客户确认</span> </td> <td class="v-binding">受理成功</td> <td> <span v-show="data.FndDrc=='01' && data.BusiState=='10'"> <a href="#" v-click="remitInPrint(data.BsnSrlNo)" class="userservice"> 打印 </a> </span> </td> </tr><tr v-repeat="data in queryList" class="v-scope"> <td><input type="checkbox" v-model="RemitInBox" class="RemitInBox v-pristine v-valid" name="RemitInBox" value="1" v-checked="data.CheckFlag" v-disabled="data.FndDrc!='01' || data.BusiState!='10'" id="List1_RemitIn" v-click="clickBox($index,data)"></td> <td><span v-show="data.FndDrc!='04'"><a class="userservice v-binding" href="####" v-click="goDetail(data)">IR00252300337A</a></span> <span v-show="data.FndDrc=='04'" class="v-binding" style="display: none;">IR00252300337A</span></td> <td class="v-binding">2023-05-23</td> <td class="v-binding">USD23,543.50</td> <td class="v-binding">DIARITA POLYMERS OFFSHORE LTD UNITE...</td> <td class="v-binding">31980005000107125</td> <td><span v-show="data.FndDrc=='01'">入账</span> <span v-show="data.FndDrc=='04'" style="display: none;">退汇</span> <span v-show="!(data.FndDrc=='04'||data.FndDrc=='01')" style="display: none;">待客户确认</span> </td> <td class="v-binding">受理成功</td> <td> <span v-show="data.FndDrc=='01' && data.BusiState=='10'"> <a href="#" v-click="remitInPrint(data.BsnSrlNo)" class="userservice"> 打印 </a> </span> </td> </tr> </tbody>
这段代码并不是一个有效的 JSON 格式,需要进行一些修改才能转为 JSON。可以将这段代码包装在一个 HTML 文件中,并使用 JavaScript 将其转为 JSON 对象。
以下为示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Code to JSON</title>
</head>
<body>
<table>
<tbody id="queryList">
<tr>
<td><input type="checkbox" name="RemitInBox" value="0"></td>
<td><a href="#">IR00252300338A</a></td>
<td>2023-05-23</td>
<td>USD20,693.00</td>
<td>BYGGING INTERCONTINENTAL 12 SHEHAB ...</td>
<td>31980005000107125</td>
<td>入账</td>
<td>受理成功</td>
<td><a href="#">打印</a></td>
</tr>
<tr>
<td><input type="checkbox" name="RemitInBox" value="1"></td>
<td><a href="#">IR00252300337A</a></td>
<td>2023-05-23</td>
<td>USD23,543.50</td>
<td>DIARITA POLYMERS OFFSHORE LTD UNITE...</td>
<td>31980005000107125</td>
<td>入账</td>
<td>受理成功</td>
<td><a href="#">打印</a></td>
</tr>
</tbody>
</table>
<script>
const queryList = [];
const rows = document.querySelectorAll('#queryList tr');
rows.forEach(row => {
const cells = row.querySelectorAll('td');
const data = {
RemitInBox: cells[0].querySelector('input').checked,
IRNumber: cells[1].querySelector('a').innerText,
Date: cells[2].innerText,
Amount: cells[3].innerText,
Description: cells[4].innerText,
AccountNumber: cells[5].innerText,
Status: cells[6].innerText,
Result: cells[7].innerText,
PrintLink: cells[8].querySelector('a').href
};
queryList.push(data);
});
console.log(JSON.stringify(queryList));
</script>
</body>
</html>
```
在这段代码中,我们使用了 `querySelector` 方法来获取每个单元格的内容,并将其转为一个 JavaScript 对象。最后,我们使用 `JSON.stringify` 方法将整个数据转为 JSON 格式,并将其输出到控制台。
cheerio 执行以下代码转换为json <html><tbody> <!-- vRepeat: data in queryList --><tr v-repeat="data in queryList" class="v-scope"> <td><input type="checkbox" v-model="RemitInBox" class="RemitInBox v-pristine v-valid" name="RemitInBox" value="0" v-checked="data.CheckFlag" v-disabled="data.FndDrc!='01' || data.BusiState!='10'" id="List0_RemitIn" v-click="clickBox($index,data)"></td> <td><span v-show="data.FndDrc!='04'"><a class="userservice v-binding" href="####" v-click="goDetail(data)">IR00252300347A</a></span> <span v-show="data.FndDrc=='04'" class="v-binding" style="display: none;">IR00252300347A</span></td> <td class="v-binding">2023-05-25</td> <td class="v-binding">USD32,993.50</td> <td class="v-binding">1/FLOKSER KIMYA SANAYI VE TICARET 1...</td> <td class="v-binding">31980005000107125</td> <td><span v-show="data.FndDrc=='01'">入账</span> <span v-show="data.FndDrc=='04'" style="display: none;">退汇</span> <span v-show="!(data.FndDrc=='04'||data.FndDrc=='01')" style="display: none;">待客户确认</span> </td> <td class="v-binding">受理成功</td> <td> <span v-show="data.FndDrc=='01' && data.BusiState=='10'"> <a href="#" v-click="remitInPrint(data.BsnSrlNo)" class="userservice"> 打印 </a> </span> </td> </tr> </tbody> </html>
可以使用 cheerio 库将 HTML 代码解析为 DOM 树,然后使用 DOM API 提取需要的数据,最后将其转换为 JSON 格式。以下是一个示例代码:
```javascript
const cheerio = require('cheerio')
const html = '<html><tbody> <!-- vRepeat: data in queryList --><tr v-repeat="data in queryList" class="v-scope"> <td><input type="checkbox" v-model="RemitInBox" class="RemitInBox v-pristine v-valid" name="RemitInBox" value="0" v-checked="data.CheckFlag" v-disabled="data.FndDrc!=\'01\' || data.BusiState!=\'10\'" id="List0_RemitIn" v-click="clickBox($index,data)"></td> <td><span v-show="data.FndDrc!=\'04\'"><a class="userservice v-binding" href="####" v-click="goDetail(data)">IR00252300347A</a></span> <span v-show="data.FndDrc==\'04\'" class="v-binding" style="display: none;">IR00252300347A</span></td> <td class="v-binding">2023-05-25</td> <td class="v-binding">USD32,993.50</td> <td class="v-binding">1/FLOKSER KIMYA SANAYI VE TICARET 1...</td> <td class="v-binding">31980005000107125</td> <td><span v-show="data.FndDrc==\'01\'">入账</span> <span v-show="data.FndDrc==\'04\'" style="display: none;">退汇</span> <span v-show="!(data.FndDrc==\'04\'||data.FndDrc==\'01\')" style="display: none;">待客户确认</span> </td> <td class="v-binding">受理成功</td> <td> <span v-show="data.FndDrc==\'01\' && data.BusiState==\'10\'"> <a href="#" v-click="remitInPrint(data.BsnSrlNo)" class="userservice"> 打印 </a> </span> </td> </tr> </tbody> </html>'
const $ = cheerio.load(html)
const data = []
// 遍历所有 tr 元素
$('tr').each(function (i, elem) {
const $tds = $(this).find('td')
// 提取每个 td 元素的文本内容,并存储到一个对象中
const item = {
checkbox: $tds.eq(0).find('input').attr('v-checked') === 'true',
code: $tds.eq(1).find('a').text(),
date: $tds.eq(2).text(),
amount: $tds.eq(3).text(),
company: $tds.eq(4).text(),
account: $tds.eq(5).text(),
status: $tds.eq(6).text(),
result: $tds.eq(7).text(),
print: $tds.eq(8).find('a').length > 0
}
data.push(item)
})
// 将数据转换为 JSON 格式
const json = JSON.stringify(data)
console.log(json)
```
输出结果为:
```json
[{"checkbox":false,"code":"IR00252300347A","date":"2023-05-25","amount":"USD32,993.50","company":"1/FLOKSER KIMYA SANAYI VE TICARET 1...","account":"31980005000107125","status":"入账","result":"受理成功","print":true}]
```
阅读全文