I have a a gridpanel that has the following fields:
Name ID Address Home_Phone Cell
Some records that return to the store may not have any data for some of these fields, rather then display an empty column for that record in the grid I would like to show a default value of N/A.
So I would have
Name ID Address Home_Phone Cell
Shaun 2 My Address N/A N/A
Rather then:
Name ID Address Home_Phone Cell
Shaun 2 My Address
I know of two ways to do this:
In the back end if the field doesn't exists return a "N/A" for that column in the JSON.
Create a template with a method that check if the record doesn't contain the value return "N/A".
My question is, is there an easier way to tell the grid column if no value is found in the record display the default N/A?
You can add a renderer for your columns. You either add a renderer for each column or you can define your own column if you have more columns with this behaviour.
//add this for each column config (works for null values and empty strings)
{
renderer: function(value){
if (Ext.isEmpty(value)) {
return 'N\\A';
}
return value;
}
}
or add the renderer to a component that extends the column and use that throughout the application.
It's easier to do this on the front end rather then modifying the backend and sent extra unnecessary data like 'N\A'.
Hope It helps.
Related
I have DBGrid, but not all fields I can see inside Grid (TdxDBGrid)
Was adding three new fields(red V) to qPassengers and I don't see those values.
Here is part of query for DBGrid
SELECT * from Workers left outer join Map on Workers.id = Map.WId
Workers.DecodedStreet AS Street,
Workers.DecodedHouseNumber AS StreetNum,
Workers.DecodedCity AS City,
ISNULL(Map.DecodedStreet,'') AS DStreet, // new field
ISNULL(Map.DecodedStreetNum,'') AS DStreetNum, // new field
ISNULL(Map.DecodedCity,'') AS DCity // new field
and after that with property SummaryGroups I placed new fields to my Grid,
BUT I didn't find any New fields in property FieldName, that connecting fields from query with column in DBGrid
For example So I can't find corresponding field in FieldName for column dbGridTableDStreet,
because DStreet absent in FieldName list, BUT present in the query qPassengers
*Compile and Build was done *
Your grid is using data taken from 'qPassengers' dataset. If this dataset is using persistent fields, then adding the fields to the query of the dataset is not enough to make them visible. The new fields must first be added to the persistent fields collection of the 'qPassengers' dataset. Click with your right mouse button on the 'qPassengers' dataset and open the fields editor to do so.
I have searched Stack Overflow to get an answer to my question, but while I found many interesting cases, none of them quite address mine.
I have a column called fields in my data, that contains JSON information, such as presented below:
Row Fields
1 [{"label":"Label 1","key":"label_1","description":"Value of label_1"},{"label":"Label 2","key":"label_2","error":"Something"}]
2 [{"description":"something","label":"Row 1","key":"row_1"},{"label":"Row 2","message":"message_1","key":"row_2"}]
In essence, I have many rows of JSON that contain label and key, and bunch of other parameters like that. From every {}, I want to extract only label and key, and then (optional, but ideally) stretch every label and key in every {} to its own row. So, as a result, I would have the following output:
Row Label Key
1 Label 1 label_1
1 Label 2 label_2
2 Row 1 row_1
2 Row 2 row_2
Please note, contents of label and key within JSON can be anything (strings, integers, special characters, a mix of everything, etc. In addition, key and label can be anywhere in relation to other parameters within each {}.
Here is the Big Query SQL dummy data for convenience:
SELECT '1' AS Row, '[{"label":"Label 1","key":"label_1","description":"Value of label_1"},{"label":"Label 2","key":"label_2","error":"Something"}]' AS Fields
UNION ALL
SELECT '2' AS Row, '[{"description":"something","label":"Row 1","key":"row_1"},{"label":"Row 2","message":"message_1","key":"row_2"}]' AS Fields
I have first thought of using REGEX to isolate all the brackets and only show me information with label and key. Then, I looked into BQ Documentation of JSON functions and got very stuck on json_path parameters, specifically because their example doesn't match mine.
Consider below approach
select `row`,
json_extract_scalar(el, '$.label') label,
json_extract_scalar(el, '$.key') key
from your_table, unnest(json_extract_array(fields)) el
if applied to sample data in your question - output is
Is it legal/okay to create a row with ID '0' and AFTER that set auto increment of that ID column to (1, 1)?
Why do I ask this?
I have a table Products with columns ID and Name. I want to show all Products inside my application in a ComboBox. Users can select a product but the default entry should not be 'Product 1' but something like '(Nothing selected)'. Is it okay to create a 'dummy row' with 'ID = 0' and Name = '(Nothing selected)' in database so the application will automatically display it as default selected item?
I think the better solution is to programmatically add a please select option and validate against it, assuming you don't really want users to have this selected? I guess in theory what you've proposed should be OK though? You'd have to be careful retrieving the values in order of their Id though, if you ordered them by name you'd have to ensure you pulled Id = 0 first then ordered the rest which feels like more work then programmatically adding the option where needed to me.
If your users can have nothing selected then programmatically add the nothing selected option and handle saving no value if it is selected. Otherwise you're saving data that represents no data really which could be deemed a waste of memory.
If I make an SOQL query such as:
select Id, Username, UserRoleId from User
The API correctly returns the results as XML, however because none of the rows contain a UserRoleId entry this node is completely excluded from the results.
Ideally I would like to be able to create a table from the resultant XML showing all the requested columns, even if they are empty. Is there a way of forcing the API to return even empty columns so that I can do this without resorting to something messy like parsing the SOQL query?
Isn't it a better idea to when you receive an result. Check if the curtain column row is empty. When the column is empty, than you know you have set a empty column by yourself. It is just an idea of my.
Think about a try catch, just an example I use in my application:
try {
parentId = records.getJSONObject(i).getJSONObject("Parent).getString("Name");
catch (JSONException e) {
parentId = "a";
}
When the column name from Parent is empty, then parentId = a. I'd set it manually because I know some other data rolls could have that data.
I have a TClientDataSet, which is provided by a TTableās dataset.
The dataset has two fields: postalcode (string, 5) and street (string, 20)
At runtime I want to display a third field (string, 20). The routine of this field is getting the postalcode as a parameter and gives back the city belongs to this postalcode.
The problem is only about adding a calculated field to the already existing ones. Filling the data itself is not the problem.
I tried:
cds.SetProvider(Table1);
cds.FieldDefs.Add('city', ftString, 20);
cds.Open;
cds.Edit;
cds.FieldByName('city').AsString := 'Test'; // --> errormessage (field not found)
cds.Post;
cds is my clientdataset, Table1 is a paradox Table, but the problem is the same with other databases.
Thanks in advance
If you want to add additional fields other than those exist in the underlying data, you need to also add the existing fields manually as well. The dataset needs to be closed when you're adding fields, but you can have the necessary metadata with FieldDefs.Update if you don't want to track all field details manually. Basically something like this:
var
i: Integer;
Field: TField;
begin
cds.SetProvider(Table1);
// add existing fields
cds.FieldDefs.Update;
for i := 0 to cds.FieldDefs.Count - 1 do
cds.FieldDefs[i].CreateField(cds);
// add calculated field
Field := TStringField.Create(cds);
Field.FieldName := 'city';
Field.Calculated := True;
Field.DataSet := cds;
cds.Open;
end;
Also see this excellent article by Cary Jensen.
Well i found a simpler solution, as i have 24 fields in my sql i didnt wanted to add them all manually so i added a dummy field to the sql statement instead like:
select ' ' as city, the rest of the fields ...
which i can modify in my program OnAfterOpen event.
Well i had to define in the sql how long that field should be by leaving enough empty spaces, for instance 5 empty spaces for 5 characters, so i must know how long the city name could be.
You should use CreateDataset after add field:
cds.SetProvider(Table1);
cds.FieldDefs.Add('city', ftString, 20);
cds.CreateDataset;
cds.Open;
cds.Edit;
cds.FieldByName('city').AsString := 'Test';
cds.Post;
Would like to share more accurate Query for unexisting fields. I bet it's better to use cast, neither spaces!
select E.NAME, E.SURNAME, cast(null as varchar(20)) as CITY
from EMPLOYEE E
e.g. | Marc'O | Polo | <NULL> |
It's more accurate, can definetly see field size, understandable, easy, safe!
if you want to combine already existing "dynamic" data fields (from provider side) with additional client side persistent fields (calculated, lookup, internalcalc, aggregate) you should subclass CDS. just introduce extra boolean property CombineFields and either override BindFields (in newer delphi versions) or the entire InternalOpen (as I did in d2006/2007) with the following line
if DefaultFields or CombineFields then CreateFields; { TODO -ovavan -cSIC : if CombineFields is true then persistent fields will coexist with Default ones }
that will allow you to avoid all that runtime mess with FieldDefs/CreateField