Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

<select id="dddate" className="dropdownclass"…

  1. User

    <select id="dddate" className="dropdownclass" defaultValue={Object.keys(date)[Object.keys(date).length - 1]} onClick={(e) => { if (e.target.value != date) { setShowTable(false);

                fetch(
                  `${configData.SERVER_URL}/getdatebyhostname?Hostname=${hostID}`
                )
                  .then((res) => res.json())
                  .then((response) => {
                    console.log(response.comment);
                    setDate(response.comment);
                    //handleCrAtDate(String(response.comment));
                  });
                fetch(
                  `${configData.SERVER_URL}/getcriticityatdate?Hostname=${hostID}&date=${date}`
                )
                  .then((res) => res.json())
                  .then((response) => {
                    console.log(response.comment);
                    setCrDate(response.comment);
                  });
              }
            }}
          >
            <option value="0"> Select Date </option>
            {date && date !== undefined
              ? date
                  .sort((a, b) => {
                    const dateA = a[0].substring(0, 19);
                    const dateB = b[0].substring(0, 19);
                    return new Date(dateB) - new Date(dateA);
                  })
                  .map((hostname, index) => {
                    const optionClassName =
                      crDate && crDate.length === 1 && crDate[0].includes(1)
                        ? "redgc"
                        : crDate && crDate.length === 1 && crDate[0].includes(2)
                        ? "orangegc"
                        : crDate && crDate.length === 1 && crDate[0].includes(3)
                        ? "browngc"
                        : crDate && crDate.length === 1 && crDate[0].includes(4)
                        ? "greengc"
                        : "";
                    console.log(optionClassName);
                    return (
                      <option
                        key={index}
                        value={hostname[0]}
                        className={optionClassName}
                      >
                        {hostname[0]}
                      </option>
                    );
                  }) //.reverse()
              : "No date"}
          </select>
    

    böyle bir select menüm var. hatası şu: menüdeki her şeyi aynı renk yapıyor ve ilk başta renk doğru olmuyor, yeşil oluyor sonraki tıklamada düzeliyor.

  2. Assistant

    Bu hatanın nedeni, defaultValue özelliğinin ilk render sırasında set edilmesi ve ardından tıklama olayında state'in güncellenmesiyle ilgilidir. defaultValue prop'u sadece başlangıç değeri olarak kullanılmalıdır ve genellikle değişken bir değer olmamalıdır.

    Bu hatayı düzeltmek için, defaultValue prop'unu kaldırın ve value prop'unu kullanarak seçilen değeri kontrol edin. Ayrıca setCrDate fonksiyonunu fetch isteği tamamlandıktan sonra çağırmalısınız.

    Aşağıda hatayı düzeltmek için yapısal bir örnek bulunmaktadır:

    <select
      id="dddate"
      className="dropdownclass"
      value={selectedDate}
      onChange={(e) => {
        const selectedValue = e.target.value;
        setSelectedDate(selectedValue);
        
        setShowTable(false);
    
        fetch(
          `${configData.SERVER_URL}/getdatebyhostname?Hostname=${hostID}`
        )
          .then((res) => res.json())
          .then((response) => {
            console.log(response.comment);
            setDate(response.comment);
            //handleCrAtDate(String(response.comment));
          });
        fetch(
          `${configData.SERVER_URL}/getcriticityatdate?Hostname=${hostID}&date=${selectedValue}`
        )
          .then((res) => res.json())
          .then((response) => {
            console.log(response.comment);
            setCrDate(response.comment);
          });
      }}
    >
      <option value="0"> Select Date </option>
      {date && date !== undefined ? (
        date
          .sort((a, b) => {
            const dateA = a[0].substring(0, 19);
            const dateB = b[0].substring(0, 19);
            return new Date(dateB) - new Date(dateA);
          })
          .map((hostname, index) => {
            const optionClassName =
              crDate && crDate.length === 1 && crDate[0].includes(1)
                ? "redgc"
                : crDate && crDate.length === 1 && crDate[0].includes(2)
                ? "orangegc"
                : crDate && crDate.length === 1 && crDate[0].includes(3)
                ? "browngc"
                : crDate && crDate.length === 1 && crDate[0].includes(4)
                ? "greengc"
                : "";
            console.log(optionClassName);
            return (
              <option
                key={index}
                value={hostname[0]}
                className={optionClassName}
              >
                {hostname[0]}
              </option>
            );
          })
      ) : (
        <option>No date</option>
      )}
    </select>
    

    Yukarıdaki örnekte, selectedDate adında bir state ve setSelectedDate adında bir state güncelleme fonksiyonu kullanıldığı varsayılmıştır. Bu değişiklikle, seçilen değer, value propu üzerinden kontrol edilir ve onChange olayında state güncellenir. Ayrıca setCrDate fonksiyonu, fetch isteği tamamlandıktan sonra çağrılmaktadır.