第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

顯示錯(cuò)誤以上傳文件或禁用 de 按鈕

顯示錯(cuò)誤以上傳文件或禁用 de 按鈕

哈士奇WWW 2022-12-09 16:28:22
我有這個(gè) React 代碼,您也可以在此處查看代碼https://stackblitz.com/edit/react-excel-to-json-parser。當(dāng)我在沒(méi)有上傳文件的情況下單擊“處理觸發(fā)器”按鈕時(shí)。該應(yīng)用程序打破了如何處理用戶必須上傳文件或禁用按鈕“處理觸發(fā)器”直到用戶上傳文件的警報(bào)錯(cuò)誤。import React, { Component } from 'react';import { Fabric } from 'office-ui-fabric-react/lib/Fabric';import { DefaultButton } from 'office-ui-fabric-react/lib/Button';import XLSX from 'xlsx';import { make_cols } from './MakeColumns';import { SheetJSFT } from './types'; class ExcelReader extends Component {  constructor(props) {    super(props);    this.state = {      file: {},      data: [],      cols: []    }    this.handleFile = this.handleFile.bind(this);    this.handleChange = this.handleChange.bind(this);  }   handleChange(e) {    const files = e.target.files;    if (files && files[0]) this.setState({ file: files[0] });  };   handleFile() {    /* Boilerplate to set up FileReader */    const reader = new FileReader();    const rABS = !!reader.readAsBinaryString;     reader.onload = (e) => {      /* Parse data */      const bstr = e.target.result;      const wb = XLSX.read(bstr, { type: rABS ? 'binary' : 'array', bookVBA : true });      /* Get first worksheet */      const wsname = wb.SheetNames[0];      const ws = wb.Sheets[wsname];      /* Convert array of arrays */      const data = XLSX.utils.sheet_to_json(ws);      /* Update state */      this.setState({ data: data, cols: make_cols(ws['!ref']) }, () => {        console.log(JSON.stringify(this.state.data, null, 2));      });     };     if (rABS) {      reader.readAsBinaryString(this.state.file);    } else {      reader.readAsArrayBuffer(this.state.file);    };  } 
查看完整描述

3 回答

?
胡子哥哥

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊

將 handleFile() 代碼放在 try catch 塊中


handleFile() {

    try {

        /* Boilerplate to set up FileReader */

        const reader = new FileReader();

        const rABS = !!reader.readAsBinaryString;

    

        reader.onload = (e) => {

          /* Parse data */

          const bstr = e.target.result;

          const wb = XLSX.read(bstr, { type: rABS ? 'binary' : 'array', bookVBA : true });

          /* Get first worksheet */

          const wsname = wb.SheetNames[0];

          const ws = wb.Sheets[wsname];

          /* Convert array of arrays */

          const data = XLSX.utils.sheet_to_json(ws);

          /* Update state */

          this.setState({ data: data, cols: make_cols(ws['!ref']) }, () => {

            console.log(JSON.stringify(this.state.data, null, 2));

          });

    

        };

    

        if (rABS) {

          reader.readAsBinaryString(this.state.file);

        } else {

          reader.readAsArrayBuffer(this.state.file);

        };

    } catch(err) {

      console.log(err);

    }

  }


查看完整回答
反對(duì) 回復(fù) 2022-12-09
?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊

您可以disabled根據(jù)本地狀態(tài)將屬性添加到提交按鈕。


您的新本地狀態(tài)將是:


this.state = {

  file: {},

  isFileLoaded: false,

  data: [],

  cols: []

}

然后更新你的狀態(tài)handleChange:


handleChange(e) {

  const files = e.target.files;

  if (files && files[0]) this.setState({ file: files[0], isFileLoaded: true });

};

最后是您的提交輸入:


<input type='submit' disabled={!this.state.isFileLoaded} value="Process Triggers" onClick={this.handleFile} />

這是一個(gè)工作演示:https ://stackblitz.com/edit/react-excel-to-json-parser-g49uhh


查看完整回答
反對(duì) 回復(fù) 2022-12-09
?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

您是否要始終顯示錯(cuò)誤(未上傳文件時(shí))?要禁用提交按鈕,您可以嘗試


<input type='submit' 

   disabled={!this.state.file}

   value="Process Triggers"

   onClick={this.handleFile}

/>


查看完整回答
反對(duì) 回復(fù) 2022-12-09
  • 3 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)