Why a-switch button doesn’t work in vue project

I try to show the table contains switch button.
Now, it doesn’t change it’s state.

Here is my List.vue file and DataTable.vue file

List.vue

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script setup lang="tsx">
import { Main } from '../styled';
import { defineAsyncComponent, h } from 'vue';
import DataTables from '@/components/table/DataTable.vue';
import { computed, reactive, ref, defineComponent, onMounted } from 'vue';
import { Button, Badge } from 'ant-design-vue/es'
import useAxios from '@/@core/axios/useAuthAxios';
...
const dataTableColumn = [
...
{
title: 'Status',
dataIndex: 'status',
key: 'status',
},
...
];
const tableDataSource = reactive([]);
...
const agent_list = async () => {
const params = {
q: ""
}
const axios = new useAxios();
const response = await axios.getAgentTree({ params });
const data = reactive(response.data);
tableDataSource.length = 0;
data.agents.forEach(agent => {
const reactiveAgent = reactive({
...
status: agent.data.status == 1,
...
});
tableDataSource.push({
...reactiveAgent,
status: <a-switch style="margin: 0 10px" size="small" checked={reactiveAgent.status} onChange={(checked) => { reactiveAgent.status = checked; console.log(checked, ": ", reactiveAgent.status) }} />
});
});
console.log('tableDataSource : ', tableDataSource);
return tableDataSource;
}
onMounted(() => {
agent_list();
});
</script>
<template>
...
<DataTables :filterOption="true" :filterOnchange="true" :tableData="tableDataSource"
:columns="dataTableColumn" :rowSelection="true" />
...
</code>
<code><script setup lang="tsx"> import { Main } from '../styled'; import { defineAsyncComponent, h } from 'vue'; import DataTables from '@/components/table/DataTable.vue'; import { computed, reactive, ref, defineComponent, onMounted } from 'vue'; import { Button, Badge } from 'ant-design-vue/es' import useAxios from '@/@core/axios/useAuthAxios'; ... const dataTableColumn = [ ... { title: 'Status', dataIndex: 'status', key: 'status', }, ... ]; const tableDataSource = reactive([]); ... const agent_list = async () => { const params = { q: "" } const axios = new useAxios(); const response = await axios.getAgentTree({ params }); const data = reactive(response.data); tableDataSource.length = 0; data.agents.forEach(agent => { const reactiveAgent = reactive({ ... status: agent.data.status == 1, ... }); tableDataSource.push({ ...reactiveAgent, status: <a-switch style="margin: 0 10px" size="small" checked={reactiveAgent.status} onChange={(checked) => { reactiveAgent.status = checked; console.log(checked, ": ", reactiveAgent.status) }} /> }); }); console.log('tableDataSource : ', tableDataSource); return tableDataSource; } onMounted(() => { agent_list(); }); </script> <template> ... <DataTables :filterOption="true" :filterOnchange="true" :tableData="tableDataSource" :columns="dataTableColumn" :rowSelection="true" /> ... </code>
<script setup lang="tsx">
import { Main } from '../styled';
import { defineAsyncComponent, h } from 'vue';
import DataTables from '@/components/table/DataTable.vue';
import { computed, reactive, ref, defineComponent, onMounted } from 'vue';
import { Button, Badge } from 'ant-design-vue/es'
import useAxios from '@/@core/axios/useAuthAxios';

...

const dataTableColumn = [
  ...
  {
    title: 'Status',
    dataIndex: 'status',
    key: 'status',
  },
  ...
];

const tableDataSource = reactive([]);
...


const agent_list = async () => {
  const params = {
    q: ""
  }
  const axios = new useAxios();
  const response = await axios.getAgentTree({ params });
  const data = reactive(response.data);
  tableDataSource.length = 0;
  data.agents.forEach(agent => {
    const reactiveAgent = reactive({
      ...
      status: agent.data.status == 1,
      ...
    });
    tableDataSource.push({
      ...reactiveAgent,
      status: <a-switch style="margin: 0 10px" size="small" checked={reactiveAgent.status} onChange={(checked) => { reactiveAgent.status = checked; console.log(checked, ": ", reactiveAgent.status) }} />
    });
  });

  console.log('tableDataSource : ', tableDataSource);
  return tableDataSource;
}

onMounted(() => {
  agent_list();
});

</script>

<template>
  ...
<DataTables :filterOption="true" :filterOnchange="true" :tableData="tableDataSource"
                  :columns="dataTableColumn" :rowSelection="true" />
...

