Показать сообщение отдельно
  #14 (permalink)  
Старый 10.02.2021, 22:04
Аватар для voraa
Профессор
Отправить личное сообщение для voraa Посмотреть профиль Найти все сообщения от voraa
 
Регистрация: 03.02.2020
Сообщений: 2,704

Как то так попробуйте
<body>
 
 <table id='div'>
 
 
   <tr>
    
   <th scope="col">Nazwę</th>
   <th scope="col" >Opis</th>
   <th scope="col">&nbsp;</th>
         
         
   </tr>
         
   <tr>
 
     <td>1234</td>
 
     <td>1234</td>
      
     <td>1234</td>
 
   </tr>
 
   <tr>
 
     <td>1234</td>
 
     <td>1234</td>
      
     <td>1234</td>
 
   </tr>
 
   <tr>
 
     <td>1234</td>
 
     <td>1234</td>
      
     <td>1234</td>
 
   </tr>
 
 </table>
  
<script>
let div = document.getElementById('div');
div.addEventListener('click', changeValue);
div.addEventListener('focusout', changeEnd);
 
function changeValue(event){
  event.target.setAttribute('contenteditable', true);
}
 
function changeEnd(event){
  event.target.removeAttribute('contenteditable');
  console.log(event.target.innerText)
  setValues ()
}
 
function setValues () {
	const tbd = document.getElementById('div').tBodies[0];
	const rws = tbd.rows;
	const v = [];
	for (let i=0; i<rws.length; i++) {
		const cls= rws[i].cells;
		const cv = []
		for (let j=0; j<cls.length; j++) {
			cv.push(cls[j].textContent);
		}
		v.push(cv);
	}
	localStorage.setItem('tblvalues',JSON.stringify(v)); 
	console.log(localStorage.getItem('tblvalues'))
}   

function restoreValues () {
	const tbd = document.getElementById('div').tBodies[0];
	const rws = tbd.rows;
	let tbv = localStorage.getItem('tblvalues')
	if (!tbv) return
	const v = JSON.parse(tbv);
	for (let i=0; i<rws.length; i++) {
		const cls= rws[i].cells;
		for (let j=0; j<cls.length; j++) {
			cls[j].textContent = v[i][j];
		}
	}
}   
restoreValues ()
</script>
</body>

Последний раз редактировалось voraa, 10.02.2021 в 22:10.
Ответить с цитированием