简体   繁体   English

单击 JQuery DataTable 单元格中的 href 链接时显示行字段数据

[英]Show row field data on click on href link in JQuery DataTable cell

I need to show data.response row field in dialog of JQuery DataTable cell.我需要在 JQuery DataTable 单元格的对话框中显示 data.response 行字段。 Cell contains a href link and when in clicked, modal dialog must open to show data.response fiela of that table row.单元格包含一个 href 链接,单击时,必须打开模式对话框以显示该表行的 data.response fiela。 I am confused, I can't understand how to detect row id on click on link in particular cell in order to get index of row to get data.response of the row I need to show id modal dialog.我很困惑,我不明白如何在单击特定单元格的链接时检测行 ID,以便获取行的索引以获取数据。我需要显示 id 模式对话框的行的响应。 The code (name of the column with a href link is statusCodeVal with render: responseRenderer method):代码(带有 href 链接的列的名称是 statusCodeVal,带有 render: responseRenderer 方法):

DataTable数据表

let table = $('#logs-table').DataTable({
            rowsGroup: [
                0,1,2,3,4,5,6
            ],
            "dom": "lptipr",
            "lengthMenu": [2, 10, 30, 100, 500, 1000],
            "pageLength": 10,
            "pagingType": "full",
            "serverSide": true,
            "deferLoading": 0,
            "ajax": {
                "contentType": "application/json",
                "url": "./bitrix-logs/json",
                "type": "post",
                "data": function (d) {
                    addParametersToRequest(d);
                    return JSON.stringify(d);
                }
            },
            "columns": [
                {"data": "dateCall", "name": "dateCall"},
                {"data": "callId", "name": "callId"},
                {"data": "numberA", "name": "numberA"},
                {"data": "numberB", "name": "numberB",},
                {"data": "lineNumber", "name": "lineNumber"},
                {"data": "crmCallType", "name": "crmCallType"},
                {"data": "crmType", "name": "crmType"},
                {"data": "crmRequestType", "name": "crmRequestType"},
                {"data": "data", "name": "statusCodeVal" , render: responseRenderer},
                {"data": "url", "name": "url"}
            ],
            "ordering": false,
            "language": {
                "search": '[(#{common.datatables.search})]',
                "info": '[(#{common.datatables.info})]',
                "infoFiltered": '[(#{common.datatables.infoFiltered})]',
                "infoEmpty": '[(#{common.datatables.infoEmpty})]',
                "emptyTable": '[(#{page.stats.mkn.not.found})]',
                "zeroRecords": '[(#{common.datatables.zeroRecords})]',
                "paginate": {
                    "first": '[(#{common.datatables.first})]',
                    "last": '[(#{common.datatables.last})]',
                    "next": '[(#{common.datatables.next})]',
                    "previous": '[(#{common.datatables.previous})]'
                },
                "lengthMenu": "[(#{common.datatables.lengthMenu})]"
            },
            "drawCallback": function (settings) {
                let api = this.api();
                let customPageInputHtml = '<span class="pg_inner"><input type="text" size="3" class="custom_page_number" ' +
                    'value="' + (api.page.info().page + 1) + '"/> [(#{common.datatables.from})] ' +
                    api.page.info().pages + '</span>';
                $('span.pg_inner').remove();
                $('.dataTables_paginate .previous').after(customPageInputHtml);
                $('.custom_page_number').change(function () {
                    let num = parseInt($(this).val()) - 1;
                    api.page(num).draw(false);
                });
            }

responseRenderer method:响应渲染器方法:

function responseRenderer(data, type, row, meta){

        $('#msg').text(row.response);
        let show = row.statusCode + " " + row.statusCodeVal;
        return `<a href="#" style="display: inline-block; margin-right: 5px;"
                   data-toggle="modal" data-target="#full-message-modal">
                   <span>` + show + `</span>
            </a>`;
    }

Modal dialog模态对话框

<div id="full-message-modal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div id="full-message-container">
                    <p><textarea id="msg" rows="10" cols="80"></textarea></p>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" th:text="#{bitrix.logs.modal.close}" class="btn btn-default" data-dismiss="modal">
                    Close
                </button>
            </div>
        </div>
    </div>
</div>

Found a solution找到了解决方案

function responseRenderer(data, type, row, meta) {
        let show = row.statusCode + " " + row.statusCodeVal;
        let resp = row.response;
        return `<a href="#" style="display: inline-block; margin-right: 5px;"
                   data-toggle="modal" data-target="#full-message-modal" onclick="setMsg('`+ escape(resp) + `')">
                   <span>` + show + `</span>
            </a>`;
    }

    function setMsg(val) {
        $('#msg').text(unescape(val)); // text for modal
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM