Передача переменной из ejs в скрипт js - PullRequest
0 голосов
/ 20 января 2019

Я пытаюсь передать переменную из ejs в функцию JavaScript, но получаю неопределенным в выводе. Вот код Предполагается визуализировать таблицу с именами файлов, и нажатие на каждое имя файла позже должно перенаправить на страницу, которая отобразит файл в браузере, но я не могу продолжить, не передав параметр в URL.

        <% if (files.length > 0) {%>
        <table class="table table-hovered">
            <thead class="thead-light">
                <tr>
                    <th scope="col">No</th>
                    <th scope="col">File</th>
                    <% files.forEach((file, index) => { %>
                <tr>
                    <th scope="row">
                        <%= index + 1 %>
                    </th>
                    <td>
                        <a onclick="func(file.fileName);">
                            <%= file.fileName %>
                        </a>
                    </td>
                </tr>
                <% }) %>
                </tbody>
        </table>
        <% } %>
    </div>
</div>
<script>
    function func(fileName) {
        window.location.href = "/thesis_view/" + fileName;
    }
</script>




getStudentUploadThesisPage: (req, res) => {
        const getFilesQuery = "SELECT fileName FROM supervision INNER JOIN thesis ON supervision.thesisId = thesis.id INNER JOIN thesis_details ON thesis_details.thesisId = thesis.Id INNER JOIN people ON supervision.teacherId = people.id INNER JOIN thesis_file ON thesis_details.id = thesis_file.thesis_detail_id WHERE supervision.studId = " + req.session.userId;
        db.query(getFilesQuery, (err, result) => {
            if (err) {
                return res.status(500).send(err);
            } else if (result.length >= 0) {
                res.render('student_upload_thesis', {
                    files: result
                });
            }
        });
    }

1 Ответ

0 голосов
/ 20 января 2019

Я придумал что-то вроде этого.Это делает свою работу

<script>
    var table = document.getElementById('table'),
        rIndex;
    for (var i = 0; i < table.rows.length; i++) {
        table.rows[i].onclick = function () {
            rIndex = this.rowIndex;
            window.location.href = "/thesis_view/" + this.cells[1].innerHTML;
        }
    }
</script>
...