簡體   English   中英

存儲與 mysql 表的列相關的值的最佳方法?

[英]Best way to store values related to columns of a mysql table?

如果問題不清楚,請道歉。 不解釋就很難傳達。 所以我從 mysql 數據庫動態獲取表數據,並在我的前端 React 應用程序中使用它。 在我的前端,我有一個包含對象的列數組。 每個 object 都有不同的屬性,如標題、字段、類型、隱藏等……這些對象中的每一個都代表一列及其相關設置。 我已經動態地獲取了列名,並且我知道如何獲取類型,但我不確定如何存儲 boolean 設置,例如可編輯、過濾和隱藏。

我想可能將它們存儲在 JSON 配置文件中,或者創建一個單獨的表來存儲每列的所有設置。

    //use map function to create columns from column_fields in props
    const columns2 = props.column_fields.map(column_name => {
        return (
            //TODO: correctly configure all attributes
            {title:column_name, field:column_name, type:'string', editable:'true', filtering:'true', editable:'true', hidden:'false'}
        )
    })

像這樣的東西

const props = { column_fields: ["id", "name", "age"] };

const column_settings = {
  id: {
    type: "string",
    editable: true,
    filtering: true,
    editable: true,
    hidden: false
  },
  name: {
    type: "string",
    editable: true,
    filtering: true,
    editable: true,
    hidden: false
  },
  age: {
    type: "number",
    editable: true,
    filtering: true,
    editable: true,
    hidden: false
  }
};

//use map function to create columns from column_fields in props
const columns2 = props.column_fields.map(column_name => {
  return (
    //TODO: correctly configure all attributes
    {
      title: column_name,
      field: column_name,
      ...column_settings[column_name]
    }
  );
});

因此,如果有人偶然發現這一點,我只需使用 JSON 文件來存儲我需要的有關特定表列的所有信息,例如:

{
    "table1": {
        "id": {
            "type": "numeric",
            "hidden": false,
            "filtering": true,
            "editable": "never"
        },
        "name": {
            "type": "string",
            "hidden": false,
            "filtering": true,
            "editable": null
        },
    },
    "table2": {
        "phone": {
            "type": "string",
            "hidden": false,
            "filtering": true,
            "editable": null
        },
        "age": {
            "type": "numeric",
            "hidden": false,
            "filtering": true,
            "editable": null
        }
    }
}

我在服務器端 php 中獲取此文件並將所有內容發送到我的前端。 現在我可以通過我的 React 項目中的 props 訪問值。

    //use map function to create columns from column_fields in props
    let i = 0;
    let columns = props.column_fields.map(column => {
        i++;
        if (column.field != 'updated_at') {
            return ({
                title: column.field, field: column.field, type: props.column_fields[i - 1].type, editable: props.column_fields[i - 1].editable,
                filtering: props.column_fields[i - 1].filtering, hidden: props.column_fields[i - 1].hidden
            })
        }
    })

希望這種方法能長期有效。

暫無
暫無

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

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