How to hide a column from a Telerik MVC grid and ALSO from the Edit view - telerik-mvc

I have a Telerik MVC grid.
1 of the columns needs to be hidden from certain users.
This is easy to achieve using .Visible
The problem is, when I launch the Edit mode for the row, the column I want hidden is displayed in the edit view.
Any idea how to hide the column from both the grid view and the edit view?
Edit: I'm using Pop Up edit mode and Ajax binding.

Hiding a column from the edit form depends on the edit mode. You have not specified which edit mode you are using so I will give a solution for every single one:
Inline and InCell - you just need to make the column Readonly() and it will no longer be editable.
Popup or InForm - those modes use Html.EditorForModel which displays all properties of the model by design. As a result even the properties which are not bound as columns will be shown. Hiding something from the edit form must be done with JavaScript via the OnEdit event. However the OnEdit event is raised only during Ajax binding. It is not clear from your description whether you are using ajax or server binding. If you use server binding you have to hook to the OnLoad JavaScript event and hide the unnecessary column from the editor using jQuery.

In addition to Atanas' excellent response, if you're using Server binding, you can use the fact that Html.EditorForModel uses ShowForDisplay and ShowForEdit properties to determine when to create scaffolds for the properties. Unfortunately, there's no built-in way to set those independently, but you can create and register your own ModelMetadataProvider. I used the code from this SO answer, and it let me easily specify what gets displayed on edit forms in C# code.
Showing Different fields in EditorForModel vs. DisplayForModel modes in MVC2

I hope this helps... and sorry for all the space between lines but without it, the code looked messy.
Like Atanas Korchev said, you can use javascript but there is a much simpler way. you can define your column as <ScaffoldColumn(False)> :
Public Class EntityS
<Key()> _
<ComponentModel.ReadOnly(True)> _
<DisplayName("Id")> _
<ComponentModel.DataAnnotations.Editable(False)> _
**<ScaffoldColumn(False)> _**
Public Property Id_EntityS As Integer
...
...
End Class
#(Html.Telerik().Grid(Of EntityS)() _
.Name("Grid") _
.DataKeys(Sub(keys)
keys.Add("Id_EntityS")
End Sub) _
.ToolBar(Sub(commands)
commands.Insert().ButtonType(GridButtonType.ImageAndText) _
.ImageHtmlAttributes(New With {.style = "margin-left:0"})
End Sub) _
.DataBinding(Sub(dataBinding)
dataBinding.Ajax() _
...
End Sub) _
.Columns(Sub(columns)
...
columns.Command(Sub(commands)
commands.Edit().ButtonType(GridButtonType.ImageAndText)
commands.Delete().ButtonType(GridButtonType.ImageAndText)
commands.Edit.HtmlAttributes("onedit")
End Sub).Width(180).Title("Commands")
End Sub) _
.Editable(Sub(editing)
editing.Mode(GridEditMode.PopUp).Window(Sub(window)
End Sub)
End Sub) _
....)

Related

WPF application page doesn't update when I update the content of the labels

I'm writing a VB.Net WPF application which uses a frame on the MainWindow to show a page that contains multiple labels. I dynamically update those labels in my logic from the page but once those labels are updated, this doesn't reflect in the frame which shows the page.
This is the sub that updates the labels in Page2.xaml.vb:
Public Sub UpdateLabels(Name As String, microphonefinal As List(Of String))
Dim labelList As New List(Of Label)
For i As Integer = 0 To microphonefinal.Count - 1
For Each Label As Label In Page1Grid.Children
If Label.Name.Contains(Name) Then
Label.Content = microphonefinal(i)
Exit For
End If
Next
Next
End Sub
I call the above sub from MainWindow.xaml.vb using the following:
Dim form = New Page2
If microphonefinal.Count > 0 Then
form.UpdateLabels("Microphones", microphonefinal)
End If
I'm not sure if the issue is because I'm creating a new instance of Page2. But if I don't include this then I get the error: "Reference to a non-shared member requires an object reference".
I tried using frame.Refresh() to see if this would update the page with the new values in the labels but it doesn't. I've also used MsgBox to display the contents of the labels to make sure they've changed.
Am I doing something wrong? Or is there anything I'm missing? Please let me know if there's anymore information you need to help. Thank you!
I managed to access the sub UpdateLabels() of class Page2 by using the frame as you said. So to call the sub I coded it like this:
frame.Content.UpdateLabels("Microphones", microphonefinal)

MS ACCESS 2007 restrict editing to form fields until combobox selection is made

