我有一個DataTable 用於顯示從SQL表加載信息。在每行的最後一個單元格中,我有一個jQuery datepicker輸入。在第一頁上,每行的日期選擇器工作得很好。以及我的jQuery函數在提交之前檢查空白字段。問題是,在任何其他頁面上,我有字段檢查器,更重要的是,日期選擇器根本不起作用。我查看了https://datatables.net/faqs/index以瞭解如何正確初始化我的表格,但在嘗試給出示例後仍然沒有運氣。任何建議都會被處理。jQuery只適用於DataTable的第一頁
創建時,每個日期選擇器都被分配了類'datepicker'。我使用這個類作爲我的jQuery腳本中輸入的選擇器。下面是我的ASP.NET MVC視圖頁我有代碼:
@using WebMatrix.Data
@using System.Data
@using System.Data.SqlClient
@using System.Data.OleDb
@using System.Configuration
@using System.Web.UI.WebControls
@using bp_open_issues.Models
@model bp_open_issues.Models.HomeView
@{
ViewBag.Title = "BullPen Open Issues - Edit";
}
@{
if (null != TempData["msg"])
{
if ("Added" == TempData["msg"])
{
<script type="text/javascript">
alert('Record succesfully added.');
</script>
}
else if ("Updated" == TempData["msg"])
{
<script type="text/javascript">
alert('Record closed.');
</script>
}
}
<link rel="stylesheet" type="text/css" href="~/Content/css" />
<script src="~/Scripts/jquery.dataTables.min.js"></script>
<section class=" featured">
<div class="content-wrapper">
<h3 style="display: inline">Zone: </h3>
@Html.DropDownListFor(m => m.SZView.ZoneSet, Model.SZView.ZoneSet.ToList(), new { id = "zoneSelect" })
<br /><h3 style="display: inline">Station: </h3>
@Html.DropDownListFor(m => m.SZView.LineSet, Model.SZView.LineSet.ToList(), new { id = "lineSelect" })
<center><h1 style="display:inline">BULLPEN OPEN ISSUES</h1></center>
</div>
</section>
}
<h3>Current Issues:</h3><br />
<div class="datagrid">
<table id="reviewTable">
<thead>
<tr>
<th>ZONE<br />area</th>
<th>STATION<br />resource</th>
<th>WHEN<br />opened</th>
<th>WHAT<br />is the concern</th>
<th>WHY<br />do we have</th>
<th>HOW<br />do we fix</th>
<th>WHO<br />is responsible</th>
<th>WHEN<br />is it fixed</th>
</tr>
</thead>
<tfoot>
<tr></tr>
</tfoot>
<tbody>
@foreach (Issue issue in Model.IssueSet.IssueList)
{
<tr class="altsec" id="@issue.RowID">
<td>@issue.Zone.ToString()</td>
<td>@issue.Station.ToString()</td>
<td>@issue.WhenOpened.Date.ToShortDateString()</td>
<td>@issue.What.ToString()</td>
<td>@issue.Why.ToString()</td>
<td>@issue.How.ToString()</td>
<td>@issue.Who.ToString()</td>
<td>
@using (Html.BeginForm())
{
<fieldset><input type="text" style="width: 100px; display: none" value="@issue.ID" name="stringID" /><input class="datepicker" type='text' style="width: 100px" name="stringDate" id="@issue.DateID" /><input class="updateButtons" type="submit" style="float:right; padding: 2px 8px; margin: 1px;color: #ff0000;border: 1px solid #000;-webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; background:#000;background-color:#000;" value="Update" /></fieldset>
}
</td>
</tr>
}
</tbody>
</table>
</div>
<script>
$(document).ready(function() {
$('#reviewTable').DataTable();
$('tr:even').css('background-color', '#EBEBEB');
$('tr:odd').css('background-color', '#FFF');
$('.datepicker').datepicker();
$('#selectFilter').change(function() {
alert('zone was changed.');
$(".all").hide();
$("." + $(this).find(":selected").attr("id")).show();
});
$('#zoneSelect').change(function() {
$('#lineSelect').val('ALL');
$(".altsec").hide();
var $this = $(this);
var zoneVal = $this.find(":selected").attr("value");
if (zoneVal != "ALL") {
$('tr:has(td:contains("' + zoneVal + '"))').each(function() {
$(this).show();
});
}
else {
$(".altsec").show();
}
});
$('#lineSelect').change(function() {
$('#zoneSelect').val('ALL');
$(".altsec").hide();
var $this = $(this);
var lineVal = $this.find(":selected").attr("value");
if (lineVal != "ALL") {
$('tr:has(td:contains("' + lineVal + '"))').each(function() {
$(this).show();
});
}
else {
$(".altsec").show();
}
});
$(".updateButtons").click(function (event) {
var blankField = false;
var dateVal = $(this).prev().val();
if (dateVal == 0 || dateVal == null) {
event.preventDefault();
alert("Please select a valid date.");
}
});
(function() {
var oldVal;
$('#searchBar').on('change textInput input', function() {
var val = this.value;
if (val !== oldVal) {
oldVal = val;
if ($('#searchBar').text == "") {
$(".altsec").hide();
var zoneVal = $('#zoneSelect option:selected').text();
var lineVal = $('#lineSelect option:selected').text();
if (zoneVal == "ALL" && lineVal == "ALL") {
$(".altsec").show();
}
}
}
});
}());
});
嘿@ Shaunak,感謝您的答覆。我嘗試使用DataTable的'page.dt'事件處理程序,但在文檔中聲明 - 在重繪表之前觸發此事件。所以當我嘗試使用''('#reviewTable')。on('page.dt',function(){ alert('page changed'); $('tr:even').css(' ('。'); $('tr:odd')。css('background-color','#FFF'); $('。datepicker')。datepicker(); } );」所有這些代碼都是在表格中的元素甚至可以被正確引用之前完成的。 –
啊好吧。我只是用這種方法更新了我的答案:(所以然後從這個事件中嘗試將'datepicker'封裝在一個dom中,就像''(function(){$('。datepicker')。datepicker(); ('page.dt',function(){ $(document).ready(function(){ alert(「}」)'看看是否有幫助 – Shaunak
這樣?: $('#reviewTable')。改變....頁!「); $('tr:odd')。('tr:odd')。css('background-color','#EBEBEB'); $ ';'#FFF'); $('。datepicker')。datepicker(); }) }); }); –