仿照以下代码用SSM框架和vue完成角色的权限配置div id=app table class=table table-striped thead tr th scope=col角色名称th th scope=col权限名称th tr thead tbody tr
<p>由于缺少相关后端代码,无法提供完整的代码实现。以下是参考实现:</p>
<ol>
<li>后端代码实现:</li>
</ol>
<ul>
<li>角色表(role)中需要添加一个权限ID(pid)字段,用于表示该角色拥有的权限。</li>
<li>提供一个接口用于查询所有角色(findAllRole)。</li>
<li>提供一个接口用于保存角色及其权限(saveRoleAndPre)。</li>
</ul>
<ol start="2">
<li>前端代码实现:</li>
</ol>
<ul>
<li>使用Vue框架,将角色及其权限以表格的形式展示出来。</li>
<li>点击“配置权限”按钮,弹出模态框,展示当前角色已有的权限及所有可选权限。</li>
<li>选择需要的权限后,点击“提交”按钮,调用后端保存角色及其权限的接口。</li>
</ul>
<p>以下是前端代码实现:</p>
<p>HTML代码:</p>
<div id="app">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">角色名称</th>
<th scope="col">权限名称</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in list" :key="index">
<td>{{ item.rname }}</td>
<td>{{ item.pname }}</td>
<td>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"
@click="showModal(item)">配置权限</button>
</td>
</tr>
</tbody>
</table>
<pre><code><div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">配置权限</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="roleName" class="col-form-label">角色名称:</label>
<input type="text" class="form-control" id="roleName" v-model="selectedRole.rname"
disabled>
</div>
<div class="form-group">
<label for="preSelect" class="col-form-label">权限:</label>
<select class="form-control" id="preSelect" v-model="selectedRole.pid">
<option v-for="(pre, index) in allPres" :key="index" :value="pre.id">{{ pre.pname }}
</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary" @click="saveRoleAndPre()">保存</button>
</div>
</div>
</div>
</div>
</code></pre>
</div>
<p>JavaScript代码:</p>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
new Vue({
el: "#app",
data: {
list: [], // 角色及其权限列表
allPres: [], // 所有权限列表
selectedRole: {}, // 当前选择的角色
},
mounted() {
this.findAllRoles();
this.findAllPres();
},
methods: {
// 查询所有角色及其权限
findAllRoles() {
axios.get("/role/findAllRoles").then(response => {
this.list = response.data;
});
},
// 查询所有权限
findAllPres() {
axios.get("/pre/findAllPres").then(response => {
this.allPres = response.data;
});
},
// 显示模态框
showModal(role) {
this.selectedRole = role;
},
// 保存角色及其权限
saveRoleAndPre() {
axios.post("/role/saveRoleAndPre", this.selectedRole).then(response => {
// 保存成功后,关闭模态框并刷新角色及其权限列表
$("#exampleModal").modal("hide");
this.findAllRoles();
});
},
},
});
</script
原文地址: https://www.cveoy.top/t/topic/hkFl 著作权归作者所有。请勿转载和采集!