如何从drupal pager进行ajax调用
我对drupal和使用drupal6不熟悉。。我有一个模块,它根据输入字段从数据库中获取一组员工详细信息如何从drupal pager进行ajax调用,ajax,drupal,pager,Ajax,Drupal,Pager,我对drupal和使用drupal6不熟悉。。我有一个模块,它根据输入字段从数据库中获取一组员工详细信息 xmlHttpReq.open('GET', "/empfinder.json&dept=" + dept + "&gender=" + gen+ "&age=" + age, true); 当我尝试使用寻呼机时,它会直接拨打电话,如下所示,并显示在新页面中 http://156.23.12.14/empfinder.json?page=1&dept=AC
xmlHttpReq.open('GET', "/empfinder.json&dept=" + dept + "&gender=" + gen+ "&age=" + age, true);
当我尝试使用寻呼机时,它会直接拨打电话,如下所示,并显示在新页面中
http://156.23.12.14/empfinder.json?page=1&dept=ACC&gender=Male&age=34
我需要在同一页中显示结果。如何修改寻呼机调用来实现这一点?在执行AJAX请求时,您应该使用jquery实用程序函数,而不是“自己”执行请求,从而使您的生活更加轻松。jquery库包含在Drupal核心中(至少对于Drupal 6)。至于文档,您可以从开始。我写了一篇关于这个主题的博客,并将其粘贴到下面 JS与AJAX和PHP Drupal对JS和AJAX的广泛支持是其标准表单的一部分,并且有教程解释了这是如何工作的。然而,我找不到一个好的教程来解释Javascript如何以一种特别的方式与Drupal模块通信。例如,我希望能够根据PHP中可用的状态信息修改任意html。下面介绍这项技术 您将在本页顶部看到一些选项卡,默认情况下,这些选项卡在本主题中非常简单。我想修改它们,使当前选择的选项卡更加突出。当然,这可以用CSS来完成,但我想开发这种技术,用于CSS本身还不够的情况 下面是可以直接添加到前面描述的JS文件中的JS。有一个jQuery函数,它在每次加载页面并准备就绪时对id为“main menu links”的元素进行操作。我获取innerHTML并使用encodeURIComponent将其转换为可以作为URL参数传递的安全字符串。我必须这样做,因为其中一个选项卡引用了传递参数的URL
var xmlhttp;
var childnodes;
// Send post to specified url
function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=cfunc;
// alert("loadXMLDoc: " + url);
xmlhttp.open("POST",url,true);
xmlhttp.send();
}
// AJAX redirect to camr_custom/getvisits with callback function to replace the href
// with something to disable the link for nodes that have not been visited.
function getMenuTabs(str)
{
loadXMLDoc("?q=flashum_status/get_menu_tabs&block="+str,function()
{
// alert("getMenuTabs status: " + xmlhttp.status + " readyState: " + xmlhttp.readyState);
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// alert("getMenuTabs: " + xmlhttp.responseText);
data= jQuery.parseJSON('['+xmlhttp.responseText+']')
$.each(data,function(){
// alert(this['block']);
document.getElementById("main-menu-links").innerHTML = this['block'];
});
}
});
}
// Locate book navigation block and send innerHTML to PHP module
$('#main-menu-links').ready(function() {
lis = document.getElementById("main-menu-links").innerHTML;
// alert("main-menu-links: " + lis);
// safe encode this block so that it can contain arbitrary urls in the href links
lis = encodeURIComponent(lis);
getMenuTabs(lis);
});
jQuery函数最终调用loadXMLDoc,这是AJAX发布的地方,指定Drupal模块中hook_菜单捕获的URL。它还使用在参数cfunc中传递的回调函数。返回时,将解析JSON响应以将其转换为HTML,并将其直接存储回原始的innerHTML。因此,PHP模块对HTML所做的任何操作都将替换原始HTML
在PHP端,首先是hook_菜单的数组元素:
$items['flashum_status/get_menu_tabs'] = array(
'page callback' => 'get_menu_tabs',
'access arguments' => array('access flashum status'),
'type' => MENU_CALLBACK,
);
回调函数如下所示。它首先提取块参数并将其加载到DOM对象中,以便对其进行解析。simple_html_dom对象由simplehtmldom模块提供,您需要安装并启用该模块。不要忘记安装相关的库。这应该在/all/libraries/simplehtmldom/simple\u html\u dom.php中结束
function get_menu_tabs() {
// drupal_set_message(t("get_menu_tabs: @code", array('@code' => print_r(null, TRUE))));
if (array_key_exists ('block', $_GET)) {
$block = $_GET['block'];
// drupal_set_message(t("get_menu_tabs block: @code", array('@code' => print_r($block, TRUE))));
// Create a DOM object.
$html_obj = new simple_html_dom();
// Load HTML from a string.
$html_obj->load($block);
// remove href for nodes not yet visited
$index = 0;
foreach ($html_obj->find('li') as $li ) {
$start = strpos($li->innertext, 'href');
$end = strpos($li->innertext, '>', $start);
$start_html = substr($li->innertext, 0, $end);
$end_html = substr($li->innertext, $end);
if (strpos($li->innertext, 'active')) {
$li->innertext = $start_html.' style="color:red;border: solid red;margin-left:5px;margin-right:5px;"'.$end_html;
// drupal_set_message(t("get_menu_tabs html_obj: @code", array('@code' => print_r($li->innertext, TRUE))));
}
else
$li->innertext = $start_html.' style="color:black;border: solid #777;"'.$end_html;
$index++;
}
$str = $html_obj->save();
// drupal_set_message(t("get_menu_tabs str: @code", array('@code' => print_r($str, TRUE))));
// Release resources to avoid memory leak in some versions.
$html_obj->clear();
unset($html_obj);
return drupal_json_output(array('block'=>$str));
}
}
最后,它通过li项循环添加内联CSS样式,该样式根据选项卡是否处于活动状态而变化。然后它只是从DOM对象创建一个字符串,并通过drupal_json_输出将其返回,并将其转换为json格式。这当然是在JS回调函数中接收到的