简体   繁体   中英

Reverse checkbox value so that checked is false in the model

I have a Kendo TreeView where I need to invert the visual presentation of the checkboxes. So the value false would result in a checked checkbox.

The reason for this is that the treelist is listing a bunch of items that should be excluded. So when they check something it's excluded. That isn't terribly intuitive.

Changing the logic behind saving this exclude list is really something I'd like to avoid. Instead I'd rather just change the visual presentation of the data. So that checked=false would result in a checked checkbox and the opposite.

Here's what I'm working with

@(Html.Kendo().TreeView()
    .Name("Tree")
    .Checkboxes(c => c.Name("Checked"))
    .BindTo((IEnumerable<TreeViewModel>)ViewBag.Tree, mappings =>
    {
        mappings.For<TreeViewModel>(binding => binding.ItemDataBound((item, vm) =>
        {
            item.Id = vm.Id.ToString();
            item.Text = vm.Name;
        })
        .Children(vm => vm.Children));
    });
)

You can set the "Checked" property based on the passed item as follows:

  mappings.For<TreeViewModel>(binding => binding.ItemDataBound((item, vm) =>
    {
        item.Id = vm.Id.ToString();
        item.Text = vm.Name;
        item.Checked = vm.Checked ? false : true;
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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