【问题标题】:Blazor selection dragging without javascript (razor syntax)没有 javascript 的 Blazor 选择拖动(剃刀语法)
【发布时间】:2021-11-10 09:04:30
【问题描述】:

简化问题,我在拖动时需要选择许多按钮,我正在使用 blazor 服务器端,所以我尝试使用 mousedown 事件(也许我必须使用 mouseover)但我只能选择第一个一。 我的问题是,我可以在不使用 javascript(使用 razor 语法和 c#)的情况下做我需要的事情吗,这是我的 razor 代码:

@page "/"

<h1>Multiple selection example</h1>



@foreach (Button btn in buttonList)
{
    <button disabled="@btn.selected"
            @onclick="(e) => SelectOne(e, btn)"
            @onmouseup="(e) => Over(e, btn)">
        button id @btn.number
    </button>
}


@code{

public List<Button> buttonList = new() {
    new Button() { number = 1 },
    new Button() { number = 2 },
    new Button() { number = 3 },
    new Button() { number = 4 },
    new Button() { number = 5 }
};



public void SelectOne(MouseEventArgs e, Button btn)
{

    btn.selected = !btn.selected;

}
public void Over(MouseEventArgs e, Button btn) {
    if (e.CtrlKey)
    {
        btn.selected = !btn.selected;
    }

}



public class Button
{
    public int number { get; set; }
    public bool selected { get; set; } = false;

}

}

所以当我“鼠标悬停”在按钮上时,我必须“选择”它们。

【问题讨论】:

  • 我可以看到您正在尝试同时更改多个按钮的禁用状态。但是我认为任何时候只有一个输入可以拥有焦点,并触发一个按钮事件。
  • 是的,但是当我向下拖动 @MrCakaShaunCurtis 时,也许有一种方法可以触发每个按钮的事件
  • 我很确定没有办法直接在 Blazor 中执行此操作。通常情况下,这并非不可能,但您将需要一个 JS 库来处理使用鼠标事件突出显示的按钮,然后通过 Blazor JSInterop 将列表传递给影响更改并引发 StateHasChanged 事件的方法在页面上。
  • 当然我认为你是对的也许我需要 js。有了你的部分想法“几乎”得到它,但我不认为解决方案可以接近更多,我把它放在答案上检查它!
  • @MrCakaShaunCurtis 我终于找到了解决方案,检查一下!

标签: .net razor blazor multiple-select


【解决方案1】:

修改 mousedown 处理程序以包含 MouseEventArgs e 参数。

检查 e 的 CtrlKey 属性以根据 MS docs 切换其多选状态

【讨论】:

  • 我通过鼠标事件args,然后我检查Ctrlkey righ,然后你建议做一个多选状态的方法我错了吗? @弗兰克
  • @hesolar 我不知道你为什么要问。您想多选按钮,因此只需在按下 Ctrl 键时将按钮设置为选中。
  • 我正在尝试你的建议,但我没有得到结果,也许我做错了,¿你能用你的解决方案做一个最小的例子吗?我认为做我需要的正确事件可能是鼠标悬停,但我不确定。谢谢。
  • @hesolar 如果你想弄清楚盒子是否在一个矩形内,例如通过在它们上面绘制,你需要调用 JS 来获取边界矩形 stackoverflow.com/a/64554452/12285843
  • 我终于明白了@Frank
【解决方案2】:

终于搞定了,不用js也可以搞定。

@page "/"
<h1>Multiple selection example</h1>



@foreach (Button btn in buttonList)
{
    <button disabled="@btn.selected"
            @onclick="(e) => SelectOne(e, btn)"
            @onmouseover="(e) => Over(e, btn)"
            style="border-block-color:@btn.bordercolor"
            >
        button id @btn.number
    </button>
}


<button @onclick="() => this.multipleSelection = false">Disable multiselection</button>
<button @onclick="() => this.multipleSelection = true">multiselection</button>



@code{
    public bool multipleSelection = false;
    public List<Button> buttonList = new()
    {
        new Button() { number = 1 },
        new Button() { number = 2 },
        new Button() { number = 3 },
        new Button() { number = 4 },
        new Button() { number = 5 }
    };



    public void SelectOne(MouseEventArgs e, Button btn) {
        if (!multipleSelection) btn.select();
        if (this.multipleSelection && buttonList.All(x => x.bordercolor == "none"))
        {

            btn.bordercolor = "coral";
            return;

        }
        if (this.multipleSelection && buttonList.Any(x => x.bordercolor != "none"))
        {

            this.multipleSelection = false;

            this.buttonList.Where(X => X.bordercolor != "none").ToList().ForEach(X => X.select());
            this.buttonList.ForEach(x => x.bordercolor = "none");


        }



    }
    public void Over(MouseEventArgs e, Button btn)
    {


        if (this.multipleSelection && buttonList.Any(x => x.bordercolor != "none"))
        {
            btn.bordercolor = "coral";
        }

    }



    public class Button
    {
        public int number { get; set; }
        public bool selected { get; set; } = false;
        public bool clicked { get; set; } = false;
        public bool hovered { get; set; } = false;
        public string bordercolor = "none";
        public void select() => selected = !selected;
    }


}

【讨论】:

  • 我还是不明白你想要达到什么目的。如果你想多选一些东西,你通常只需 Ctrl-LeftClick 项目。您是在谈论尝试将选择区域拖到某些按钮上吗?要检测用户是否在鼠标按下时按下 Ctrl,只需查看 MouseEventArgs。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-05
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多