簡體   English   中英

如何對表格的行進行分組(按“數量”單元格)?

[英]How can I group the rows of a table (by the "Quantity" cell)?

我認為這是一個常見問題,但我不知道任何明顯的解決方案。

我有一個要訂購的物品表(即銷售清單)。 此表中的一列是“數量”。 是否有工具(庫或其他東西)可以自動對記錄(行)進行分組,並增加單元格的“數量”?

簡而言之,我希望表格將其轉換為(行 ID、名稱、價格):

3, Red ball, 3 pieces, $ 5
3, Red ball, 3 pieces, $ 5
3, Red ball, 3 pieces, $ 5

進入這個(名稱,數量,價格):

Red ball, 3 pieces, $ 5

問題的詳細信息如下......為什么我需要這個? 我將表格的每個位置(行)存儲在服務器上。 但是由於“數量”列,我必須在新模型中創建新記錄,在該模型中我為每個項目創建重復記錄。 例如,字符串(名稱、數量、價格):

Red ball, 3 pieces, $ 5

復制到另一個模型作為(行 ID,名稱,價格):

3, Red ball, 3 pieces, $ 5
3, Red ball, 3 pieces, $ 5
3, Red ball, 3 pieces, $ 5

這樣做是為了能夠處理這些記錄(例如,計算它們的數量、鏈接到其他記錄等)。 但是一切都會破壞“數量”列,如果它不存在,那么我就不必在我的應用程序的不同模型中復制任何內容,我只會顯示相同的記錄,並且已經“一些插件/庫/插件/片段I ASK YOU ABOUT”將顯示一個表格並自動將相同的行分組為一個,在客戶端 (JS) 的“數量”列中計算它們的數量。 請原諒我描述我的問題,我認為問題的第一部分就足夠了......

我目前正在使用 TABULATOR (JS) 表,這是一個非常酷的工具,但我還沒有在其中找到這樣的內置函數......

來自服務器的數據格式:

 var tabledata = [
    
    {
        order_item_id:6,
        name:'red ball',
        price:5,
        
     },

    {
        order_item_id:7,
        name:'red ball',
        price:5,
        
     },

    {
        order_item_id:8,
        name:'red ball',
        price:5,
        
     },

     ]

 const tabledata = [{ order_item_id: 5, name: 'blue ball', price: 3, }, { order_item_id: 6, name: 'red ball', price: 5, }, { order_item_id: 7, name: 'red ball', price: 5, }, { order_item_id: 8, name: 'red ball', price: 5, }, { order_item_id: 9, name: 'blue ball', price: 3, }, { order_item_id: 10, name: 'blue ball', price: 3, }, { order_item_id: 11, name: 'red ball', price: 5, }, { order_item_id: 12, name: 'yellow ball', price: 15, }] const grouped = tabledata.reduce((acc, item) => { /* * check whether the accumulator already contains a product with the same name * (if it does, return its index otherwise return -1) */ const index = acc.findIndex(_item => _item.name === item.name); if (index > -1) { /* * if the product is already present, increase its quantity by 1 */ acc[index].quantity = acc[index].quantity + 1; } else { /* * if the product is not present, add it to the accumulator array */ acc.push({ name: item.name, unit_price: item.price, quantity: 1 }); } return acc; }, []); console.log('### TABLEDATA:\\n', tabledata); console.log('### GROUPED:\\n', grouped);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM