Created
June 23, 2010 21:11
-
-
Save kidsil/450566 to your computer and use it in GitHub Desktop.
Ajax Call for Drupal CCK Table Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2010 Asaf Zamir | |
* All rights reserved. | |
* | |
*Redistribution and use in source and binary forms, with or without | |
*modification, are permitted provided that the following conditions | |
*are met: | |
* | |
* 1. Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* 2. Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in the | |
* documentation and/or other materials provided with the distribution. | |
* 3. Neither the name of the University nor the names of its contributors | |
* may be used to endorse or promote products derived from this software | |
* without specific prior written permission. | |
*/ | |
//Added this function so I won't have to get parent().parent().... | |
jQuery.fn.getParent = function(num) { | |
var last = this[0]; | |
for (var i = 0; i < num; i++) { | |
last = last.parentNode; | |
} | |
return jQuery(last); | |
} | |
jQuery(document).ready(function($) { | |
$("[id$='-tablefield-rebuild']").click(function() { | |
rebuildedTable = this; | |
formData = $(this).getParent(3).siblings().find('input').serialize(); | |
formData = formData + '&op=Rebuild+Table&form_id=table_node_form'; | |
$.ajax({ | |
type: "POST", | |
cache: false, | |
data: formData, | |
dataType: 'html', | |
//Switch the node/add/table to node/add/ContentTypeName | |
url: Drupal.settings.basePath+'node/add/table', | |
success: function(msg) | |
{ | |
//Newdata holds the entire Table Div that we got from the Ajax | |
//We are using the rebuildedTable id and not the element since the | |
//Element would return us the original DOM element (not inside msg) | |
Newdata = $(msg).find('#'+$(rebuildedTable).attr('id')).getParent(4); | |
currentTable = $(rebuildedTable).getParent(4).children('table'); | |
if (!(currentTable .size()) || !(currentTable.html())) { | |
$(rebuildedTable).getParent(4).prepend($(Newdata).children('table')); | |
} | |
else { | |
$(rebuildedTable).getParent(4).children('table').replaceWith($(Newdata).children('table')); | |
} | |
}, | |
error: function(XMLHttpRequest, textStatus, errorThrown) | |
{ | |
alert('Error'); | |
} | |
}); | |
return false; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment