if (d.groundingState == 0 || d.groundingState == 3) {//layui-btn-disabled edit result = `<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a> <a class="layui-btn layui-btn-xs layui-bg-orange" lay-event="add">上架</a> <a class="layui-btn layui-btn-xs layui-btn-danger layui-btn-disabled">下架</a> <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="det">详情</a>` } else if (d.groundingState == 1) { result = `<a class="layui-btn layui-btn-xs layui-btn-disabled edit">编辑</a> <a class="layui-btn layui-btn-xs layui-btn-disabled edit">上架</a> <a class="layui-btn layui-btn-xs layui-btn-danger layui-btn-disabled">下架</a> <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="det">详情</a>` } else if (d.groundingState == 2) { result = `<a class="layui-btn layui-btn-xs layui-btn-disabled edit">编辑</a> <a class="layui-btn layui-btn-xs layui-btn-disabled edit">上架</a> <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">下架</a> <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="det">详情</a>` } else if (d.groundingState == 4 || d.groundingState == 5) { result = `<a class="layui-btn layui-btn-xs layui-btn-disabled edit">编辑</a> <a class="layui-btn layui-btn-xs layui-btn-disabled edit">上架</a> <a class="layui-btn layui-btn-xs layui-btn-disabled">下架</a> <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="det">详情</a>` }优化一下
时间: 2024-03-17 11:43:37 浏览: 88
按钮下拉菜单的大小-Bootsrap实用教程
可以将重复出现的代码进行封装,减少代码的冗余。例如,可以将按钮的class属性提取出来,写成一个变量,然后在生成按钮时直接使用该变量。同时,也可以使用模板字符串来简化代码。优化后的代码如下:
```javascript
const btnDisabledClass = "layui-btn layui-btn-xs layui-btn-disabled";
const editBtn = `<a class="layui-btn layui-btn-xs edit">编辑</a>`;
const addBtn = `<a class="layui-btn layui-btn-xs layui-bg-orange" lay-event="add">上架</a>`;
const delBtn = `<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">下架</a>`;
const detBtn = `<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="det">详情</a>`;
if (d.groundingState == 0 || d.groundingState == 3) {
result = `${editBtn} ${addBtn} <a class="${btnDisabledClass}">下架</a> ${detBtn}`;
} else if (d.groundingState == 1) {
result = `<a class="${btnDisabledClass} edit">编辑</a> <a class="${btnDisabledClass} edit">上架</a> <a class="${btnDisabledClass}">下架</a> ${detBtn}`;
} else if (d.groundingState == 2) {
result = `<a class="${btnDisabledClass} edit">编辑</a> <a class="${btnDisabledClass} edit">上架</a> ${delBtn} ${detBtn}`;
} else if (d.groundingState == 4 || d.groundingState == 5) {
result = `<a class="${btnDisabledClass} edit">编辑</a> <a class="${btnDisabledClass} edit">上架</a> <a class="${btnDisabledClass}">下架</a> ${detBtn}`;
}
```
阅读全文