Dynamic Context Binding in XML - odata

I have odata coming from backend. I have two select controls on my SAPUI5 page. The first select displays a list of items received from the backend. The second select changes depending on what is selected from the first select control.
Right now I am constructing in the controller a new path for "planets" select. Is it possible to change the "planets" items path depending on the "stars" selected item just in the XML? Without using Javascript?
I would like to do something like this:
<Select id="stars"
items="{
path: '/Stars'
}">
<core:Item key="{StarID}" text="{StarName}" />
</Select>
<Select id="planets"
items="{
path: '("/Stars('" + StarID + "')/toPlanets"'
}">
<core:Item text="{PlanetName}" />
</Select>

Unfortunately, I do not believe that there is any existing functionality to do something like this naively in UI5. The only thing that is similar is binding replacement during XML preprocessing, but you cannot use that for your situation.
I have met this situation a lot of times in the past and have created a helper control for dealing with this (might not be the perfect solution, but it works). It only makes sense to use a similar approach if you have this kind of construct in multiple places (so you avoid having the same boilerplate code in your JS controllers).
You can find here an example implementation of such a control and here an example usage. Basically this allows you to have a reusable mechanism for doing such an "indirect binding" without resorting to event listeners.

Related

Select2 4.0.0 cascading dropdowns

I'm using select2.full.js
I'm not using any require.js to help me pull in modules, I'm basically using my know how from everything I learned in v3.5
I've been trying countless of hours, wrapping my head around this, but I have no idea how to make ajax cascading dropdowns.
Example: You load up the page, there is the first select2 dropdown that pulls data using ajax automatically (you don't enter anything.. data is there on the page load from the server), a default value is selected. Since that default value is selected, the second select2 drop down will use that value to make an ajax call to populate its own list. Now, when I select another value in the first select2 dropdown, the second select2 dropdown will reflect those changes with the new dataset.
What I've able to come up with was a 3 dropdowns that you can populate on a page load, but when you do any ajax request, the dropdown data doesn't change.
It's been two years since I used select2, and the library keeps changing, the documentation keeps changing, I'm at my wits end with this library. But I feel drawn to it by the bootstrap support, and the look and feel. The functionality, the way to implement things, the documentation is just so unbelievably awful.
I don't have any useful code at all, I've been pulling, plugging, modifying, etc.. and now I'm moving to my hair and teeth.
ANY useful example to do this would be awesome.
Just found my answer..
I was using the templating:
<select id="test">
<option value="1">1</option>
<option value="2">2</option>
</select>
to:
data = [{id: "1", text: "1"}, {id: "2", text: "2"}];
<input id="test" type="hidden" />
and just kept my jquery on select2 the same. So it was really just a change in the templating from to that solved it.
However, the only downside to this is that when you are refreshing new data from these drop downs, the highlighted (selected value) is always the first item in the drop down list, even though that's not the selected value shown in the dropdown.
Here is a small code snippet to make Select2 (v4.x) cascading dropdown - https://gist.github.com/ajaxray/187e7c9a00666a7ffff52a8a69b8bf31
Using this module, options of a select2 list box will be loaded/refreshed by ajax based on selection of another select2 list box.
Example use -
new Select2Cascade($('#province'), $('#district'), 'path/to/geocode', {type:"district", parent_id: ''});
Selected value of parent select2 listbox will be set to the parent_id of the data object before ajax call.

How to loop through properties in a tab and display them using Razor?

I have a Document Type, that has a tab with some properties.
The properties are Upload types, and Simple Editor types.
(Users are supposed to upload images with some image text).
I have not grouped the "Upload" and "Simple Editor" properties, so how do i do this?
Next question,
I want to loop through each group (there should be 3 currently) and display them on my website.
The markup should look like the following:
<div>
<img src="PATH-TO-UPLOAD-TYPE" />
<div>"TEXT FROM SIMPLE EDTIOR TYPE"</div>
</div>
..
<div>
<img src="PATH-TO-UPLOAD-TYPE" />
<div>"TEXT FROM SIMPLE EDTIOR TYPE"</div>
</div>
...
I would like to use Razor for this. Thanks in advance!
For the first part, using the Razor model, you can't. The content object that you get on the front end only contains the properties, the tabs are not included, as they're only really for organising things in the back office.
You CAN get that information using the Umbraco API, but it's pretty database intensive and could potentially be quite slow if you have a lot of properties/tabs.
You'd be better grouping them yourself in your Razor Macro.
for the second part, you can acces the properties of a page via #Model.property. For example:
<div>
<div>#Model.simpleProperty</div>
</div>

Grails: Load data on one ComboBox depending on another

Let's say I have a combobox with the options GENERAL, AIR, GROUND, and SEA
<g:select name="group" from="${['GENERAL', 'AIR', 'GROUND', 'SEA']}" valueMessagePrefix="default.category" value="${tipoN}" />
And then another combobox that loads certain information depending whether you select GENERAL, AIR, GROUND, or SEA.
Let's say GROUND has 3 options, FedEx, USPS, DHL, but AIR has complete different ones, AIRPLANE, JET, HOT AIR BALLOON.
The name of the other <g:select> should be "commodity"
I thought about creating a javascript file and treating everything like HTML but I did some google research and is not as simple as I thought.
Does anyone know what would be the best way to do this?? Thanks in advance!
FG
Sounds like you'll want to use AJAX for this. One way you could do it is by using a combination of templates, and domain objects:
// grails-app/domain/ShippingOption.groovy
class ShippingOption = {
String method, // can be 'ground', 'sea', 'air', or 'general'
name // can be 'fedex', 'ups', etc.
def options = {
def meth = params.method ?: "general"
def comList = ShippingOption.findByMethod(meth)
render(template:"shippingList", model: [ commodityList: comList ])
}
}
And the template:
<!-- grails-app/views/_shippingList.gsp -->
<g:each var="opt" in="${commodityList}">
<option value="${opt.name}">${opt.name}</option>
</g:each>
And in your gsp with the select box on it:
<!-- ... other stuff is before here ... -->
<g:select name="method" from="${['GENERAL', 'GROUND', 'SEA', 'AIR']}"
onchange="${remoteFunction(action:'options', update:'commodity',
params:''method=' + this.value' )}" />
<select id="commodity"></select>
I'm sure I've messed up some syntax, and you'll definitely have to refactor this a bit to work with your code. But at least you've got the general idea.
And to use them, add them to the database as ShippingOptions. Here's one way to do it.
["fedex", "ups"].each { name ->
def so = new ShippingMethod(method: "ground", name: name )
so.save()
}
PS: You'd also be able to render the shipping methods dynamically, as well.
See also: remoteFunction, g:select, templates, and AJAX
I would consider re-designing your UI and changing the flow.
A drop-down dependency that you are describing suggests the form should probably be split and adopting a 'wizard-like' solution will result in a more user-friendly and solid solution that will work also without JavaScript.
I have a worked example using AngularJS and Grail here:
http://wordpress.transentia.com.au/wordpress/2013/12/24/keeping-up-with-the-joneses/
(apologies if this is not appropriate SO 'style', but I dont' think that posting 100s of lines of code and verbiage is appropriate, either).

ASP.NET MVC: add to querystring on form submit

I'm building a grid in ASP.NET MVC and I have the following issue:
Above the grid i have a column selector which lets people customize the columns being shown. This is a form with a submit button so that people can add/remove multiple columns at once without going trough multiple postbacks.
Below the grid I have paging. This is paging trough actionlinks (a href's).
alt text http://thomasstock.net/mvcget.jpg
What happens when a user add/removes columns is that the form gets submitted to http://localhost:56156/?columnsToDisplay=EmployeeId and ofcourse the grid jumps back to page 1. I'd like to keep the grid on the page the user was currently on. So I need a way to include the current querystring parameters into the form's action attribute.
The other way around too: I need a way to do the same with actionlinks. But this is less necessary as I could always replace the a href's with buttons and put them in a form. But I'd rather not do that.
I'm looking for a solution without javascript! I can do it myself in javascript, but I'd like my grid to work perfectly on javascript-disabled browsers.
Any help is appreciated.
Edit:
Oh yeah, to make it a bit harder, I'm also looking for a solution without cookies/session variables. :-)
You need to add the line below into your column selector form
<input type="hidden" name="page" value="<%=Request.QueryString["page"]%>" />

Is there a benefit to using the HtmlHelper in MVC?

Is there a specific reason why I should be using the Html.CheckBox, Html.TextBox, etc methods instead of just manually writing the HTML?
<%= Html.TextBox("uri") %>
renders the following HTML
<input type="text" value="" name="uri" id="uri"/>
It guess it saves you a few key strokes but other than that. Is there a specific reason why I should go out of my way to use the HtmlHelpers whenever possible or is it just a preference thing?
Another benefit is that if your ViewData contains a value matching the name of the field it will be populated.
e.g.
ViewData["FirstName"] = "Joe Bloggs";
<%=Html.TextBox("FirstName") %>
will render
<input type="text" value="Joe Bloggs" id="FirstName" />
There are huge benefits:
It has overloaded methods to pre-populate the values (formatted, and safe for HTML) just like the ViewState.
It allows built in support for the Validation features of MVC.
It allows you to override the rendering by providing your own DLL for changing the rendering (a sort of "Controller Adapter" type methodology).
It leads to the idea of building your own "controls" : http://www.singingeels.com/Articles/Building_Custom_ASPNET_MVC_Controls.aspx
One thing is for consistency...I for one always forget the name attribute. Plus, you can extend the functions for your own projects. They're not called helpers for nothing!
The upside to using an abstraction layer is future proofing your code in a pluggable way. Maybe today, you create HTML 4 pages but tomorrow you want to create XHTML pages or XAML or XUL. That's a lot of changes if you just hard code the tags everywhere, especially if you've got hundreds of pages. If everything is calling this library, then all you've got to do is rewrite the library. The downside is that it is usually considered to be slightly less readable by humans. So, it most probably increases the cognitive demand on your maintenance programmers. These advantages and disadvantages really have nothing to do with MVC.
It actually auto populates your textbox based upon first your ViewData.Model.uri and second by ViewData["uri"]. Doing it manually you'd need to do <input value="<%Html.Encode(ViewData.Model.Uri"%>" />
I haven't been doing MVC too long, but I've already written some extension methods to generate menu tabs based on Html.ActionLink. It allows me to be consistent with my usage and, if I decide to change how my CSS menus work, only modify a single method to output the new tab format.
The other use that I have made of them is in conditional output using ViewData to supply values to the controls.

Resources