admin管理员组文章数量:1389762
I have a function that waits for a user to click on an input of type text and then waits for the user to press the down and up arrow keys. When the user presses the down and up arrow keys the function hoverDown() is called which essentially programmatically hovers over tables rows. When enter is pressed, window.location is called and the appropriate page is loaded. The problem is that I'm using pjax throughout my application to dynamically load the content and push the new url without a page refresh. When I call the window.location function, the pjax no longer works, but instead, the correct url is loaded and a full page refresh occurs - the very thing I'm trying to avoid. Is there any way to programmatically invoke and execute pjax from within my function? Relevent Code:
//HTML
<div id="main">
<input type="text" class="table-search" id="search" autoplete="off" placeholder="Search Clients..." /><table class="table" id="tblData">
<thead><tr><th>Name</th> <th>Title</th></tr></thead>
<tbody id="tblDataBody">
<tr><td><a href="/cases">Scott</a></td> <td>Client</td></tr>
<tr><td><a href="/cases">Billy</a></td><td>Client</td></tr>
<tr><td><a href="/cases">George</a></td><td>Client</td></tr>
<tr><td><a href="/cases">Sara</a></td><td>Client</td></tr>
<tr><td><a href="/cases">John</a></td><td>Client</td></tr>
<tr><td><a href="/cases">Megan</a></td><td>Client</td></tr>
<tr><td><a href="/cases">Ben</a></td><td>Client</td></tr>
<tr><td><a href="/cases">Jully</a></td><td>Client</td></tr>
<tr><td><a href="/cases">Bethany</a></td><td>Client</td></tr>
<tr><td><a href="/cases">Alen</a></td><td>Client</td></tr>
<tr><td><a href="/cases">Jane</a></td><td>Client</td></tr>
<tr><td><a href="/cases">Alice</a></td><td>Client</td></tr></tbody></table>
</div>
//Javascript
$(document).ready(function(){
$("#main").on("keydown", "#search", hoverDown);
});
function hoverDown(e) {
var $tbl = $('#tblDataBody');
var $cur = $('.active', $tbl).removeClass('active').first();
if (e.keyCode === 40) { //down
if ($cur.length) {
$cur.next().addClass('active');
} else {
$tbl.children().first().addClass('active');
}
} else if (e.keyCode == 38) { //up
if ($cur.length) {
$cur.prev().addClass('active');
} else {
$tbl.children().last().addClass('active');
}
} else if (e.keyCode == 13) {
if ($cur.length) {
window.location = $cur.find("a").attr("href");
}
}
}
//For the sake of pleteness, CSS:
.active {
background-color: yellow;
}
If you want to see this code in action to get a better understanding, you can check out this jsFiddle.
And again, I'm trying to use pjax to load the content.
I have a function that waits for a user to click on an input of type text and then waits for the user to press the down and up arrow keys. When the user presses the down and up arrow keys the function hoverDown() is called which essentially programmatically hovers over tables rows. When enter is pressed, window.location is called and the appropriate page is loaded. The problem is that I'm using pjax throughout my application to dynamically load the content and push the new url without a page refresh. When I call the window.location function, the pjax no longer works, but instead, the correct url is loaded and a full page refresh occurs - the very thing I'm trying to avoid. Is there any way to programmatically invoke and execute pjax from within my function? Relevent Code:
//HTML
<div id="main">
<input type="text" class="table-search" id="search" autoplete="off" placeholder="Search Clients..." /><table class="table" id="tblData">
<thead><tr><th>Name</th> <th>Title</th></tr></thead>
<tbody id="tblDataBody">
<tr><td><a href="http://lar.loc/cases">Scott</a></td> <td>Client</td></tr>
<tr><td><a href="http://lar.loc/cases">Billy</a></td><td>Client</td></tr>
<tr><td><a href="http://lar.loc/cases">George</a></td><td>Client</td></tr>
<tr><td><a href="http://lar.loc/cases">Sara</a></td><td>Client</td></tr>
<tr><td><a href="http://lar.loc/cases">John</a></td><td>Client</td></tr>
<tr><td><a href="http://lar.loc/cases">Megan</a></td><td>Client</td></tr>
<tr><td><a href="http://lar.loc/cases">Ben</a></td><td>Client</td></tr>
<tr><td><a href="http://lar.loc/cases">Jully</a></td><td>Client</td></tr>
<tr><td><a href="http://lar.loc/cases">Bethany</a></td><td>Client</td></tr>
<tr><td><a href="http://lar.loc/cases">Alen</a></td><td>Client</td></tr>
<tr><td><a href="http://lar.loc/cases">Jane</a></td><td>Client</td></tr>
<tr><td><a href="http://lar.loc/cases">Alice</a></td><td>Client</td></tr></tbody></table>
</div>
//Javascript
$(document).ready(function(){
$("#main").on("keydown", "#search", hoverDown);
});
function hoverDown(e) {
var $tbl = $('#tblDataBody');
var $cur = $('.active', $tbl).removeClass('active').first();
if (e.keyCode === 40) { //down
if ($cur.length) {
$cur.next().addClass('active');
} else {
$tbl.children().first().addClass('active');
}
} else if (e.keyCode == 38) { //up
if ($cur.length) {
$cur.prev().addClass('active');
} else {
$tbl.children().last().addClass('active');
}
} else if (e.keyCode == 13) {
if ($cur.length) {
window.location = $cur.find("a").attr("href");
}
}
}
//For the sake of pleteness, CSS:
.active {
background-color: yellow;
}
If you want to see this code in action to get a better understanding, you can check out this jsFiddle.
And again, I'm trying to use pjax to load the content.
Share Improve this question asked Feb 18, 2013 at 13:31 ScottScott 1,3425 gold badges25 silver badges37 bronze badges3 Answers
Reset to default 7I don't know how I missed this in the documentation the first time around, but I found a simple way to manually invoke pjax.
} else if (e.keyCode == 13) {
if ($cur.length) {
// manually invoke pjax after enter has been pressed
var $url = $cur.find("a").attr("href");
$.pjax({url: $url, container: '#main'});
}
}
After reading over the doucmentation you linked, I believe you should be using the $.pjax.click
function instead of assigning your URL to window.location
:
var container = $(this).closest('[data-pjax-container]')
$.pjax.click(event, {container: container})
Have a look at Djax : Dynamic Pjax
https://github./beezee/djax
本文标签:
版权声明:本文标题:jquery - Is there any way to programmatically call and execute pjax from within my javascript file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744575529a2613601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论