0

根据 React-Native-Calendars 文档,这里是带有硬编码标记日期的测试代码,显示日历上的点:

const FirstDot = { key: 'First', color: 'blue' };
const SecondDot = { key: 'Second', color: 'blue' };

<Calendar
   current={new Date()}
   markingType={'multi-dot'}
   markedDates={{
      '2019-04-15': { dots: [FirstDot, SecondDot] },
      '2019-04-14': { dots: [FirstDot] },
   }}
/>

基本上我想根据数据显示一两个点。假设我正在从 SQLite 检索数据,如何在 React Native 中动态填充标记日期?

this.state = { markedDates: {} };  //how to declare the state object?
//other codes.....

db.transaction((tx) => { 
    let objMarkedDates = this.state.markedDates;
    tx.executeSql('SELECT myDates, dataDots FROM myTable', 
    [], (tx, results) => {
        const len = results.rows.length;
        if (len > 0) {
            for (let i = 0; i < len; ++i) {
                if (results.rows.item(i).dataDots === 2) { //show 2 dots
                    //How to populate the date and the dots to the object???
                }
            }
        }
    }); 

    this.setState({ markedDates: objMarkedDates });
});

<Calendar
   current={new Date()}
   markingType={'multi-dot'}
   markedDates={this.state.markedDates}
/>
4

2 回答 2

0

根据 user11630092 的回答:

const MARKED_DATE_CONTAINER: ViewStyle = {
    backgroundColor: AppColors.primary,
    borderRadius: 0
}

const MARKED_DATE_TEXT: TextStyle = {
    color: AppColors.white
}

在此处输入图像描述

const customStyles = {
    container: {...MARKED_DATE_CONTAINER},
    text: {...MARKED_DATE_TEXT}
}
var element = {};

for(var i =0;i<array.length;i++){
    var a = {customStyles: {...customStyles}}
    element[`${formatMyDate(array[i].startDate)}`] = a;
}

结果

于 2020-08-01T11:34:16.867 回答
0

假设您的数据是数组形式,这是我为 json 数据数组编写的

            var element = {};

            for(var i =0;i<this.state.array.length;i++){
                if((Object.keys(element)).indexOf(this.state.array[i].date) > -1){
                  for(var j = 0;j<Object.keys(element).length;j++){
                    if(this.state.array[i].start_date === Object.keys(element)[j]){
                      var a ={key:this.state.array[i].id,color:'red'}
                      element[this.state.array[i].date].dots.push(a);
                    }
                  }
                }else{
                  var a = {dots:[{key:this.state.array[i].id,color:'red'}]}
                  element[this.state.array[i].date+''] = a;
                }
            }
于 2019-06-11T09:11:15.780 回答