简体   繁体   English

查找具有特定值的单元格的行

[英]Finding a row with a cell with a specific value

I have to mark all rows in a table (and later change the code to delete the rows). 我必须标记表中的所有行(然后更改代码以删除行)。

The table structure looks like: 表结构如下:

<table class="table table-striped table-condensed" id="buckettable">
            <thead>
                ...cut out
            </thead>
            <tbody>
                <tr class="success">
                    <td><input class="fld-bucketno" type="checkbox" name="bucket" value="158bf61f-8f66-4dee-9ff6-b9d1f6d4b840" /></td>
                    <td class="text-right">
                        <a class="bucketNo" href="/Sim/Staging/1212">1212</a>
                    </td>

What I am interested in is the value of the anchor in the td - second last line. 我感兴趣的是td中锚点的值-第二行。

I get a json list of id's to delete. 我得到要删除的ID的JSON列表。 First I mark them, then change the code to delete them. 首先,我标记它们,然后更改代码以将其删除。 That is the idea, at least. 至少是这样。 Mind me - I know a lot of programming, but Javascript is an unknown land for me ;) 注意我-我知道很多编程知识,但是Java语言对我来说是未知之地;)

My code so far is: 到目前为止,我的代码是:

success: function (resp) {
                var table = $('#buckettable').find('tbody')[0];

                var length = resp.length;
                for (var i = 0; i < length; i++) {
                    var bucketNo = resp[i];
                    var rows = table.children.length;
                    for (var j = 0; j < rows; j++) {
                        var row = table.children[j];
                        var bucket = row.find('.bucketNo').text();
                        if (bucket == bucketNo) {
                            alert(bucket);
                        }
                    }
                }
                $('#cleanup').modal('hide');

and I fail to filter the rows. 而且我无法过滤行。 I am open to any sensible other approach - hopefully it teaches me a lot. 我愿意接受任何其他明智的方法-希望它能教给我很多东西。

In the above code I manage to get the row... but then the find('.bucketNo') fails with an aexception that Object # has no method find.... which I find funny because I use that on a form earlier. 在上面的代码中,我设法获得了该行...但是find('。bucketNo')失败,并抛出一个异常,即对象#没有找到方法...。我觉得很有趣,因为我在较早的表单上使用了该方法。 Pointers to documentation VERY welcome, especially in addition to an answer. 指向文档的指针非常欢迎,尤其是除了答案之外。

If there is a better way to do that tell me. 如果有更好的方法可以告诉我。 I was told that search by ID is faster (obviously) but somehow I am not sure - should I set a coded ID (bucket-xxx, xxx being the number) on every row? 有人告诉我,按ID进行搜索的速度更快(很明显),但是不知为何-我应该在每行上设置一个编码ID(bucket-xxx,xxx是数字)吗?

There is a much simpler way of doing this. 有一种更简单的方法。

var targetBucketNo = 1212;
$('#buckettable a.bucketNo')
  .filter(function(item) {
    return item.text() == targetBuvketNo;
  })
  .parent()
  .remove();

To explain in more detail. 详细解释。 The following will get the anchors with the bucketNo class that are inside your #buckettable table. 以下代码将获取#buckettable表中具有bucketNo类的锚点。

$('#buckettable a.bucketNo')

Filter will filter the results for ones that have the target bucket number 筛选器将筛选具有目标存储桶编号的结果

.filter(function(item) {
    return item.text() == targetBuvketNo;
 })

Remove will remove the elements 删除将删除元素

.remove();

You can replace .remove() with .addClass('your-class-name') if you wanted to add a class instead. 如果要添加类,可以将.remove()替换为.addClass('your-class-name') This would add it to the td element so you should add .parent() before addClass . 这会将其添加到td元素中,因此您应该在addClass之前添加.parent()

You are accessing the native Javascript children , hence find() is not a function. 您正在访问本机Javascript children ,因此find()不是函数。 However you can skip around a lot of the native functions you're using by using the jQuery filter method: 但是,您可以通过使用jQuery过滤器方法跳过许多正在使用的本机函数:

var $aTag = $('#buckettable').find('tbody td a.bucketNo').filter(function() {
    return $(this).text() == bucketNo
});
alert($aTag.text());

You can also loop all links with the class "bucketNo" and then look if the ID is in your array. 您还可以使用“ bucketNo”类循环所有链接,然后查看ID是否在您的数组中。 After this get the containing TR and delete it or add a class or something: 之后,获取包含的TR并将其删除或添加一个类或其他内容:

var resp = [2323,5656]

$('a.bucketNo').each(function() {
    if( resp.indexOf( parseInt($(this).text()) ) != -1 )
        $(this).closest('tr').addClass('del');
});

http://jsfiddle.net/44cEt/ http://jsfiddle.net/44cEt/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM