我正在尝试实现一项功能,使用户能够使用vue js向表中添加新的列和行。我能够将标签推送到表,但是我使用的添加方法不正确,特别是因为将td添加到对象而仅添加到视图本身。
我正在从laravel的帮助程序文件中提取默认的html表内容
(object)array(
'label' => 'Table',
'field_name' => '',
'type' => 'table',
'properties' => (object)array(
'headers' => [
'Header 1',
],
'rows' => [
(object) array(
'value' => 'Row 1 content',
),
],
)
)
HTML(vue js)
...
<div v-if="field.type == 'table'">
<pre>{{field.properties.headers}}</pre><br>
<pre>{{field.properties.rows}}</pre>
<table :class="`table ` + field.properties.style" :id="`table`+index">
<thead>
<tr>
<th v-for="(header, index) in field.properties.headers" v-if="header.length > 0">
{{header}}
<a href="#" title="Remove Column" @click.prevent="removeTableColumn(field.properties, index)"><i class="fa fa-trash"></i></a>
</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in field.properties.rows" :key="index" id="default-row">
<td>{{row.value}}</td>
</tr>
</tbody>
</table>
<table>
<tr>
<td>
<a href="#" class="btn btn-sm btn-add-page" @click.prevent="addTableColumn(field.properties, index)">Add Column</a>
<a href="#" class="btn btn-sm btn-add-page" @click.prevent="addTableRow(field.properties, index)">Add Row</a>
</td>
<td></td>
<td></td>
</tr>
</table>
</div>
...
脚本(vue js)
...
methods: {
addTableColumn(properties, index) {
properties.headers.push('Column Heading');
$(`#table${index} #default-row`).append(`<td>Row Content</td>`);
},
removeTableColumn(properties, index) {
properties.headers.splice(index, 1);
properties.rows.splice(index, 1);
},
addTableRow(properties, index) {
// let columnCount = properties.headers.length;
// for(let i = 0; i < columnCount; i++) {
// properties.rows.push({'value': 'Row content'});
// }
// console.log(columnCount);
// console.log(index);
// rows.push({ 'value': 'Row column' });
$(`#table${index}`).append(`<tr>${$('#default-row').html()}</tr>`);
}
},...