简体   繁体   English

从 Javascript object 写入和读取数据

[英]Write and read data from Javascript object

I have the following small setup in Svelte:我在 Svelte 中有以下小设置:

日历和复选框

Calendar and checkboxes work.日历和复选框有效。 What I am trying to understand is the best way to read and store the option responses for each date that is selected by the user.我想了解的是读取和存储用户选择的每个日期的选项响应的最佳方式。

Here is what I currently have in my svelte file:这是我目前在我的 svelte 文件中的内容:

<script>
    import Datepicker from "praecox-datepicker";
    import { onMount } from 'svelte';

    export let name;

    let day;
    let questions = [
            {value: "Condition 1", checked: false},
            {value: "Condition 2", checked: false},
            {value: "Condition 3", checked: false},
            {value: "Condition 4", checked: false}
        ];
    let selectedQuestions = [];

    $:  questions = {...questions,
        [day]: [
            {value: "Condition 1", checked: false},
            {value: "Condition 2", checked: false},
            {value: "Condition 3", checked: false},
            {value: "Condition 4", checked: false},
    ]
    };


    $: {
        selectedQuestions = questions.savedValues[day]
};

    $: selectedQuestions = Object.fromEntries(Object.entries(questions).filter(([key, value]) => key == day))

    $: day = day;
    $: readDate = new Date(day);
    $: mm = readDate.toLocaleString("en-US", {month: "long"});
    $: dd = readDate.toLocaleString("en-US", {day: "numeric"});
    $: yyyy = readDate.toLocaleString("en-US", {year: "numeric"});

    onMount(async () => {
        day = new Date()
    });
</script>

<main>
        <div class="row">
            <div class="col-sm">
                <Datepicker pickerRule='singleChoice' bind:pickerResult={day}/>
            </div>
            <div class="col-sm">
                <p>{mm} {dd}, {yyyy}</p>
            <ul>
                {#each questions as question}
                <li>
                    <input type=checkbox checked={question.checked}>
                    {question.value}
                </li>
                {/each}

            </ul>
            </div>
</main>

The current issue is I'm not sure how to access the filtered object and if I'm even accessing that properly.当前的问题是我不确定如何访问过滤后的 object 以及我是否能够正确访问它。

To recap the workflow would need to handle the following:回顾一下工作流程需要处理以下内容:

  1. User loads page, options are presented and default to false.用户加载页面,显示选项并默认为 false。
  2. User selects options for first date, these are saved, and user moves to new day.用户选择第一次约会的选项,这些选项被保存,然后用户移动到新的一天。
  3. New list of checkboxes are presented, all default to false.显示了新的复选框列表,所有复选框都默认为 false。 User updates options and these are saved.用户更新选项并保存这些选项。
  4. User revisits item and updates previously input information.用户重新访问项目并更新先前输入的信息。

You need:你需要:

1.Upgrade praecox-datepicker to version 1.0 or higher. 1.将praecox-datepicker datepicker升级到1.0或更高版本。

2.Use bind:selected to get the selected result. 2.使用bind:selected获取选中的结果。

3.Pass the selection result (start date) to the viewDate property to keep the view synchronized. 3.将选择结果(开始日期)传递给viewDate属性,保持视图同步。

4.If you want a timely response, please submit an issue directly on Github. 4.如需及时回复,请直接在Github提交issue。

REPL REPL

See more.查看更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM