gpt4 book ai didi

javascript - jQuery 表排序器不适用于 AJAX 获取的值

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:44:31 25 4
gpt4 key购买 nike

我已经实现了 jQuery tablesorter在我的项目中。

我的表由输入文本字段组成,其中一些由 ajax 填充。表格排序对于用户输入的输入字段非常有效,但使用 ajax 从数据库填充的输入字段排序不正确。

我的代码:

jQuery(function () {
jQuery('#tablesorter-demo').tablesorter({
widgets: ['zebra', 'stickyHeaders'],
headers: {
2: {
sorter: 'inputs'
},
3: {
sorter: 'inputs'
},
5: {
sorter: 'inputs'
}
}
});
});

jQuery.tablesorter.addParser({
id: 'inputs',
is: function (s) {
return false;
},
format: function (s, table, cell, cellIndex) {
var jQueryc = jQuery(cell);

// return 1 for true, 2 for false, so true sorts before false
if (!jQueryc.hasClass('updateInput')) {
jQueryc
.addClass('updateInput')
.bind('keyup', function () {
console.log(table);
jQuery(table).trigger('updateCell', [cell, false]); // false to prevent resort
});
}
return jQueryc.find('input[type="text"]').val();
},
type: 'text'
});

我的 AJAX 函数:

jQuery('.bulkupload').keyup(function () {
check = 1;
jQuery("#" + jQuery(this).attr("id")).css("color", "#928F8F");
var part_no1 = jQuery("#" + jQuery(this).attr("id")).val();
var fieldcount = part_no1.toString().length;
var thenum = jQuery(this).attr("id").replace(/^\D+/g, '');

if (jQuery('#qty' + thenum).val() == '') {
jQuery('#qty' + thenum).val('Enter Quantity');
jQuery('#qty' + thenum).css("color", "#DF1F26");
}

var url1 = "<?php echo Mage::getBaseUrl(); ?>availableorders/index/getdetails";
jQuery.ajax({
type: "POST",
url: url1,
data: {
part_no1: part_no1
},
success: function (response) {
if (response == "check") {
jQuery('#item_name' + thenum).val("Not Found");
jQuery('#item_desc' + thenum).val("Not Found");
jQuery('#av_qty' + thenum).css("color", "#DF1F26");
jQuery('#item_name' + thenum).css("color", "#DF1F26");
jQuery('#item_desc' + thenum).css("color", "#DF1F26");
jQuery('#brand_name' + thenum).css("color", "#DF1F26");
}
else {
var result1 = jQuery.parseJSON(response);

jQuery('#item_name' + thenum).val(result1.prodname1);
jQuery('#item_desc' + thenum).val(result1.productdescription1);
jQuery('#brand_name' + thenum).val(result1.brand);
jQuery('#av_qty' + thenum).css("color", "#DF1F26");
jQuery('#item_name' + thenum).css("color", "#DF1F26");
jQuery('#item_desc' + thenum).css("color", "#DF1F26");
jQuery('#brand_name' + thenum).css("color", "#DF1F26");
if (jQuery('#av_qty' + thenum).val(result1.stock) == 0) {
jQuery('#av_qty' + thenum).val("Not in Stock");
} else {
jQuery('#av_qty' + thenum).val(result1.stock);
}

jQuery("#tablesorter-demo").trigger('updateCell');
}
}
});
});

最佳答案

从您使用的选项和小部件来看,您实际上正在使用我的 fork of tablesorter而不是原来的插件。

无论如何,您为输入解析器创建的小部件绑定(bind)到单元格

jQuery(cell).bind('keyup', function () { ... }

但是当对表格进行排序时,单元格会从 tbody 中移除,从而导致任何事件绑定(bind)中断。解决这个问题的方法是使用 delegated event binding在 tbody 上(但在小部件 format 函数之外完成,因为它只需要完成一次)。

jQuery('table tbody').on('keyup', 'input', function(e) {
var $input = $(e.target);
...
}

此外,当您从 ajax 调用更新大量输入时,最好一次更新所有输入 (.trigger('update')),否则您正在使用updateCell 方法过多,可能导致整个过程花费的时间比必要的时间长。

This demo使用非常相似的方法来更新表中的复选框,因此转换它以使其与输入值一起使用应该是相当直接的 - 如果您遇到问题,请在此处发帖,我会提供帮助。

// checkbox parser
$.tablesorter.addParser( {
id: 'checkbox',
is: function( s ) {
return false;
},
format: function( s, table, cell ) {
var $c = $( cell ).find( 'input' );
return $c.length ? $c.is( ':checked' ) ? 1 : 2 : s;
},
type: 'numeric'
});

$( function() {
// using .on() which requires jQuery 1.7+
$( 'table' ).on( 'tablesorter-initialized', function() {

// class name to add on tr when checkbox is checked
var highlightClass = 'checked',
// resort the table after the checkbox is modified?
resort = true,
// if a server side database needs to be updated, do it here
serverCallback = function( table, inputElement ) {},

$table = $( this ),
c = this.config,
wo = c && c.widgetOptions,
// include sticky header checkbox; if installed
$sticky = c && wo.$sticky || '',
doChecky = function( c, col ) {
$table
.children( 'tbody' )
.children( 'tr:visible' )
.children( 'td:nth-child( ' + ( parseInt( col, 10 ) + 1 ) + ' )' )
.find( 'input' )
.each( function() {
this.checked = c;
$( this ).trigger( 'change' );
});
};

$table
.children( 'tbody' )
.on( 'change', 'input', function() {
// ignore change if updating all rows
if ( $table[0].ignoreChange ) { return; }
var col, $this = $( this );
$this.closest( 'tr' ).toggleClass( highlightClass, this.checked );
$this.trigger( 'updateCell', [ $this.closest( 'td' ), resort ] );
// if your server side database needs more parameters, add them here sent to the callback
serverCallback( $table[0], this );
// uncheck header if any checkboxes are unchecked
if ( !this.checked ) {
$table.add( $sticky ).find( 'thead input' ).prop( 'checked', false );
}
})
.end()
.add( $sticky )
.find( 'thead input' )
// Click on checkbox in table header to toggle all inputs
.on( 'change', function() {
// prevent updateCell for every cell
$table[0].ignoreChange = true;
var c = this.checked,
col = $( this ).closest( 'th' ).attr( 'data-column' );
doChecky( c, col );
// update main & sticky header
$table.add( $sticky ).find( 'th[data-column=' + col + '] input' ).prop( 'checked', c );
$table.children( 'tbody' ).children( 'tr:visible' ).toggleClass( highlightClass, c );
// update all at once
$table[0].ignoreChange = false;
$table.trigger( 'update', [ resort ] );
})
.on( 'mouseup', function() {
return false;
});

});
});

我还应该注意,当您的 ajax 调用完成并应用更改时,应该触发“更新”方法,而不是“updateCell”。

最后,有一个现有的 input-select parser但它不包括防止大量 updateCell 方法调用与一次更新所有表的方法。

关于javascript - jQuery 表排序器不适用于 AJAX 获取的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24818386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com