How do i restrict the editing of form fields until a combobox selection is made? I am using MS Access 2007.
Thanks for your help.
On the form, you can start off with all the fields being locked (cannot edit) and disabled (cannot click on them) (you can find those in the property sheet of the form). Then on the On Change event of the combo box, in VBA, cycle through all the fields and enable them again.
This answer provides code for how to loop through controls on a form. In this example, you would set ctl.Enabled = True and ctl.Locked = False (where ctl is the control you are referencing)
A note, to make sure that the combo box is always changed, I usually put "Choose One..." as the first item of the list. And check it on form submission to make sure it's not still selected. That way, it forces the On Change event to fire where it wouldn't if someone used the first option of the list.
Hope that was at least enough to get you started...
EDIT: Additional info
In the table design, I put default value of "Choose 1" on the Combo Box and made it the first option. So when the form comes up, that's what you see.
You can set the Locked property of your memo and date fields to Yes (found in the property sheet under data or you can use the .Locked = True in VBA.
Then, in the on change event of the combo box, click the ... and use the code builder and put something like this:
Dim ctl As Control
If Me.Combo77.Value <> "Choose 1" Then
'Loop through all controls on form
For Each ctl In Me.Controls
'Only text boxes, not labels, or combo box ect.
If ctl.ControlType = acTextBox Then
'Unlock the field and allow editing
ctl.Locked = False
End If
Next
End If
I tried it out and it works on my little test database.
Here is the MSDN article on the control types: MSDN Control Types
To do it this way, I would recommend what I said above and put the first item of the Combo Box something they're not going to choose.

Lightswitch modal window

I've got standard CreateNewEntity screen. Entity can contain list of entities of some other type. By default there is an add button that opens modal window when user wants to add another entity into collection. However, default modal window was lacking some of the needed functionality so I've done a bit of research. Turns out that default modal screens cannot be modified. So, I found a nice custom modal window helper class. The problem is that I can't seem to be able to access modal window fields in order to enforce needed logic. There are two dropdown lists that are associated. Change in one will result in limiting the other dropdown list options. I'm stuck at this particular part:
var proxy = this.FindControl("DodavanjeParcele");
proxy.ControlAvailable += (s, e) =>
{
var ctrl = e.Control as System.Windows.Controls.Control;
//how to obtain access to ctrl fields?
};
"DodavanjeParcele" is custom modal window. Before this, modal window is instantiated and initialized. It pops up after button click and functions as expected. The only thing missing are above-mentioned rules. I need to set change event handlers for modal window fields in order to define rules. As seen above I tried to cast IProxy as a standard Windows control. This is where I got stuck. I can't seem to find a way to access control fields and set event handlers. Any thoughts?
If I understand you correctly, I'm not sure why you need to search through controls or cast anything.
Control1 is an entity which creates an AutoComplete Box (dropdown list). That selection is copied into a local property in the Control1_Changed method. That property is used as a parameter in a filter query to create Control2.
C#:
private void Control1_Changed()
{
this.MyLocalProperty = this.Control1.SelectedItem;
}
VB.NET:
Private Sub Control1_Changed()
Me.MyLocalProperty = Me.Control1.SelectedItem
End Sub
Just make sure you have Auto Execute Query checked in Control2's Properties and the second control should update and filter when Control1 changes the query parameter.
The code in my screen shots all takes place inside of Yann's Modal Helper so there is nothing special you need to do.

Additional filtering parameters in #fuelux datagrid

Does the fuelux datagrid support adding additional filter options? I would like - for instance - to add (next to the filter dropdown you already have in place), a checkbox which should also be added as a parameter to my function that retrieves the data remotely from the server.
It would be nice if the grid would automatically take any parameters from input elements that we can mark by adding a specific class or data- attribute.
Thanks very much! :)
David.
You can absolutely do this.
Add filtering logic to the datasource you provide, keyed off any custom property you add to the datasource.
Add the checkbox within the datagrid markup (or anywhere).
When your checkbox is clicked, update the custom property on your datasource and then call $('#MyGrid').datagrid('reload') to tell the datagrid to reread your datasource

Combobox clearing value issue

I've stumbled on an issue with Comboboxes in javafx2.2. This is the scenario:
Users click on the 'editFile' button.
Another pane becomes visible (with the setVisible method).
This pane contains 6 comboboxes.
Three of them have fixed items: cboReport, cboSales, cboSend. Three of them get their data from a db (ObservableList) and get populated when the pane becomes visible: cboFile, cboCustomer, cboVet
The user selects a file number from the cboFile. The rest of the comboboxes are beeing set with the correct values.
The user presses the save button, the file gets saved as intended.
Next the user presses a close button.
When the window closes, the data on the pane gets resetted through a resetGUI_editFilePane() method. There is have lines like:
...
cboReport.getSelectionModel().clearSelection();
cboSales.getSelectionModel().clearSelection();
cboSend.getSelectionModel().clearSelection();
cboFile.getSelectionModel().clearSelection();
cboCustomer.getSelectionModel().clearSelection();
cboVet.getSelectionModel().clearSelection();
cboFile.getItems().clear();
cboCustomer.getItems().clear();
cboVet.getItems.clear();
...
When the user opens the pane again by pressing the 'editFile' button I notice that only the 'fixed item' comboboxes have cleared their selection, the dynamicly filled comboboxes show the last selected item although the value from the selection itself is null. This looks like a graphics bug to me or am I doing something wrong?
Is there any way around this issue or what is the best method to reset a combobox?
EDIT 2014/08/27:
This is officially not a bug(clearSelection() does not clear value):
https://bugs.openjdk.java.net/browse/JDK-8097244
The official "workaround" is to clear the value of the ComboBox after clearing selection.
cb.getSelectionModel().clearSelection();
// Clear value of ComboBox because clearSelection() does not do it
cb.setValue(null);
It is very simple. You just need to work with the value property of ComboBox. here you go ....
ComboBox c;
c.valueProperty().set(null);
I hope this works for you :-D
I ran into nearly the exact same situation and came across your question while looking for a solution. Fortunately, I came up with a workaround that forces the ComboBoxes to reset. When you reset the data on your pane, instead of doing something like:
cboVet.getSelectionModel().clearSelection();
cboVet.getItems.clear();
do something like this...
parentNode.getChildren().remove(cboVet);
cboVet = new ComboBox(); // do whatever else you need to format your ComboBox
parentNode.add(cboVet);
You'll also need to do a setItems() again on your ComboBox so the new one will be populated. This is not an ideal solution but it seems to be working as I expect the provided clearSelection() method would.
You can retrieve the items and have them all removed:
cboVet.getItems().removeAll(cboVet.getItems());
I've just tested a working solution with the Java JDK 1.7.11:
combobox.setSelectedItem(null);
combobox.setValue(null);
Hope it helps :)
I use reflection with direct manipulation of buttonCell field in ComboBox skin:
#SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> void resetComboBox(ComboBox<T> combo) {
Skin<?> skin = combo.getSkin();
if(skin==null){
return;
}
combo.setValue(null);
Field buttonCellField;
try {
buttonCellField = skin.getClass().getDeclaredField("buttonCell");
buttonCellField.setAccessible(true);
ListCell buttonCell = (ListCell) buttonCellField.get(skin);
if(buttonCell!=null){
StringProperty text = buttonCell.textProperty();
text.set("");
buttonCell.setItem(null);
}
} catch (NoSuchFieldException
| SecurityException
| IllegalArgumentException
| IllegalAccessException e) {
e.printStackTrace();
}
}
I think it's also possible by providing your own buttonCell implementation through buttonCellFactory property
I had the same problem with a ComboBox. The buttonCell of the ComboBox is not updated correctly when I change the items of the ComboBox. This looks like a graphics bug.
I use direct manipulation of buttonCell field in ComboBox.
combo.getButtonCell().setText("");
combo.getButtonCell().setItem(null);
This is the best solution I've found without recreate the ComboBox.
To clear SelectionModel I found nothing better than creating a new instance of Combobox (previous answers update):
myParentNode.getChildren().remove(myCombobox);
myCombobox = new ComboBox();
myParentNode.add(myCombobox);
But this solution evolves other problems: if you use fxml, this combobox will be placed in the wrong place and with wrong parameters. Some fxml parameters are hardly reproduced directly from your controller class code and this is awful to do it every time you need to clear the combobox.
The solution is using custom components instead of creating instances directly in main controller class code, even if these components are standard. This also helps to free some lines in your main controller class by moving component related event methods and other methods to a separate class file, where you use a reference to your main controller class.
How to create custom components in JavaFX FXML Application can be found in http://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm , but note that CustomControlExample class is not needed for every custom component in your application, if it already has an entry point class with start(Satge stage) method.
How to resolve possible errors with reference from custom component controller class to main controller class can be found in JavaFx: how to reference main Controller class instance from CustomComponentController class?
I need to clear selection of the combo box. And this code worked for me:
List<Object> list = new ArrayList<>(comboBox.getItems());
comboBox.getItems().removeAll(list);
comboBox.getItems().addAll(list);

Resources