vue · identify one record in table

backgroud:

1. database table doesn't have a primary key or unique key
2. cant add a composite key from the database perspective
3. need to perform CRUD (Create, Read, Update, Delete) operations line by line, then  need to use all column data of a row to identify a specific row from the front-end.

assumed example:

in one play-out page, a table (named product) was showed, this table has no primary key or unique key. If you want to locate a line in db, such as deleting or updating, need to use all its columns.

table-schema listed as below:

PRODUCT_ID
PRODUCT_NAME
UNIT_PRICE
PRODUCT_DETAILS
PRODUCT_IMAGE
IMAGE_MIME_TYPE
IMAGE_FILENAME
IMAGE_CHARSET
IMAGE_LAST_UPDATED

in front-end, table `product` was supported to insert, update or delete, line by line, every action can manipulate one row.

cant add composite key in this table from db perspective. want to identify a row from technology of vue.

possible solution:

You can do this by storing the entire row data when a row is selected in the Vue.js interface. Here's a potential solution:

  1. First, let's suppose that you fetch your data from the backend and store it in a variable in your Vue.js component:
    data() {
      return {
        products: [],
        selectedProduct: null,
      };
    },
    

  2. When you display your data, you can add a @click event handler to each row to store the entire row data:
    <table>
      <tr v-for="(product, index) in products" :key="index" @click="selectProduct(product)">
        <td>{{ product.PRODUCT_ID }}</td>
        <!-- other cells... -->
      </tr>
    </table>
    
  3. Define the selectProduct method to set the selected product:
    methods: {
      selectProduct(product) {
        this.selectedProduct = product;
      }
    }
    
  4. Then, when you want to update or delete a row, you would send the entire selectedProduct object back to the backend:
    methods: {
      deleteProduct() {
        // You would replace this with your actual API call
        api.delete('products', this.selectedProduct);
      },
    
      updateProduct(updatedProduct) {
        // You would replace this with your actual API call
        api.update('products', updatedProduct);
      }
    }
    

    In this case, the backend would need to use all the column data to identify the correct row to update or delete. This would likely involve finding the row where all columns match the data of the selected product.

    This approach works, but keep in mind it has a few potential downsides. It might not perform well if your table has many columns or if the columns contain a lot of data. Also, if two users are editing the same row at the same time, one user's changes might overwrite the other's. It's generally better to have a unique identifier for each row if possible.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值