将模态绑定到数据表中的行单击

时间:2015-08-05 11:21:33

标签: html datatable bootstrap-modal

直升机, 我有一个数据表,其中包含我的数据库中的数据。在每一行都有一个细节按钮。我希望点击按钮打开一个模式,其中包含所单击行的详细信息。 我试图在页面加载的每一行创建模态实例,但它正在减慢网站的速度。 有没有办法根据行ID填充模态?

这是我的表

   <table id="tblMessagesDetail" class="table table table-hover table-striped  table-condensed export-table" style="width: 100%;">
<thead>
    <tr>
        <th>
            @*@Html.DisplayNameFor(model => model.SmsList[0].SmsMT.Id)*@
            @ViewRes.SharedStrings.SmsID
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.SmsList[0].SmsMT.Account.Project.Customer.Name)*@
            @ViewRes.SharedStrings.CustomerName
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.SmsList[0].SmsMT.Account.Project.Name)*@
            @ViewRes.SharedStrings.ProjectName
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.SmsList[0].SmsMT.Account.Login)*@
            @ViewRes.SharedStrings.Account
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SmsList[0].MSISDN)
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.SmsList[0].SmsMT.SmsStatus.Value)*@
            @ViewRes.SharedStrings.smsStatus
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SmsList[0].SmsMT.EventReason)
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.SmsList[0].SendDate)*@
            @ViewRes.SharedStrings.SendDate
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.SmsList[0].SmsMT.SentDate)*@
            @ViewRes.SharedStrings.SentDate
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SmsList[0].SmsMT.Ticket)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SmsList[0].ExtId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SmsList[0].SmsMTContent.NotificationLevel)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SmsList[0].SmsMTContent.OADC)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SmsList[0].SmsMTContent.Message)
        </th>
        <th>

        </th>
    </tr>
</thead>
<tbody>    
   @foreach (var item in Model.SmsList)
    {
        <tr>

            <td>
                @Html.DisplayFor(modelItem => item.SmsMT.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SmsMT.Account.Project.Customer.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SmsMT.Account.Project.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SmsMT.Account.NMGLogin)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.MSISDN)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SmsMT.SmsStatus.Value)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SmsMT.EventReason)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SendDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SmsMT.SentDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SmsMT.Ticket)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ExtId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SmsMTContent.NotificationLevel)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SmsMTContent.OADC)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SmsMTContent.Message)
            </td>
            <!-- Button trigger modal -->
            <td>
                @*@using (Html.BeginForm("FillDetailModal", "Statistics", FormMethod.Post))
                    {*@
                <button id="btnDetails" type="submit" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal-@item.SmsMTId">
                    <i class="glyphicon glyphicon-th-list"></i> @ViewRes.SharedStrings.btnDetails
                </button>
            </td>
         </tr>
     }
 </tbody>

这是我的模态

<div class="modal fade" draggable="true" id="myModal-@item.SmsMTId" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div id="modalDialog" draggable="true" class="modal-dialog" style="padding-bottom:0px">
                        <div class="modal-content">
                            <div class="modal-header" style="background-color: #428bca; color: white; ">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                                    &times;
                                </button>
                                <h4 class="modal-title" id="myModalLabel">
                                    Message Detail ID: '@item.SmsMTId' - Ticket #@item.SmsMT.Ticket - Project @item.SmsMT.Account.Project.Name
                                </h4>
                            </div>
                            <div class="modal-body" style="padding: 0px;">
                                <table style="width:100%;" class="table table table-hover table-striped  table-condensed export-table" border="0">
                                    <tr>
                                        <td align="left">

                                            <label>@ViewRes.SharedStrings.SmsID</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMT.Id)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">

                                            <label>@ViewRes.SharedStrings.CustomerName</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMT.Account.Project.Customer.Name)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">

                                            <label> @ViewRes.SharedStrings.ProjectName</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMT.Account.Project.Name)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <label>@ViewRes.SharedStrings.Account</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMT.Account.NMGLogin)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <label> @Html.DisplayNameFor(model => model.SmsList[0].MSISDN)</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.MSISDN)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <label>@ViewRes.SharedStrings.smsStatus</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMT.SmsStatus.Value)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <label>@ViewRes.SharedStrings.EventReason</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMT.EventReason)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <label>@ViewRes.SharedStrings.SendDate</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SendDate)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <label>@ViewRes.SharedStrings.SentDate</label>

                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMT.SentDate)

                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <label>@ViewRes.SharedStrings.TicketId</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMT.Ticket)
                                        </td>
                                    </tr>

                                    <tr>
                                        <td align="left">
                                            <label>@ViewRes.SharedStrings.ExternalID</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.ExtId)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <label style="width:160px;">@ViewRes.SharedStrings.NotificationLevel</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMTContent.NotificationLevel)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <label>  @Html.DisplayNameFor(model => model.SmsList[0].SmsMTContent.OADC)</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMTContent.OADC)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <label>@Html.DisplayNameFor(model => model.SmsList[0].SmsMTContent.Message)</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMTContent.Message)
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">
                                            <label> @ViewRes.SharedStrings.binaryMessage</label>
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.SmsMTContent.Binary)
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="modal-footer" style="margin: 0px; padding: 9px 30px 10px;">
                                <button type="button" class="btn btn-default" data-dismiss="modal">
                                    @ViewRes.SharedStrings.btnClose
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

