0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

残プロ 第-32回 ~HTMLの子要素を取得する~

Last updated at Posted at 2021-07-03

for文を意識したHTML

task.csv

task_csv.png

.html

前々回でjinja2によるテンプレートfor文を利用しました.
id指定の際,個別にするのでなくテンプレートを使用することで利便性が高まります.

<tbody>
    {% for task in tasks_csv[1:] %}
    <tr id={{ task[0] }}>
        <th scope='row'>{{ task[1] }}</th>
        <td>{{ task[2] }}</td>
        <td>{{ task[3] }}</td>
        <td>{{ task[4] }}</td>
        <td>{{ task[5] }}</td>
        <td>
            <div class="form-group">
                <select class="form-control" onchange="replaceForm();">
                    <option value="">-</option>
                    <option value="Edit">Edit</option>
                    <option value="Delete">Delete</option>
                </select>
            </div>
        </td>
    </tr>
    {% endfor %}
</tbody>

<script>
    let row;
    let select;
    let col_1;
    let tasks = {{ tasks_csv|tojson }}
    let len = tasks.length

    function replaceForm(){
        for (let i=1; i<len; i++){
            row = document.getElementById(tasks[i][0]);
            console.log(row.children)
        }
    }
</script>

ここで注意なのですが,JavaScriptの関数内でjinja2のテンプレートは使用できません.新たに配列を用意しjson化したのち,代入することで利用できるようになります.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?