From 19ce053d350d0a8f070096a5da993b455967a785 Mon Sep 17 00:00:00 2001 From: zhengpengju Date: Sun, 27 Mar 2022 21:35:38 +0800 Subject: [PATCH] fix score setter --- admin/src/global.less | 3 + .../attestation/step/index.tsx | 6 +- .../components/ScoreSetter.tsx | 205 +++++++----------- .../examinationrules/normal/step/index.tsx | 37 +++- 4 files changed, 117 insertions(+), 134 deletions(-) diff --git a/admin/src/global.less b/admin/src/global.less index 0d6eb49..918553c 100644 --- a/admin/src/global.less +++ b/admin/src/global.less @@ -64,4 +64,7 @@ ol { } .ant-pro-field-index-column-border{ background-color: #999 !important; +} +main{ + min-width: 1024px; } \ No newline at end of file diff --git a/admin/src/pages/examinationrules/attestation/step/index.tsx b/admin/src/pages/examinationrules/attestation/step/index.tsx index 348c2b9..7f245c9 100644 --- a/admin/src/pages/examinationrules/attestation/step/index.tsx +++ b/admin/src/pages/examinationrules/attestation/step/index.tsx @@ -43,6 +43,7 @@ export default () => { const [questionTypeValues, setQuestionTypeValues] = useState([]); // 题型数据[{count:0, score:0, score_harf:0}] const [questionType, setQuestionType] = useState([]); // 题型 + const [passScore, setPassScore] = useState(0) const [rulesId, setRulesId] = useState(0); // 规则id, 保存新建的rules_id const [subjectId, setSubjectId] = useState(0); // 关联主题id @@ -480,7 +481,7 @@ export default () => { key="primary" onClick={async () => { /** 获取试卷 */ - const {question_type_score} = await queryRulesPaper( + const {question_type_score, question_list} = await queryRulesPaper( { rules_id: params?.id || rulesId, page_number: 1, @@ -488,6 +489,7 @@ export default () => { } ); console.log('_data:', question_type_score) + setPassScore(question_list[0]?.pass_score) //questionTypeValues?.map((item,idx)=>{return {...item, score:values[idx]?.score, score_harf:values[idx]?.score_harf}}) //setQuestionTypeValues() // 更新分值成功后设置组件分值 handleScoreModalVisible(true) @@ -660,7 +662,7 @@ export default () => { return true; }} > - + { - - const {questionTypeValues } = props - - console.log('questionTypeValues::::::::::', questionTypeValues) - //console.log('passScoreValue:::', passScoreValue) - - const match = useRouteMatch(); - console.log('match', match); - - const [typeScore, setTypeScore] = useState(questionTypeValues.map(item=>({question_type:item.code, score: item.score, score_harf: item.score_harf}))) ; // 题型分数数组 [{"question_type": "0","score": "3","score_harf": "0"},] + const [flag, setFlag] = useState([]) // 分数变更标记 + const {questionTypeValues, passScore: passScoreValue } = props + const [typeScore, setTypeScore] = useState(questionTypeValues.map((item: { code: number; score: number; score_harf: number; })=>({question_type:item.code, score: item.score, score_harf: item.score_harf}))) ; // 题型分数数组 [{"question_type": "0","score": "3","score_harf": "0"},] //const type = history.location.pathname === '/questionbank/attestation' ? 1 : 0 ; // 题库类型 - const [dataSource, setDataSource] = useState([]); //const [selectedRowsState, setSelectedRows] = useState([]); //const [addType, setAddType] = useState({name: '', value: 0}); - const [sumScore, setSumScore] = useState(0); - const [passScore, setPassScore] = useState(0); // 设置通过分数线 + //const [sumScore, setSumScore] = useState(); + const [passScore, setPassScore] = useState(passScoreValue); // 设置通过分数线 //let sumScore = 0 // 总分 - console.log('init........', typeScore) - const formRef = useRef(); - - //setTypeScore(questionTypeValues) // 初始化分值 - //const actionRef = useRef(); + //console.log('init........', typeScore) + const formRef = useRef(); /** 列表项定义 */ const columns: any = [ @@ -70,7 +51,7 @@ const ScoreSetter = (props: any, ref: any) => { dataIndex: 'score', key: 'code', width: 400, - render: (text, record, index) => + render: (_text: any, record: { code: number; }, index: string | number) => 每题 { size="small" onChange={(e)=>{ const value = e.currentTarget.value; - console.log('row', index) - console.log('v1',value) - const scoreData = typeScore?.map((item, key)=>{ + const scoreData = typeScore?.map((item: { score: any; }, key: string | number)=>{ return {...item, score: ((index === key) ? Number(value) : Number(item?.score))} // 题型分数 }) - console.log('question',questionTypeValues) - console.log('scoreData-----------------',scoreData) + // 标记重新设置项(序号) + if(flag.indexOf(index) === -1){ + setFlag([...flag, index]) + } setTypeScore(scoreData) }}/> @@ -113,8 +94,7 @@ const ScoreSetter = (props: any, ref: any) => { >{ // const _data = []; - console.log('typeScore',typeScore) - typeScore?.forEach((item, key)=>{ + typeScore?.forEach((item: { score_harf: any; }, key: number)=>{ _data.push({...item, score_harf: ((index === key) ? value : item?.score_harf)}) }) setTypeScore(_data) @@ -128,118 +108,93 @@ const ScoreSetter = (props: any, ref: any) => { title: '题目数量', dataIndex: 'count', width: 80, - render: (text, _, index) => <>{questionTypeValues ? questionTypeValues[index]?.count : '0'}, - /* - editable: (text, record, index) => { - return true; - },*/ + render: (_text: any, _: any, index: number) => <>{questionTypeValues ? questionTypeValues[index]?.count : '0'}, }, { title: '总分值', dataIndex: 'score_sum', //key: 'age', width: 80, - render: (text, _, index) => { - {/*console.log('typeScore---', typeScore)*/} + defaultValue:()=>{ return 123}, + render: (_text: any, _: any, index: number) => { + //console.log('typeScore---', typeScore) + //console.log('questionTypeValues',questionTypeValues) + //console.log('==score==',typeScore) return <> - {(typeScore && questionTypeValues) ? ((typeScore[index]?.score > 0 ? typeScore[index]?.score : 0) * questionTypeValues[index]?.count).toFixed(1) : '0'} + {flag?.indexOf(index) === -1 ? + questionTypeValues[index]?.score * questionTypeValues[index].count : + (typeScore && questionTypeValues) ? ((typeScore[index]?.score > 0 ? typeScore[index]?.score : 0) * questionTypeValues[index]?.count).toFixed(1) : '0' + } }, } ]; - //const labels = ['A','B','C','D','E'] - /** 获取总题数 */ let sumQuestion = 0; - questionTypeValues?.forEach((item) => { + questionTypeValues?.forEach((item: any) => { sumQuestion += item.count; }); - // 获取题型 - const { data } = useRequest(() => { - return queryQuestionType(); - },{ - formatResult: (result) => { - const _data = []; - if(result.list){ - result.list.forEach((item)=>{ - _data.push({ - name: item?.name, - code: item?.code, - count: 0, - score_sum: 0, - harf: 0 - }) - }) - } - console.log('_data',_data) - return _data; - } - }); - - useEffect(() => { - setDataSource(data); - console.log('data', data) - const _typeScore = []; - data?.forEach((item)=>{ - _typeScore.push({question_type: item?.code, score: item?.count, score_harf: 0}) - }) - setTypeScore(_typeScore) // 初始值 - //return ()=>{} - }, [data]); - - /** 计算总分 */ - useEffect(() => { - console.log('typeScore', typeScore) - let _sum = 0; - typeScore?.forEach((item, index)=>{ - _sum += (item.score > 0 ? item.score : 0 ) * questionTypeValues[index]?.count; - }) - - setSumScore(_sum) - //return ()=>{} - }, [typeScore]); - // 暴露组件的方法 接受外部获取的ref useImperativeHandle(ref, () => ({ // 构造ref的获取数据方法 getData: () => { - return typeScore?.map((item, idx)=>{return {...item, count: questionTypeValues[idx]?.count, sum: item?.score * questionTypeValues[idx]?.count}}); + return typeScore?.map((item: any, idx: number)=>{ + return { + ...item, + count: questionTypeValues[idx]?.count, + sum: item?.score * questionTypeValues[idx]?.count, + } + }) }, getValue: () => { - return passScore; + return passScore }, getSum: () => { - console.log('getSum', sumScore) - return sumScore; + return typeScore?.map((item: any, key: number)=>{return item?.score * questionTypeValues[key].count})?.reduce((total: number, item: number)=>(total + item)) || 0 ; }, })); return ( -
- { - return 共计 {sumQuestion} 题,总计 {sumScore} 分 通过分数线 - { - setPassScore(value) - }} /> - 分}} - //formMapRef={formMapRef} - //rowKey={'code'} - bordered - cardProps={{bodyStyle:{padding:0}}} - recordCreatorProps={false} - search={false} - options={false} - value={dataSource} - columns={columns} - /> - - + <> +
+ { + return 共计 {sumQuestion} 题,总计 { + typeScore?.map((item: any, key: number)=>{return item?.score * questionTypeValues[key].count})?.reduce((total: number, item: number)=>(total + item)) || 0 + } 分 通过分数线 + { + setPassScore(value) + }} /> + 分}} + //formMapRef={formMapRef} + rowKey={'code'} + request={async ()=>{ + const result = await queryQuestionType() // 从字典获取题型数据 [{code: 0, name:"单选题"},] + setTypeScore(questionTypeValues?.map((item: { score: any; score_harf: any; }, key: number)=>{ + return {question_type: result.list[key]?.code, score: item?.score, score_harf: (item?.score_harf || 0)} + })) + return {data:questionTypeValues?.map((item: any, key: number)=>({ + ...item, + code: result.list[key]?.code, + question_type: result.list[key]?.code, + name: result.list[key]?.name, + count: result.list[key]?.count + }))} + }} + bordered + cardProps={{bodyStyle:{padding:0}}} + search={false} + options={false} + columns={columns} + pagination={false} + /> + + ); }; // forwardRef这个组件能够将其接受的 ref 属性转发到其组件树下 diff --git a/admin/src/pages/examinationrules/normal/step/index.tsx b/admin/src/pages/examinationrules/normal/step/index.tsx index fd77d24..1f5c1e1 100644 --- a/admin/src/pages/examinationrules/normal/step/index.tsx +++ b/admin/src/pages/examinationrules/normal/step/index.tsx @@ -276,7 +276,7 @@ export default () => { }); ruleData = data } - console.log(ruleData, 'ruleData'); + console.log('ruleData', ruleData); return ( @@ -600,27 +600,50 @@ export default () => {
试卷信息与设置 - 共 {questions?.length} 题 {paperInfo?.sum_score || '-'}{ console.log('scoreData', scoreData)} 分 + 共 {questions?.length} 题,共 {/*paperInfo?.sum_score || 0*/} { + questionType?.length > 0 ? + questionType?.map((item) => { + return typeQuestionCount[Number(item.code)] * scoreData?.filter(val=>(val.question_type === Number(item?.code)))[0]?.score || 0; + }).reduce((total, item)=>(total + item)) : 0 + } 分 {questionType.map((item) => { - return {item?.name} 共 {typeQuestionCount[Number(item.code)]} 题, 共 {typeQuestionCount[Number(item.code)] * scoreData?.filter(val=>(val.question_type === Number(item?.code)))[0]?.score || 0} 分 + const type_sum = typeQuestionCount[Number(item.code)] * scoreData?.filter(val=>(val.question_type === Number(item?.code)))[0]?.score || 0; + return {item?.name} 共 {typeQuestionCount[Number(item.code)]} 题, 共 {type_sum} 分 } )} - 请选择试题组卷并设置分值后保存} placement="bottom" color='#108ee9'> @@ -775,8 +798,8 @@ export default () => { handleScoreModalVisible(false) return true; }} - > - + >{console.log('q1',questionTypeValues)}{console.log('q2',scoreData)} +