如何使用服务表格中的待办事项填充组合框

我想用todo填充一个组合框,但是当我单击comboBox时页面空白。

这是我的服务模式:

 const mongoose = require('mongoose');
 const { Schema } = mongoose;
 const serviceSchema = new Schema({
  name: String,description: String,pricePerHour: Number,relatedTodos: 
    [{type: mongoose.Schema.Types.ObjectId,ref:'todos'}],createdAt: Date,UpdatedAt: Date
 });

 mongoose.model('services',serviceSchema);

这是我的待办事项模型:

const mongoose = require('mongoose');
const { Schema } = mongoose;

    const todoSchema = new Schema({
   name: String,isDone: Boolean,updatedAt: Date
  });

 mongoose.model('todos',todoSchema);

actionsTodos.js:

import axios from 'axios';
import { typesTodos } from './types';

export const listTodos = () => async dispatch => {
 const res = await axios.get('/api/todos');
  dispatch({type: typesTodos.LIST_TODOS,payload: res.data});
 }

ReducerTodos.js:

import { typesTodos} from '../actions/types';

const INITIAL_STATE = {
  listTodos: [],todo: {},loading: false,errors: {}
 };

 export default function(state = INITIAL_STATE,action) {
   switch (action.type) {

 case typesTodos.LIST_TODOS :
        return{
          ...state,listTodos: action.payload
         };

     default:
       return state;
   }
 }

ReducerIndex.js:

import { combineReducers } from 'redux';
import { reducer as reduxForm } from 'redux-form';
import reducerTodos from './ReducerTodos';
import reducerServices from './ReducerServices';

export default combineReducers({
  form: reduxForm,listTodos: reducerTodos,servicesDs: reducerServices,});

最后是ServiceForm.jsx:

 import React,{ Component } from 'react';
 import { reduxForm,Field,FieldArray} from 'redux-form';
 import { Link } from 'react-router-dom';
 import {listTodos} from '../../actions/actionsTodos';
 import { connect } from 'react-redux';
 import ComboBox from 'react-widgets/lib/Combobox';
 import moment from 'moment';
 import momentLocaliser from 'react-widgets-moment-localizer';
 import 'react-widgets/dist/css/react-widgets.css';

 momentLocaliser(moment)


class ServiceForm extends Component {
   componentWillReceiveProps = nextProps => {
     // Load Contact Asynchronously
     const { service } = nextProps;
     if (service._id !== this.props.service._id) {
       // Initialize form only once
      this.props.initialize(service);
       this.isUpdating = true;
     }
   };

   renderField = ({ input,label,type,meta: { touched,error } }) => (
     <div classname='form-group'>
      <label forname={input.name}>{label}</label>
      <input
        {...input}
        type={type}
        classname='form-control'
        id={input.name}
        placeholder={input.label}
     />
      <div classname='text-danger' style={{ marginBottom: '20px' }}>
        {touched && error}
      </div>
     </div>
  );

renderComboBox = ({ input,data,valueField,textField }) =>

<ComboBox {...input}
data={data}
valueField={valueField}
textField={textField}
onChange={input.onChange}
/> 


componentDidmount(){
  this.props.listTodos();
}


    renderArrayTodos = ({ fields,meta: { error,submitFailed } }) => (
      <ul>
        <li>
          <button type="button" onClick={() => fields.push()}>Add todo</button>
          {submitFailed && error && <span>{error}</span>}
         </li>
        {fields.map((todos,index) =>
          <li key={index}>
            <button
              type="button"
              title="Remove Todo"
              onClick={() => fields.remove(index)}/>

        <div>
           <label>todo</label>
         <Field
        name="relatedTodos"
         type='ComboBox'
        data={this.searchTodos}
        valueField="value"
        textField="todo"
        component={this.renderComboBox}
       />
       </div>
          </li>

        )}

        {error && <li classname="error">{error}</li>}
      </ul>
    );
       searchTodos = () =>{
      return this.props.listTodos.map((todo)=>{

        return(
          <div>
            <ul>
              <li>id: {todo._id}</li>
              <li>name: {todo.name}</li>
              <li>description: {todo.description}</li>
              <li>isDone: {todo.isDone}</li>
            </ul>

           </div>
        )
     });


     ArrayFieldTodos = props => {
     const { handleSubmit,pristine,reset,submitting } = props
       return(
        <form onSubmit={handleSubmit}>
         <FieldArray name="relatedTodos" component={this.renderArrayTodos} />
           <div>
             <button type="submit" disabled={submitting}>
              Submit
            </button>
        <button type="button" disabled={pristine || submitting} onClick={reset}>
          Clean Values
        </button>    
      </div>
        </form>
      )
     }

  render() {
    const { handleSubmit,loading} = this.props;

    if (loading) {
      return <span>Loading...</span>;
    }
    return (
      <form onSubmit={handleSubmit}>
        <Field
          name='name
          type='text'
          component={this.renderField}
          label='Name'
        />
        <Field
          name='description'
          type='text'
          component={this.renderField}
          label='Description'
        />
         <Field
          name='pricePerHour'
          type='text'
          component={this.renderField}
          label='PricePerHour'
        />
             <FieldArray
             name = 'relatedTodos'
             component ={this.ArrayFieldTodos}
             label = 'RelatedTodos'
             />
        <div>
      </div>



            <Link classname='btn btn-light mr-2' to='/services'>
          Cancel
        </Link>
        <button classname='btn btn-primary mr-2' type='submit'>
          {this.isUpdating ? 'Update' : 'Create'}
        </button>
      </form>
    );
  }
}
    function mapstatetoProps(state){
     return{
    listTodos: state.listTodos.listTodos
      }
    }
    ServiceForm = connect(mapstatetoProps,{listTodos})(ServiceForm);

    export default reduxForm({form:'service')(ServiceForm);

我点击服务。服务使我很好。然后单击“新建”。我可以看到按钮添加待办事项。然后单击它,它会打开一个comboBox。但是,当我单击ComboBox时,页面为空白。

怎么了?错误可能在searchTodos中。

xbl_lkz 回答:如何使用服务表格中的待办事项填充组合框

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3130773.html

大家都在问