DataTable.vue

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script setup lang="ts">
import { computed, ref, unref } from 'vue';
import { DataTableStyleWrap } from './Style';
import { TableWrapper } from '../../views/styled';
import { useStore } from 'vuex';
const props = defineProps({
filterOption: Boolean,
filterOnchange: Boolean,
rowSelection: Boolean,
tableData: Array,
columns: Array,
});
const { dispatch } = useStore();
const handleIdSearch = (e: any) => {
const id = e.currentTarget.value;
dispatch('dataLiveFilter', { value: id, key: 'id' });
};
const handleStatusSearch = (value: any) => {
dispatch('dataLiveFilter', { value, key: 'status' });
};
const handleDataUser = (e: any) => {
const { value } = e.currentTarget;
dispatch('dataLiveFilter', { value, key: 'name' });
};
const handleSearch = () => {
const idInput = document.querySelector('.ninjadash-data-id') as HTMLInputElement;
const statusSelect = document.querySelector('.ninjadash-data-status .ant-select-selection-item') as HTMLDivElement;
if (idInput && statusSelect) {
const id = idInput.value;
const status = statusSelect.title;
dispatch('filterWithSubmit', { id, status });
}
};
const selectedRowKeys = ref([]); // Check here to configure the default column
const onSelectChange = (changableRowKeys: any) => {
console.log('selectedRowKeys changed: ', changableRowKeys);
selectedRowKeys.value = changableRowKeys;
};
const rowSelections = computed(() => {
return {
selectedRowKeys: unref(selectedRowKeys),
onChange: onSelectChange,
hideDefaultSelections: true,
};
});
</script>
<template>
<DataTableStyleWrap>
<div v-if="filterOption" class="ninjadash-datatable-filter">
<div class="ninjadash-datatable-filter__right">
<a-input @change="handleDataUser" size="default" placeholder="Search">
<template #prefix>
<unicon name="search"></unicon>
</template>
</a-input>
</div>
</div>
<div class="ninjadasj-datatable">
<TableWrapper class="table-data-view table-responsive">
<a-table
:pagination="{ pageSize: 10, showSizeChanger: true }"
:data-source="tableData"
:columns="columns"
/>
</TableWrapper>
</div>
</DataTableStyleWrap>
</template>
</code>
<code><script setup lang="ts"> import { computed, ref, unref } from 'vue'; import { DataTableStyleWrap } from './Style'; import { TableWrapper } from '../../views/styled'; import { useStore } from 'vuex'; const props = defineProps({ filterOption: Boolean, filterOnchange: Boolean, rowSelection: Boolean, tableData: Array, columns: Array, }); const { dispatch } = useStore(); const handleIdSearch = (e: any) => { const id = e.currentTarget.value; dispatch('dataLiveFilter', { value: id, key: 'id' }); }; const handleStatusSearch = (value: any) => { dispatch('dataLiveFilter', { value, key: 'status' }); }; const handleDataUser = (e: any) => { const { value } = e.currentTarget; dispatch('dataLiveFilter', { value, key: 'name' }); }; const handleSearch = () => { const idInput = document.querySelector('.ninjadash-data-id') as HTMLInputElement; const statusSelect = document.querySelector('.ninjadash-data-status .ant-select-selection-item') as HTMLDivElement; if (idInput && statusSelect) { const id = idInput.value; const status = statusSelect.title; dispatch('filterWithSubmit', { id, status }); } }; const selectedRowKeys = ref([]); // Check here to configure the default column const onSelectChange = (changableRowKeys: any) => { console.log('selectedRowKeys changed: ', changableRowKeys); selectedRowKeys.value = changableRowKeys; }; const rowSelections = computed(() => { return { selectedRowKeys: unref(selectedRowKeys), onChange: onSelectChange, hideDefaultSelections: true, }; }); </script> <template> <DataTableStyleWrap> <div v-if="filterOption" class="ninjadash-datatable-filter"> <div class="ninjadash-datatable-filter__right"> <a-input @change="handleDataUser" size="default" placeholder="Search"> <template #prefix> <unicon name="search"></unicon> </template> </a-input> </div> </div> <div class="ninjadasj-datatable"> <TableWrapper class="table-data-view table-responsive"> <a-table :pagination="{ pageSize: 10, showSizeChanger: true }" :data-source="tableData" :columns="columns" /> </TableWrapper> </div> </DataTableStyleWrap> </template> </code>
<script setup lang="ts">
import { computed, ref, unref } from 'vue';
import { DataTableStyleWrap } from './Style';
import { TableWrapper } from '../../views/styled';
import { useStore } from 'vuex';

const props = defineProps({
  filterOption: Boolean,
  filterOnchange: Boolean,
  rowSelection: Boolean,
  tableData: Array,
  columns: Array,
});

const { dispatch } = useStore();

const handleIdSearch = (e: any) => {
  const id = e.currentTarget.value;
  dispatch('dataLiveFilter', { value: id, key: 'id' });
};
const handleStatusSearch = (value: any) => {
  dispatch('dataLiveFilter', { value, key: 'status' });
};

const handleDataUser = (e: any) => {
  const { value } = e.currentTarget;
  dispatch('dataLiveFilter', { value, key: 'name' });
};

const handleSearch = () => {
  const idInput = document.querySelector('.ninjadash-data-id') as HTMLInputElement;
  const statusSelect = document.querySelector('.ninjadash-data-status .ant-select-selection-item') as HTMLDivElement;
  if (idInput && statusSelect) {
    const id = idInput.value;
    const status = statusSelect.title;
    dispatch('filterWithSubmit', { id, status });
  }
};

const selectedRowKeys = ref([]); // Check here to configure the default column

const onSelectChange = (changableRowKeys: any) => {
  console.log('selectedRowKeys changed: ', changableRowKeys);
  selectedRowKeys.value = changableRowKeys;
};

const rowSelections = computed(() => {
  return {
    selectedRowKeys: unref(selectedRowKeys),
    onChange: onSelectChange,
    hideDefaultSelections: true,
  };
});
</script>

<template>
  <DataTableStyleWrap>
    <div v-if="filterOption" class="ninjadash-datatable-filter">
      

      <div class="ninjadash-datatable-filter__right">
        <a-input @change="handleDataUser" size="default" placeholder="Search">
          <template #prefix>
            <unicon name="search"></unicon>
          </template>
        </a-input>
      </div>
    </div>

    <div class="ninjadasj-datatable">
      <TableWrapper class="table-data-view table-responsive">

        <a-table
          :pagination="{ pageSize: 10, showSizeChanger: true }"
          :data-source="tableData"
          :columns="columns"
        />
      </TableWrapper>
    </div>
  </DataTableStyleWrap>
</template>

It shows the switch button but it doesn’t work.
What’s wrong with this code?

I want to know how to fix it and why.

When I click the switch button, it should changes it’s ‘checked’ state but it stays initial state

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật