jQuery 表格行选择的 css 与需要分隔的 2 个不同背景颜色的行共享

发布于 2025-01-11 17:09:02 字数 1948 浏览 0 评论 0原文

嘿,当我单击一行时,我一直在试图弄清楚如何更改所选属性box-shadow

输入图片这里的描述

我可以这样做:

$('#grdSocialMediaFeeds').on('click', 'tr', function () {
  if ($(this)[0].style.backgroundColor == "") {
     //the rows background is white
     console.log("white");
     $(this).css('box-shadow',"inset 0 0 2px 2px #a7957f"); //Greyish color
  } else {
     //the rows background is red.
     console.log("red");
     $(this).css('box-shadow',"inset 0 0 2px 2px #a92525"); //Dark red-ish color
  }
});

将正确的颜色框阴影放在我单击的行周围,效果很好。

如果它是具有红色背景的行,那么当用户单击/选择该行时,我会在其周围放置较暗的红色框阴影

输入图片此处描述

如果它是具有白色背景的行,那么当用户单击/选择该行时,我会在其周围放置一个灰色框阴影排。

输入图片这里的描述

现在问题是,如果我去选择另一个单元格(白色或红色背景),那么之前单击的行仍然是视为仍被选中行,因此它的仍然具有框阴影,现在“真实”选定的行也应用了框阴影

输入图片这里的描述

为了让事情变得更复杂(比实际应该的更复杂),白色背景行和红色背景行分享相同的tr.k-master-row.k-state-selected CSS 属性。您可以使用 Shift+单击Ctrl+单击选择多行 kbd> 意味着。

输入图片这里的描述

有没有通过jQuery的任何方法/技巧,我可以取消选择任何以前选择的行并让它只显示框- 当前用户行的阴影被选中?

Hey all I am stuck trying to figure out how to change the selected property box-shadow when I click on a row.

enter image description here

I could do this:

$('#grdSocialMediaFeeds').on('click', 'tr', function () {
  if ($(this)[0].style.backgroundColor == "") {
     //the rows background is white
     console.log("white");
     $(this).css('box-shadow',"inset 0 0 2px 2px #a7957f"); //Greyish color
  } else {
     //the rows background is red.
     console.log("red");
     $(this).css('box-shadow',"inset 0 0 2px 2px #a92525"); //Dark red-ish color
  }
});

And that does work fine putting the correct color box-shadow around the row I clicked on.

If it was a row that has a red background then I would put a darker red box-shadow around it when the user clicked/selected that row.

enter image description here

If it was a row that has a white background then I would put a greyish box-shadow around it when the user clicked/selected that row.

enter image description here

Now the problem being that if I go and select another cell (white or red background) then the previously clicked row is still considered as still being the selected row and therefore its still has the box-shadow and now the "real" selected row also has the box-shadow applied to it.

enter image description here

To make things more complicated (than they really should be), both the white background and red background rows share the same tr.k-master-row.k-state-selected css property. And you can select more than one row using the Shift+click or Ctrl+click means.

enter image description here

Is there any way/trick via jQuery that I can un-select whatever previous selected row(s) and have it only show the box-shadow on what the current user row(s) selected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春花秋月 2025-01-18 17:09:03

有什么方法/技巧可以通过 jQuery 取消选择之前选择的行

您已经用简单的语言描述了解决方案,您只需要在 JS 中实现它即可! :-) 这是一个工作片段:

$('#grdSocialMediaFeeds').on('click', 'tr', function () {

    // First remove highlight from all rows
    $('#grdSocialMediaFeeds tr').css('box-shadow', 'none');

    // Now add the highlight to the clicked row
    if ($(this)[0].style.backgroundColor == "") {
        $(this).css('box-shadow',"inset 0 0 2px 2px #a7957f");
    } else {
        console.log("red");
        $(this).css('box-shadow',"inset 0 0 2px 2px #a92525");
    }
});
table {
    width: 100%;
    border: 1px solid #eee;
    border-collapse: collapse;
    border-spacing: 0;
}
td {
    padding: 8px;
    border: 1px solid #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="grdSocialMediaFeeds">
    <tr>
        <td>Date</td>
        <td>Something</td>
        <td>AnotherThing</td>
    </tr>
    <tr style="background-color: #f1c0e8;">
        <td>01/03/2022</td>
        <td>Public</td>
        <td>admin</td>
    </tr>
    <tr>
        <td>01/03/2022</td>
        <td>admin</td>
        <td>admin</td>
    </tr>
    <tr>
        <td>01/03/2022</td>
        <td>Public</td>
        <td>Public</td>
    </tr>
</table>

你确实应该使用 CSS,而不是内联样式,而且这样做会更简单,下面是如何做到这一点的示例:

$('#grdSocialMediaFeeds').on('click', 'tr', function () {

    // First remove highlight from all rows
    $('#grdSocialMediaFeeds tr').removeClass('highlighted');

    // Now add the highlight to the clicked row
    $(this).addClass('highlighted');
});
table {
    width: 100%;
    border: 1px solid #aaa;
    border-collapse: collapse;
    border-spacing: 0;
}
td {
    padding: 8px;
    border: 1px solid #aaa;
}

.white {
    background-color: #eee;
}

.pink {
    background-color: #f1c0e8;
}

.white.highlighted {
    box-shadow: inset 0 0 2px 2px #a7957f;
}
.pink.highlighted {
    box-shadow: inset 0 0 2px 2px #a92525;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="grdSocialMediaFeeds">
    <tr>
        <td>Date</td>
        <td>Something</td>
        <td>AnotherThing</td>
    </tr>
    <tr class="pink">
        <td>01/03/2022</td>
        <td>Public</td>
        <td>admin</td>
    </tr>
    <tr class="white">
        <td>01/03/2022</td>
        <td>admin</td>
        <td>admin</td>
    </tr>
    <tr class="white">
        <td>01/03/2022</td>
        <td>Public</td>
        <td>Public</td>
    </tr>
</table>

为了让事情变得更复杂(比实际应该的更复杂)...您可以使用 Shift+单击或 Ctrl+单击方式选择多行。

是的,这让事情变得复杂了。然而,这里有很多关于如何做到这一点的示例 - 这是一个: 使用shift和鼠标单击选择多个元素 - jquery

因此使用这种方法:

  • 如果是普通单击,首先删除所有突出显示的行,然后添加这highlighted 类到单击的行(我们已经完成了这部分);

  • 如果是按住 Shift 键单击,则将 highlighted 类添加到单击的行,而不删除任何当前突出显示;

  • 但是如果按住 Shift 键并单击时它已突出显示怎么办?在这种情况下,我们希望从单击的行中删除突出显示,而不删除任何其他当前突出显示;

这是一个工作片段,证明了这一点:

$('#grdSocialMediaFeeds').on('click', 'tr', function (e) {

    if (! e.shiftKey) {
        // If it was a plain click (ie NOT a shift-click), remove all
        // highlights, and highlight this row
        $('#grdSocialMediaFeeds tr').removeClass('highlighted');
        $(this).addClass('highlighted');
        
    } else {
        // If it was a shift-click, and the row was already hightlighted,
        // we want to unhighlight it.  If it was not already highlighted,
        // we want to highlight it.  So simply toggling that class:
        $(this).toggleClass('highlighted');
    }
});
table {
    width: 100%;
    border: 1px solid #aaa;
    border-collapse: collapse;
    border-spacing: 0;
}
td {
    padding: 8px;
    border: 1px solid #aaa;
}

.white {
    background-color: #eee;
}

.pink {
    background-color: #f1c0e8;
}

.white.highlighted {
    box-shadow: inset 0 0 2px 2px #a7957f;
}
.pink.highlighted {
    box-shadow: inset 0 0 2px 2px #a92525;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="grdSocialMediaFeeds">
    <tr>
        <td>Date</td>
        <td>Something</td>
        <td>AnotherThing</td>
    </tr>
    <tr class="pink">
        <td>01/03/2022</td>
        <td>Public</td>
        <td>admin</td>
    </tr>
    <tr class="white">
        <td>01/03/2022</td>
        <td>admin</td>
        <td>admin</td>
    </tr>
    <tr class="white">
        <td>01/03/2022</td>
        <td>Public</td>
        <td>Public</td>
    </tr>
</table>

Is there any way/trick via jQuery that I can un-select whatever previous selected row(s)

You've already described the solution in plain language, you just need to implement it in JS! :-) Here's a working snippet:

$('#grdSocialMediaFeeds').on('click', 'tr', function () {

    // First remove highlight from all rows
    $('#grdSocialMediaFeeds tr').css('box-shadow', 'none');

    // Now add the highlight to the clicked row
    if ($(this)[0].style.backgroundColor == "") {
        $(this).css('box-shadow',"inset 0 0 2px 2px #a7957f");
    } else {
        console.log("red");
        $(this).css('box-shadow',"inset 0 0 2px 2px #a92525");
    }
});
table {
    width: 100%;
    border: 1px solid #eee;
    border-collapse: collapse;
    border-spacing: 0;
}
td {
    padding: 8px;
    border: 1px solid #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="grdSocialMediaFeeds">
    <tr>
        <td>Date</td>
        <td>Something</td>
        <td>AnotherThing</td>
    </tr>
    <tr style="background-color: #f1c0e8;">
        <td>01/03/2022</td>
        <td>Public</td>
        <td>admin</td>
    </tr>
    <tr>
        <td>01/03/2022</td>
        <td>admin</td>
        <td>admin</td>
    </tr>
    <tr>
        <td>01/03/2022</td>
        <td>Public</td>
        <td>Public</td>
    </tr>
</table>

You really should be using CSS, rather than inline styles, and it is even a bit simpler when you do, here's an example of how you could do that:

$('#grdSocialMediaFeeds').on('click', 'tr', function () {

    // First remove highlight from all rows
    $('#grdSocialMediaFeeds tr').removeClass('highlighted');

    // Now add the highlight to the clicked row
    $(this).addClass('highlighted');
});
table {
    width: 100%;
    border: 1px solid #aaa;
    border-collapse: collapse;
    border-spacing: 0;
}
td {
    padding: 8px;
    border: 1px solid #aaa;
}

.white {
    background-color: #eee;
}

.pink {
    background-color: #f1c0e8;
}

.white.highlighted {
    box-shadow: inset 0 0 2px 2px #a7957f;
}
.pink.highlighted {
    box-shadow: inset 0 0 2px 2px #a92525;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="grdSocialMediaFeeds">
    <tr>
        <td>Date</td>
        <td>Something</td>
        <td>AnotherThing</td>
    </tr>
    <tr class="pink">
        <td>01/03/2022</td>
        <td>Public</td>
        <td>admin</td>
    </tr>
    <tr class="white">
        <td>01/03/2022</td>
        <td>admin</td>
        <td>admin</td>
    </tr>
    <tr class="white">
        <td>01/03/2022</td>
        <td>Public</td>
        <td>Public</td>
    </tr>
</table>

To make things more complicated (than they really should be) ... you can select more than one row using the Shift+click or Ctrl+click means.

Yep, that complicates things. However there are plenty of examples of how to do that here on SO - here's one: selecting multiple elements using shift and mouse click - jquery

So using that approach:

  • if it is a plain click, first remove all highlighted rows, and then add the highlighted class to the clicked row (we've already done this part);

  • if it is a shift-click, add the highlighted class to the clicked row, without removing any current highlights;

  • but what if it is already highlighted on shift-click? In that case we want to remove the highlight from the clicked row, without removing any other current highlights;

Here's a working snippet demonstrating that:

$('#grdSocialMediaFeeds').on('click', 'tr', function (e) {

    if (! e.shiftKey) {
        // If it was a plain click (ie NOT a shift-click), remove all
        // highlights, and highlight this row
        $('#grdSocialMediaFeeds tr').removeClass('highlighted');
        $(this).addClass('highlighted');
        
    } else {
        // If it was a shift-click, and the row was already hightlighted,
        // we want to unhighlight it.  If it was not already highlighted,
        // we want to highlight it.  So simply toggling that class:
        $(this).toggleClass('highlighted');
    }
});
table {
    width: 100%;
    border: 1px solid #aaa;
    border-collapse: collapse;
    border-spacing: 0;
}
td {
    padding: 8px;
    border: 1px solid #aaa;
}

.white {
    background-color: #eee;
}

.pink {
    background-color: #f1c0e8;
}

.white.highlighted {
    box-shadow: inset 0 0 2px 2px #a7957f;
}
.pink.highlighted {
    box-shadow: inset 0 0 2px 2px #a92525;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="grdSocialMediaFeeds">
    <tr>
        <td>Date</td>
        <td>Something</td>
        <td>AnotherThing</td>
    </tr>
    <tr class="pink">
        <td>01/03/2022</td>
        <td>Public</td>
        <td>admin</td>
    </tr>
    <tr class="white">
        <td>01/03/2022</td>
        <td>admin</td>
        <td>admin</td>
    </tr>
    <tr class="white">
        <td>01/03/2022</td>
        <td>Public</td>
        <td>Public</td>
    </tr>
</table>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文