如何传递行的值,然后在数据库上执行链接以提取相应的数据?

我感谢能得到的任何帮助

2 个答案:

答案 0 :(得分:1)

您实际上可以将您的ID绑定到 data-whatever 属性,并将其放在表格行或甚至列中。

#include <windows.h>


const WCHAR g_szChildClassName[] = L"Childclass////";
HINSTANCE g_hInst;


LRESULT CALLBACK ChildProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static BOOL bMouseTracking = FALSE;

    switch(msg)
    {
    case WM_PAINT:
        {
            HDC hdc;
            PAINTSTRUCT ps;
            hdc = BeginPaint(hwnd, &ps);
            if(hdc)
            {
                HBRUSH hbr = CreateSolidBrush(bMouseTracking?RGB(255, 0, 0):RGB(0, 0, 255));
                if(hbr)
                {
                    FillRect(hdc, &ps.rcPaint, hbr);
                    DeleteObject(hbr);
                }
                EndPaint(hwnd, &ps);
            }
        }
        break;

    case WM_MOUSEMOVE:
        if(!bMouseTracking) 
        {
            TRACKMOUSEEVENT tme = { 0 };
            tme.cbSize    = sizeof(TRACKMOUSEEVENT);
            tme.dwFlags   = TME_LEAVE;
            tme.hwndTrack = hwnd;
            bMouseTracking = TrackMouseEvent(&tme);
            InvalidateRect(hwnd, 0, TRUE);
        }
        break;

    case WM_MOUSELEAVE:
        bMouseTracking = FALSE;
        InvalidateRect(hwnd, 0, TRUE);
        break;

    case WM_ENABLE:
        if(wParam) 
        {
            RECT rc;
            if(GetWindowRect(hwnd, &rc))
            {
                POINT pt;
                if(GetCursorPos(&pt))
                    if(PtInRect(&rc, pt)) 
                    {
                        TRACKMOUSEEVENT tme = { 0 };
                        tme.cbSize    = sizeof(TRACKMOUSEEVENT);
                        tme.dwFlags   = TME_LEAVE;
                        tme.hwndTrack = hwnd;
                        //TrackMouseEvent() posts WM_MOUSELEAVE if conditions 1 and 2 are met, even though I'm trying to turn 
                        //mouse tracking on and the cursor is over the child window. It doesn't make sense
                        //The problems is this piece of code right here /* bMouseTracking = TrackMouseEvent(&tme); */
                        //It should turn tracking on but it doesn't it cancels it even though WS_DISABLED has already been removed
                        //at this point
                        bMouseTracking = TrackMouseEvent(&tme); 
                        InvalidateRect(hwnd, 0, TRUE);
                    }
            }
        } else {
            if(bMouseTracking) { 
                ////////If you comment everything from here ...
                TRACKMOUSEEVENT tme = { 0 };
                tme.cbSize    = sizeof(TRACKMOUSEEVENT);
                tme.dwFlags   = TME_LEAVE | TME_CANCEL;
                tme.hwndTrack = hwnd;
                //if(TrackMouseEvent(&tme)) PostMessage(hwnd, WM_MOUSELEAVE, 0, 0); //Commented this line out to do things a bit differently with the same results  
                if(TrackMouseEvent(&tme)) { //If this succeeds it means mouse tracking was canceled
                    bMouseTracking = FALSE;
                    InvalidateRect(hwnd, 0, TRUE);
                }
                ////////all the way down to here the result is the same
                //If you comment everything in this block out then you have another problem which can be tested with this condition:
                //With window enabled move mouse over window, then press the ENTER key. The color should change 
                //from red to blue but it doesn't. It will change to blue though if you move the mouse 1 or more pixels after you've pressed the ENTER key
            }
        }
        break;

    case WM_DESTROY:
        bMouseTracking = FALSE;
        break;

    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HWND hChild;

    switch(msg)
    {
    case WM_CREATE:
        hChild = CreateWindowEx(0, g_szChildClassName, 0, WS_VISIBLE | WS_CHILD, 4, 4, 240, 80, hwnd, 0, g_hInst, 0);
        break;

    case WM_KEYDOWN:
             if(wParam == VK_SPACE)  EnableWindow(hChild, TRUE);
        else if(wParam == VK_RETURN) EnableWindow(hChild, FALSE);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default: 
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}


int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
    const TCHAR szClassName[] = L"abccccccc";
    WNDCLASSEX wc;
    HWND hwnd;
    MSG msg;

    SecureZeroMemory(&wc, sizeof(WNDCLASSEX));
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    wc.hCursor       = LoadCursor(0, IDC_ARROW);
    wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
    wc.hInstance     = hInstance;
    wc.lpfnWndProc   = WndProc;
    wc.lpszClassName = szClassName;
    if(!RegisterClassEx(&wc)) return 0; //Register main window class

    SecureZeroMemory(&wc, sizeof(WNDCLASSEX));
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.hCursor       = LoadCursor(0, IDC_ARROW);
    wc.hInstance     = hInstance;
    wc.lpfnWndProc   = ChildProc;
    wc.lpszClassName = g_szChildClassName;
    if(!RegisterClassEx(&wc)) return 0; //Register child window class

    g_hInst = hInstance;
    hwnd = CreateWindowEx(0, szClassName, L"Test", WS_OVERLAPPEDWINDOW, 40, 40, 420, 200, 0, 0, hInstance, 0);
    if(!hwnd) return 0;

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg, 0, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

在jQuery中:

    <tr data-toggle="modal" data-target="#myModalIdHere" data-whatever="myDataIdHere">

答案 1 :(得分:0)

使用while循环查询所有数据并将图标放在最后一列中。有了这个,你可以创建一个包含所有数据的表格,图标将为我们提供查看,删除或编辑的按钮

while($row = $sql->fetch()){

                                    $id = $row['id'];
                                    $company = $row['company_name'];
                                    $domain = $row['domain_name'];
                                    $industry = $row['industry_type'];
                                    $websitet = $row['website_type'];
                                    $websitep = $row['website_purpose'];
                                    $audience = $row['target_audience'];
                                    $web_pages = $row['web_pages'];
                                    $color = $row['color_scheme'];
                                    $style = $row['style'];
                                    $addons = $row['addons'];
                                    $url = $row['url_of_existing'];
                                    $like_dislike = $row['like_dislike_existing_website'];
                                    $sample = $row['sample_website'];


                                    echo '<tr><td>'.$id.'</td>';
                                    echo '<td>'.$company.'</td>';
                                    echo '<td>'.$domain.'</td>';
                                    echo '<td>'.$industry.'</td>';
                                    echo '<td><a href="#openModal'.$id.'" data-toggle="modalDialog" data-target="#openModal"><img src=img/view.png width=20px height=20px id="view"></a></td>';
                                    echo '<td><a href="delete.php?id='.$id.'" onclick="return confirm(\'Delete '.$id.'?\')"><img src="img/delete.png" width=20px height=20px name="delete"></a></td></tr>';
                                    ?> 

<a href...?id='.$id.'"部分是使用get方法发送id ..如果你不了解它,请查阅它。这几乎可以解决你的问题。