如果您想了解Drupal中对Node页面的Ajax修改和node.jsajax的知识,那么本篇文章将是您的不二之选。我们将深入剖析Drupal中对Node页面的Ajax修改的各个方面,并为您解答nod
如果您想了解Drupal 中对Node页面的Ajax修改和node.js ajax的知识,那么本篇文章将是您的不二之选。我们将深入剖析Drupal 中对Node页面的Ajax修改的各个方面,并为您解答node.js ajax的疑在这篇文章中,我们将为您介绍Drupal 中对Node页面的Ajax修改的相关知识,同时也会详细的解释node.js ajax的运用方法,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- Drupal 中对Node页面的Ajax修改(node.js ajax)
- ajax – 在drupal中以编程方式更新{node_counter}表
- ajax – 如何使用Drupal返回实际的JSON?
- ajax修改密码
- Ajax修改的简单示例
Drupal 中对Node页面的Ajax修改(node.js ajax)
项目中有个对新增的机器进行自行编号保证编号的唯一和快速,下面把程序丢出来记录下:
<?PHP /** * @file * Defines "warehouse import" Feeds import. * * @Todo: Split admin functions into their own file. */ /* function node_modfiy_form_alter(&$form,$form_state,$form_id) { if ($form_id == 'fixed_assets_node_form') { drupal_set_message(t('some message.')); dpm($form); dpm($node); } } */ function node_modfiy_form_alter(&$form,&$form_state,$form_id) { if($form_id=='fixed_assets_node_form'){ //dpm($form); //dpm($form_state); if(!isset($form['nid']['#value'])){ $form['title']['#ajax']=array( 'event'=>'click','wrapper'=>'form-item-form-type-textfield-form-item-title','callback'=>'node_modfiy_title_ajax_callback',); $form['title']['#prefix']='<div id=form-item-form-type-textfield-form-item-title>'; $form['title']['#suffix']='</div>'; $form['title']['#validate']=TRUE; //form_set_cache($form['#build_id'],$form,$form_state); $form['#validate'][]='node_modfiy_form_validate'; //dpm($form['#validate']); } } } /** * Implementation of hook_validate(). *验证输入的条码不可以有重复 */ function node_modfiy_form_validate($form,&$form_state){ if(strlen($form_state['values']['title'])<10 ) form_set_error('title','请选择正确的位置信息和机器分类信息'); $myresult=db_query("select n.nid FROM {node} n Where n.title=:title And n.type=:type",array('title'=>$form_state['values']['title'],'type'=>"fixed_assets")); $nid=$myresult->fetchColumn(1); if($nid) form_set_error('title','重复的条码!'); } function node_modfiy_title_ajax_callback($form,$form_state){ $fixed_barcode=taxonomy_term_load($form_state['storage']['hs']['hs_fields']['field_localtion-und']['hierarchical_select']['selects'][0]['#default_value'])->name; $fixed_barcode.=taxonomy_term_load($form_state['storage']['hs']['hs_fields']['field_machineclass-und']['hierarchical_select']['selects'][0]['#default_value'])->name; $fixed_barcode.=taxonomy_term_load($form_state['storage']['hs']['hs_fields']['field_machineclass-und']['hierarchical_select']['selects'][1]['#default_value'])->name; $fixed_class=$form_state['storage']['hs']['hs_fields']['field_machineclass-und']['hierarchical_select']['selects'][1]['#default_value']; $count=db_query("select count(nid) from {taxonomy_index} where tid in (SELECT tid FROM {taxonomy_term_hierarchy} WHERE parent=".$form_state['storage']['hs']['hs_fields']['field_machineclass-und']['hierarchical_select']['selects'][1]['#default_value'].")")->fetchField(); //$count = $query->execute()->fetchField(); $myid=$fixed_barcode.(sprintf("%04d",$count+1)); //$myresult=db_result(db_query("select 1 FROM {node} Where title=%s And type=%s",$myid,"fixed_assets")); $form['title']=array( '#type'=>'textfield','#title'=>'衣车编号','#required'=>TRUE,'#value'=>$myid,'#maxlength'=>255,'#weight'=>-7,'#prefix'=>'<div id=form-item-form-type-textfield-form-item-title>','#suffix' => '</div>','#validated' => TRUE,'#name' => 'title','#id'=>'edit-title','#class'=>'form-text required ajax-processed' ); //if(strlen($myid)<10 || $myresult){ if(strlen($myid)<10){ form_set_error('title','请选择正确的位置信息和机器分类信息,将生成衣车编号'); } return $form['title']; }
程序很简单但其中有较多的知识点:AJAX的使用,sprintf 函数,form_set_error ,二个Hook,还有找到应的变量的结构等等。
程序参考了以下资料:
SQL查询方面的,
https://api.drupal.org/api/drupal/includes!database!select.inc/function/SelectQuery%3A%3AaddExpression/7
https://api.drupal.org/api/drupal/includes%21database%21database.inc/function/db_query/7
https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_select/7
https://www.drupal.org/node/1051242
https://www.drupal.org/node/494026
一个验证Hook,
http://www.thinkindrupal.com/node/1016
AJAX例子,
https://www.drupal.org/node/2465119
ajax – 在drupal中以编程方式更新{node_counter}表
我的问题是 – 我可以通过编程方式递增此计数器吗?我希望当用户与从视图创建的内容(例如点击灯箱)进行交互时实现这一点,因此能够使用AJAX更新表格将是理想的选择.
我已经对d.o进行了快速搜索,似乎没有任何模块可以直接显示出来.有人对这个有经验么?
解决方法
统计模块运行的查询是:
db_query('UPDATE {node_counter} SET daycount = daycount + 1,totalcount = totalcount + 1,timestamp = %d WHERE nid = %d',time(),arg(1)); // If we affected 0 rows,this is the first time viewing the node. if (!db_affected_rows()) { // We must create a new row to store counters for the new node. db_query('INSERT INTO {node_counter} (nid,daycount,totalcount,timestamp) VALUES (%d,1,%d)',arg(1),time()); }
我们唯一需要做的就是用我们想要添加计数的节点id替换arg(1),这可以在这样的自定义模块中完成.
function custom_module_menu() { $items['custom/ajax/%node'] = array( 'title' => 'Update count','page callback' => 'custom_module_update_counter','page arguments' => array(2),'access callback' => array('custom_module_access_control'),); function custom_module_update_counter($node) { db_query('UPDATE {node_counter} SET daycount = daycount + 1,$node->nid); // If we affected 0 rows,this is the first time viewing the node. if (!db_affected_rows()) { // We must create a new row to store counters for the new node. db_query('INSERT INTO {node_counter} (nid,$node->nid,time()); } }
剩下的就是实现一个自定义访问控制功能,你可以检查请求是ajax还是做你喜欢的控制,函数必须返回TRUE或FALSE.您还需要在设置中使用节点ID创建ajax事件,但这也不应该太难.
你需要点击url custom / ajax / 2来更新id为2的节点.
ajax – 如何使用Drupal返回实际的JSON?
<html> <head> </head> <body> <pre> {json here}</pre> </body> </html>
我已经尝试实现一个自定义页面模板,它只输出内容,产生相同的结果.以下是我目前在模块文件中执行此操作的方法…
<?PHP /** * Implementation of hook_menu() */ function content_relation_menu() { $items = array(); $items['api'] = array( 'title' => 'Search','page callback' => 'content_relation_get','access callback' => TRUE,'type' => MENU_CALLBACK,); return $items; } function content_relation_get($term = '') { drupal_add_http_header('Content-Type','application/javascript; utf-8'); $var = json_encode( db_query("SELECT nid,title FROM {node} WHERE title LIKE :title LIMIT 5",array(":title" => $term.'%'))->fetchAll() ); echo $var; exit(0); }
如何返回JUST原始JSON?
drupal_json_output()
和
drupal_exit()
.
$data = db_query("SELECT nid,array(":title" => $term.'%'))->fetchAll(); drupal_json_output($data); drupal_exit();
UPDATE
我只是把你的代码放到一个模块中,当我请求http://site.com/api是预期的JSON时,我得到的就是没有标签.这个问题与Drupal无关,更可能与服务器/浏览器配置有关.
此链接可能有所帮助:
What do browsers want for the Content-Type header on json ajax responses?
ajax修改密码
修改密码是一个十分普通的事情,为什么还要单独写?
- 用ajax修改密码,和普通的提交form表单相比简单。
- 有密码校验规则,放在客户端,减轻了服务器压力。密码匹配输入不符合,提示好看。
- 做完后,在ajax页面能进行的填写数据操作可以提升一个台阶的水平。
使用的easyui插件进行密码修改制作
Easyui有window框、自带输入框校验等功能,不懂多查询api
1制作窗口,引入easyui-window制作一个窗口
引入class=”easyui-window”、title、style、height、padding等制作窗口
2窗口内容,用的还是easyui-layout,分区就中部和南部
3输入的校验规则,使用easyui提供的
只提供了4种,如果没有我们需要的需要使用其他插件,或者我们自己写。
用法:class、数据选项:必填和校验类型
一个添加了校验,一个没有添加校验,注意class里面多个值是用空格隔开。添加了了校验起了作用。
修改了校验规则,长度在4到8之间,生效了
4 点击确定,就提交了,没有需要进一步验证
确定和取消用的是easyui额样式
Jquery给id绑定了事件,我们到这里修改
点击确定的时候,要对所有的数据进行验证,使用一个form包起来,form要在easyui的里面才行。
form要加一个id,然后action没用上去掉
js需要修改:
在验证规则通过后,检查两个密码框输入是否一致。直接比较就好了,注意:messager的使用。
后续在服务端的操作,另外写。
Ajax修改的简单示例
感兴趣的小伙伴,下面一起跟随小编 jb51.cc的小编来看看吧。JS代码如下:
/**
* 修改简单示例
*
* @param
* @arrange (小编) jb51.cc
**/
function editRow() {
if (id != null) {
$('#id').textBox('readonly',true);//表示主键只读不能改
$(#updateflag).textBox(setText,update);//表示添加与修改是同一个框,选择
$(#id).textBox(setValue,id);
$(#names).textBox(setValue,names);
$('#dlg').dialog('open');
} else {
swal('提示','请选择一行数据!');
}
}
// 来自:小编 jb51.cc(jb51.cc)
关于Drupal 中对Node页面的Ajax修改和node.js ajax的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于ajax – 在drupal中以编程方式更新{node_counter}表、ajax – 如何使用Drupal返回实际的JSON?、ajax修改密码、Ajax修改的简单示例的相关知识,请在本站寻找。
本文标签: