lodash-es 基本使用

中文文档:https://www.lodashjs.com/

cloneDeep方法文档:https://www.lodashjs.com/docs/lodash.cloneDeep#_clonedeepvalue

在这里插入图片描述

参考掘金文章:https://juejin.cn/post/7354940462061715497
在这里插入图片描述

在这里插入图片描述

安装:

pnpm install lodash-es

npm地址:https://www.npmjs.com/package/lodash-es

github地址:https://github.com/lodash/lodash

在antd组件库中的使用示例:

<template>
  <a-button class="editable-add-btn" style="margin-bottom: 8px" @click="handleAdd">Add</a-button>
  <a-table bordered :data-source="dataSource" :columns="columns">
    <template #bodyCell="{ column, text, record }">
      <template v-if="column.dataIndex === 'name'">
        <div class="editable-cell">
          <div v-if="editableData[record.key]" class="editable-cell-input-wrapper">
            <a-input v-model:value="editableData[record.key].name" @pressEnter="save(record.key)" />
            <check-outlined class="editable-cell-icon-check" @click="save(record.key)" />
          </div>
          <div v-else class="editable-cell-text-wrapper">
            {{ text || ' ' }}
            <edit-outlined class="editable-cell-icon" @click="edit(record.key)" />
          </div>
        </div>
      </template>
      <template v-else-if="column.dataIndex === 'operation'">
        <a-popconfirm
            v-if="dataSource.length"
            title="Sure to delete?"
            @confirm="onDelete(record.key)"
        >
          <a>Delete</a>
        </a-popconfirm>
      </template>
    </template>
  </a-table>
</template>
<script setup>
import { computed, reactive, ref } from 'vue';
import {CheckOutlined, EditOutlined} from "@ant-design/icons-vue";
import { cloneDeep } from 'lodash-es';

const columns = [
  {
    title: 'name',
    dataIndex: 'name',
    width: '30%',
  },
  {
    title: 'age',
    dataIndex: 'age',
  },
  {
    title: 'address',
    dataIndex: 'address',
  },
  {
    title: 'operation',
    dataIndex: 'operation',
  },
];
const dataSource = ref([
  {
    key: '0',
    name: 'Edward King 0',
    age: 32,
    address: 'London, Park Lane no. 0',
  },
  {
    key: '1',
    name: 'Edward King 1',
    age: 32,
    address: 'London, Park Lane no. 1',
  },
]);
const count = computed(() => dataSource.value.length + 1);
const editableData = reactive({});
const edit = key => {
  editableData[key] = cloneDeep(dataSource.value.filter(item => key === item.key)[0]);
};
const save = key => {
  Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]);
  delete editableData[key];
};
const onDelete = key => {
  dataSource.value = dataSource.value.filter(item => item.key !== key);
};
const handleAdd = () => {
  const newData = {
    key: `${count.value}`,
    name: `Edward King ${count.value}`,
    age: 32,
    address: `London, Park Lane no. ${count.value}`,
  };
  dataSource.value.push(newData);
};
</script>
<style lang="less" scoped>
.editable-cell {
  position: relative;
  .editable-cell-input-wrapper,
  .editable-cell-text-wrapper {
    padding-right: 24px;
  }

  .editable-cell-text-wrapper {
    padding: 5px 24px 5px 5px;
  }

  .editable-cell-icon,
  .editable-cell-icon-check {
    position: absolute;
    right: 0;
    width: 20px;
    cursor: pointer;
  }

  .editable-cell-icon {
    margin-top: 4px;
    display: none;
  }

  .editable-cell-icon-check {
    line-height: 28px;
  }

  .editable-cell-icon:hover,
  .editable-cell-icon-check:hover {
    color: #108ee9;
  }

  .editable-add-btn {
    margin-bottom: 8px;
  }
}
.editable-cell:hover .editable-cell-icon {
  display: inline-block;
}
</style>

相关推荐

  1. React使用 lodash-es 中的throttle方法失效

    2024-07-09 19:32:04       31 阅读
  2. lodash 使用

    2024-07-09 19:32:04       32 阅读
  3. ES6模板字符串的基本使用

    2024-07-09 19:32:04       51 阅读
  4. ES6:promise基本使用讲解

    2024-07-09 19:32:04       32 阅读
  5. 使用lodash防抖节流

    2024-07-09 19:32:04       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 19:32:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 19:32:04       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 19:32:04       58 阅读
  4. Python语言-面向对象

    2024-07-09 19:32:04       69 阅读

热门阅读

  1. kafka--发布-订阅消息系统

    2024-07-09 19:32:04       28 阅读
  2. 3160. 所有球里面不同颜色的数目

    2024-07-09 19:32:04       27 阅读
  3. go语言hassuffix的简单使用

    2024-07-09 19:32:04       31 阅读
  4. Vim常用整理快捷键

    2024-07-09 19:32:04       24 阅读
  5. Elasticsearch 分析器(Analyzer)的作用和配置

    2024-07-09 19:32:04       20 阅读
  6. html5 video去除边框

    2024-07-09 19:32:04       17 阅读
  7. 机器学习模型运用在机器人上

    2024-07-09 19:32:04       23 阅读
  8. 在网站存在漏洞的情况下强化安全防御

    2024-07-09 19:32:04       23 阅读
  9. 驱动开发系列-如何与硬件通信

    2024-07-09 19:32:04       27 阅读