I have a dropdown list which I populated using a data source (in this case they are hotel names). I want to have one extra field in the list which says "Select a Hotel" which is the default selection when the user opens the page. How can I do this?
If you want the text "Select a Hotel" to be part of your results, then you will have to make it part of your query. Use an union, since you don't mention the type of DB you are using, I'm going to give you an example from MySQL.
select "Select a Hotel"
union
select hotelname from MyDB.hotels;
on the other hand, if you read the hotel names to a storage array, then you could add "Select a Hotel" to the array after reading from the DB.
Also another alternative, when you create the HTML you could add the "Select a Hotel" as part of the first option and then loop through the returned values from the DB. (This is pseudo code)
<select>
<option value="0">Select a Hotel</option>
// loop through storage array
for (int i=0; i<hotels.size(); i++) {
<option value="hotels[i].value">hotels[i].name</option>
} //end loop
</select>
You've got three options here, hope one of these helps.
Related
<select multiple="multiple>
<option th:each="vName : ${variety}"
th:value="${vName}"
th:text="${vName}"
th:selected="${selectedVariety}"></option>
</select>
In the above code "selectedVariety" is a string array sent from controller.
I'm not able to bind the select tag on edit.
And this select tag is not a part of entity table so th:filed="*{selectedVariety}" is not working.
Tried th:attr="selected=${selectedVariety==vName?true:false}">
Not working
On using this "th:selected="${selectedVariety}" every option in the dropdown is getting selected.
What may be the solution??
The option is selected when the value is included to the String array. You'll need following attribute within your <option>:
th:selected="${#arrays.contains(selectedVariety, vName)}"
It returns true (selected) if selectedVariety contains given vName or false (unselected) otherwise.
I'm new to graphQL and Hasura. I'm trying(in Hasura) to let me users provide custom aggregation (ideally in the form of a normal graphQL query) and have then each item the results compared against the aggreation.
Here's a example. Assume I have this schema:
USERTABLE:
userID
Name
Age
City
Country
Gender
HairColor
INCOMETABLE:
userID
Income
I created a relationship in hasura and I can query the data but my users want to do custom scoring of users' income level. For example, one user may want to query the data broken down by country and gender.
For the first example the result maybe:
{Country : Canada
{ gender : female
{ userID: 1,
Name: Nancy Smith,..
#data below is on aggregated results
rank: 1
%fromAverage: 35%
}...
Where I'm struggling is the data showing the users info relative to the aggregated data.
for Rank, I get the order by sorting but I'm not sure how to display the relative ranking and for the %fromAverage, I'm not sure how to do it at all.
Is there a way to do this in Hasura? I suspected that actions might be able to do this but I'm not sure.
You can use track a Postgres view. Your view would have as many fields as you'd like calculated in SQL and tracked as a separate "table" on your graphql api.
I am giving examples below based on a simplification where you have just table called contacts with just a single field called: id which is an auto-integer. I am just adding the id of the current contact to the avg(id) (a useless endeavor to be sure; just to illustrate...). Obviously you can customize the logic to your liking.
A simple implementation of a view would look like this (make sure to hit 'track this' in hasura:
CREATE OR REPLACE VIEW contact_with_custom AS
SELECT id, (SELECT AVG(ID) FROM contacts) + id as custom FROM contacts;
See Extend with views
Another option is to use a computed field. This is just a postgres function that takes a row as an argument and returns some data and it just adds a new field to your existing 'table' in the Graphql API that is the return value of said function. (you don't 'track this' function; once created in the SQL section of Hasura, you add it as a 'computed field' under 'Modify' for the relevant table) Important to note that this option does not allow you to filter by this computed function, whereas in a view, all fields are filterable.
In the same schema mentioned above, a function for a computed field would look like this:
CREATE OR REPLACE FUNCTION custom(contact contacts)
RETURNS Numeric AS $$
SELECT (SELECT AVG(ID) from contacts ) + contact.id
$$ LANGUAGE sql STABLE;
Then you select this function for your computed field, naming it whatever you'd like...
See Computed fields
Post value is multi select, means values are coming in the form of array
if(!empty($_POST['form_type']))
{
$test = implode("','",$_POST['form_type']);
$this->db->where_in('enquiry.type_of_enquiry',"'".$test."'");
}
my query is like this
SELECT `sobha_enquiry`.`name`, `sobha_enquiry`.`date_created`, `sobha_enquiry`.`company`, `sobha_enquiry`.`form_of`, `sobha_enquiry`.`projectname`, `sobha_enquiry`.`city`, `sobha_enquiry`.`country`, `sobha_enquiry`.`phone`, `sobha_enquiry`.`type_of_enquiry`, `sobha_enquiryzone`.`enquiry_id`, `sobha_enquiry`.`hearaboutus`, `sobha_enquiry`.`email`, `sobha_enquiry`.`comments`, `sobha_enquiry`.`address`, `sobha_admin`.`id`, `sobha_admin`.`city_id` FROM (`sobha_senquiry`) LEFT JOIN `sobha_enquiryzone` ON `sobha_enquiryzone`.`enquiry_id` =`sobha_enquiry`.`id` LEFT JOIN `sobha_admin` ON `sobha_admin`.`city_id`=`sobha_enquiryzone`.`city_id` WHERE `sobha_enquiry`.`type_of_enquiry` IN ('\'register form\',\'feedback form\'') GROUP BY `sobha_enquiry`.`id` ORDER BY `sobha_enquiry`.`id` desc LIMIT 15
since i used implode function it is coming like this IN ('\'register form\',\'feedback form\'') i want to remove those backslashes . Please help me
$this->db->where_in() will accept array as second argument.
So there is no need for imploding the data.
if(!empty($_POST['form_type']))
{
$this->db->where_in('enquiry.type_of_enquiry',$_POST['form_type']);
}
This will result as,
WHERE `enquiry`.`type_of_enquiry` IN ('register form','feedback form')
You can refer this link for more info, Codeigniter Active Record Documentation
I have defined a select in a jqgrid table as follows:
{name: 'station', index: 'station', editable: true, width: 60,
edittype:"select", defaultValue:"",
editoptions:{
dataUrl: "getStationList"
},
editrules: { required: true }
},
The getStationList returns something like:
<select>
<option value="id1">Station1</option>
<option value="id2">Station2</option>
</select>
With the current definition, the first time the combo is shown, the list of Station appears nicely (Station1, Station2,...) and the JSON contains "id1" when Station1 is selected.
But when the table updates it displays "id1" in the select combo instead of keeping showing the Station's list.
Is that a bug or I`m missing some configuration option? (probably the last)
Thanks!
I think you have already a problem during filling the grid. If you want to use ids instead of names you should use formatter: "select" additionally to edittype:'select' (see the documentation). In the case you will have to place ids in the grid during filling of the grid with data. I mean that input data for jqGrid sould contains id1 and id2 instead of "Station1" and "Station2". Only in the case you will be able to use dataUrl which provide <option value="id1">Station1</option>. The next problem is: you have to set editoptions.value or formatoptions.value instead of usage dataUrl: "getStationList". So the usage of formatter: "select" is relatively complex. What one can do is to send the data like {"id1":"Station1", "id2":"Station2"} as a part of main grid data. One can place {stations: {"id1":"Station1", "id2":"Station2"}} as userdata part of JSON input (see the documentation). Inside of beforeProcessing callback one could set formatoptions.value based of userdata.stations. In the way one would de facto make request to getStationList not only during editing of data, but additionally during every filling of grid.
I personally prefer to use no formatter: "select" and use selects in the form
<select>
<option value="Station1">Station1</option>
<option value="Station2">Station2</option>
</select>
In the way the client code would "know" nothing about implementation details of the representation of data. One would fill grid with the data "Station1" and "Station2" and send the same data during editing of grid. The server on the other side will get ids by the names whenever it's needed. Typically if I create "lookup" table Stations with column like "Id" and "Name" I would set
CONSTRAINT UC_Stations_Name UNIQUE NONCLUSTERED (Name ASC)
So there are unique index in the table which can get Id by Name. In the way all Update statements with Name are exactly so quickly as with Id. I use Id in all internal SQL statements, but send only Name to external source. In the way I don't need use formatter: "select".
I'm using OpenXML to open a spreadsheet and loop through the rows of a spreadsheet. I have a linq query that returns all cells within a row. The linq query was ripped straight from a demo on the MSDN.
IEnumerable<String> textValues =
from cell in row.Descendants<Cell>()
where cell.CellValue != null
select (cell.DataType != null
&& cell.DataType.HasValue
&& cell.DataType == CellValues.SharedString
? sharedString.ChildElements[int.Parse(cell.CellValue.InnerText)].InnerText
: cell.CellValue.InnerText);
The linq query is great at returning all cells that have a value, but it doesn't return cells that don't have a value. This in turn makes it impossible to tell which cell is which. Let me explain a little more. Say for instance we have three columns in our spreadsheet: Name, SSN, and Address. The way this linq query works is it only returns those cells that have a value for a given row. So if there is a row of data that has "John", "", "173 Sycamore" then the linq query only returns "John" and "173 Sycamore" in the enumeration, which in turn makes it impossible for me to know if "173 Sycamore" is the SSN or the Address field.
Let me reiterate here: what I need is for all cells to be returned, and not just cells that contain a value.
I've tried to monkey the linq query in every way that I could think of, but I had no luck whatsoever (ie - removing the where clause isn't the trick). Any help would be appreciated. Thanks!
The OpenXML standard does not define placeholders for cells that don't have data. In other words, it's underlying storage in XML is sparse. You could work round this on one of two ways:
Create a list of all "available" or "possible" cells (probably by using a CROSS JOIN type of operation) then "left" joining to the row.Descendants<Cell>() collection to see if the cell reference has a value
Utilize a 3rd party tool such as ClosedXML or EPPlus as a wrapper around the Excel data and query their interfaces, which are much more developer-friendly.
With ClosedXML:
var wb = new XLWorkbook("YourWorkbook.xlsx");
var ws = wb.Worksheet("YourWorksheetName");
var range = ws.RangeUsed();
foreach(var row in range.Rows())
{
// Do something with the row...
// ...
foreach(var cell in row.Cells())
{
// Now do something with every cell in the row
// ...
}
}
The one way I recommend is to fill in all the null cells with blank data so they will be returned by your linq statement. See this answer for how to do that.