Change column of txt

Asked

Viewed 191 times

0

I need to change the content of a column with javascript, so I don’t need to use php with apache. Since it would run with html and javascript.

It would be a file with 10 columns and only modify the 5th column and only the cell that had blank.

1|1|1|1| |1|1|1|1|1|

The file would be set by the form and change the 5th column that is empty with contents of a form text field.

And do this for the next lines of the same text.

  • Also publish the relevant code, and better explain what you want sff

  • @Miguel I changed the description of the question.

  • If you want to touch files on the client’s machine or on the server with javascript only, you can forget it. No

  • @Miguel In the client, it would only be executed by html, without apache.

  • But you can’t read, write, change the file. "What I need and only a code to read the file, change and write"

  • I don’t know what you mean.

  • Corrected, I made a typo

  • @Miguel I’ll have to go pro php even then. Have any idea?

Show 3 more comments

1 answer

1


What you can do in Javascript is a method that changes the content:

var content = '1|1|1|1| |1|1|1|1|1|';

 alterContent(content, '123');
 function alterContent(content, val) {
 var add = [];
    content.split('|').forEach(function(v, e){
      add[e] = (e == 4 && (v == ' '|| v == '')) ? val : v;
    });
    return add.join('|');
 }

If you need to do this for each line, just pass the method inside a loop and test each line:

var content =  "1|1|1|1| |1|1|1|1|1|\n1|1|1|1| |1|1|1|1|1|\n1|1|1|1|1|1|1|1|1|1|";
var linhas = content.split("\n");

/* passa a posição 2, se usar parâmetro: null,
   ignora a regra de linha, e testa todas as linhas */
alterarLinhas(linhas, '123', 2);

function alterarLinhas(linhas, val, posicao_linha) {
    for (var i=0; i <= linhas.length; i++) {
        if (posicao_linha != null) {
           if (posicao_linha == i) {
              linhas[i] = alterContent(linhas[i], val);
           } else {
              linhas[i] = val;
           }
        } else {
           linhas[i] = alterContent(linhas[i], val);
        }
    }
return linhas.join("\n");
}

function alterContent(content, val) {
    var add = [];
    content.split('|').forEach(function(v, e){
          add[e] = (e == 4 && (v == ' '|| v == '')) ? val : v;
    });
    return add.join('|');
}

1st JSFIDDLE example
2nd example JSFIDDLE

Reading, changing and recording: see this.

  • Interesting. More how do I make it only in the fifth column replace blank values? in the case there it will replace all lines by the value.

  • And in that case it would change line by line individually if I create a text box and copy the code to the form text box??

  • I made a modification, to do this only when it is blank.

  • I’m using the code this way to alter data coming from the form and the character I want to fill in the blank. The problem now that it only changes in the first line since the text will have lines breaking. I’ve already made several changes plus all without success or partial errors.

  • document.getElementById("FormAlterar").onclick = function(e) {&#xA; var content_branco = document.getElementById("campo").value;&#xA; var dado = document.getElementById("dado").value;&#xA;&#xA;&#xA; document.getElementById('retorno').innerHTML = alterContent(content_branco, dado);&#xA; function alterContent(content, val) {&#xA; var add = [];&#xA; content.split('|').forEach(function(v, e){&#xA; add[e] = (e == 4 && (v == ' ' || v == '')) ? val : v;&#xA; });&#xA; return add.join('|');&#xA; }&#xA; }

  • You did not specify this in the question, but you can make a separate method that separates line breaks " n" or "<br>" using .split(), you better edit your question and be clearer and objective so that we can solve your problem at once.

  • already specified. I must use the break when reading the line, or after changing it?

  • before processing the exchange method, you must find the line that will process the exchange, correct? So, you need to know which line you are going to modify, so you need to have some information that will take you there. In this case, is it empty space? Or do you have a specific line position? I need to know this information, so create the method that will validate the correct line.

  • It would be '1|1|1|1| |1|1|' break '1|1|1| |1|1|'

  • Look at the answer.

  • Line position since I will use areatext in the form.

  • textarea, is line break " n".

  • line position is only to inform the array line, instead of passing inside the loop: function alterarLinhas(linhas, val) {&#xA; for (var i=0; i <= linhas.length; i++) {&#xA; if (i == posicao_linha) {&#xA; linhas[i] = alterContent(linhas[i], val);&#xA; } else {&#xA; linhas[i] = val; &#xA; }&#xA; }&#xA;return linhas.join("\n");&#xA;}

  • It worked in the vein. I made the changes to accept the form data and good. Thank you.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.