के भीतर एक कॉलबैक फ़ंक्शन प्रोग्रामिंग मैं एक jQuery प्लग-इन लिख रहा हूं, इसलिए मैं इस कोड को कई स्थानों पर पुन: उपयोग कर सकता हूं क्योंकि यह कोड का एक बहुत अच्छी तरह से इस्तेमाल किया जाने वाला टुकड़ा है, कोड स्वयं तालिका में एक नई पंक्ति जोड़ता है जिसे एक छिपी हुई पंक्ति से क्लोन किया गया है, यह नई पंक्ति पर कुशलतापूर्वक भार का प्रदर्शन जारी रखता है।एक jQuery प्लगइन
मैं वर्तमान में इस तरह यह संदर्भित कर रहा हूँ:
$(".abc .grid").grid();
लेकिन मैं तो हर क्षेत्र प्लग-इन से कहा जाता है कुछ थोड़ा और अद्वितीय कर सकते हैं जब पंक्ति जोड़ दिया गया है एक कॉलबैक शामिल करना चाहते हैं । मैंने पहले jQuery AJAX प्लग-इन का उपयोग किया है, इसलिए success
कॉलबैक फ़ंक्शन का उपयोग किया है, लेकिन यह समझ में नहीं आता कि कोड पृष्ठभूमि में कैसे काम करता है। यहाँ मैं क्या हासिल करना चाहते है:
$(".abc .grid").grid({
row_added: function() {
// do something a bit more specific here
}
});
यहाँ मेरी प्लग में कोड
(function($){
$.fn.extend({
//pass the options variable to the function
grid: function() {
return this.each(function() {
grid_table=$(this).find('.grid-table > tbody');
grid_hidden_row=$(this).find('.grid-hidden-row');
//console.debug(grid_hidden_row);
$(this).find('.grid-add-row').click(function(event) {
/*
* clone row takes a hidden dummy row, clones it and appends a unique row
* identifier to the id. Clone maintains our jQuery binds
*/
// get the last id
last_row=$(grid_table).find('tr:last').attr('id');
if(last_row===undefined) {
new_row=1;
} else {
new_row=parseInt(last_row.replace('row',''),10)+1;
}
// append element to target, changes it's id and shows it
$(grid_table).append($(grid_hidden_row).clone(true).attr('id','row'+new_row).removeClass('grid-hidden-row').show());
// append unique row identifier on id and name attribute of seledct, input and a
$('#row'+new_row).find('select, input, a').each(function(id) {
$(this).appendAttr('id','_row'+new_row);
$(this).replaceAttr('name','_REPLACE_',new_row);
});
// disable all the readonly_if_lines options if this is the first row
if(new_row==1) {
$('.readonly_if_lines :not(:selected)').attr('disabled','disabled');
}
});
$(this).find('.grid-remove-row').click(function(event) {
/*
* Remove row does what it says on the tin, as well as a few other house
* keeping bits and pieces
*/
// remove the parent tr
$(this).parents('tr').remove();
// recalculate the order value5
//calcTotal('.net_value ','#gridform','#gridform_total');
// if we've removed the last row remove readonly locks
row_count=grid_table.find('tr').size();
console.info(row_count);
if(row_count===0) {
$('.readonly_if_lines :disabled').removeAttr('disabled');
}
});
});
}
});
})(jQuery);
मैं आमतौर पर किया है elgooG पर खोज कर रहा है ... लेकिन मैं के साथ शोर का एक बहुत हो रही हो रहे हैं थोड़ा नतीजा, किसी भी मदद की सराहना की जाएगी।
धन्यवाद!
चीयर्स, इस यह बहुत आसान समझने के लिए :-) बना देता है, कई धन्यवाद! –
@ILMV: निश्चित रूप से, कोई समस्या नहीं है। मैंने आपको यह दिखाने के लिए कुछ कोड जोड़ा है कि डिफ़ॉल्ट कॉलबैक कैसे करें ताकि आपको कॉल करने से पहले कॉलबैक अस्तित्व की जांच करने की आवश्यकता न हो। आप जो कर रहे हैं उसके आधार पर, आपको कॉलबैक फ़ंक्शंस को कॉल करने के 'कॉल' या 'लागू' विधियों का उपयोग करने की आवश्यकता हो सकती है। उनका उपयोग करने से आप कॉलबैक के अंदर 'this' के मान को बदल सकते हैं। – Tauren