How to access child 'model' data using parent 'model' to show in grid in extjs - user-interface

Scenario : I have two grid on UI 1st is live-offence 2nd Disposed-offence there is on field in 1st grid 'nextDate' ,model of first grid is 'offence' There is another model which is child of offence model that result model ,and the field next-date is inside result model, and result model is in require of offence model. how to access next-date on grid.

If i understand correctly, you can use the column renderer function, which gives you the row's record as a parameter, and access the child result record from there. I'm not sure how the relationship is setup so you'll have to work that bit out yourself!
renderer: function (value, metaData, record) {
var resultRecord = record.getResultRecord(); // replace this with correct code!
return resultRecord.get('next-date');
}

Related

Query to pass data to the edit view

I need to pass data to the "edit" view from the "index" view, the data are from two different related tables (table "personas" parent and table "residentes" child) so i'm working in the "edit()" method in the residentesController and i couldn't find a way to pass the data in a single object, is it possible to do it? if there is not What is the best way to do it?
//in the ResidentesController
//this is what i currenly have
public function edit(Residente $residente)
{
$persona = Persona::find($residente->persona_id);
return Inertia::render(
'Residentes/Editar',
[
'residente' => $residente,
'persona' => $persona
]
)
}
If i understand correctly your question you need to load the relationship you need in the original query (keep in mind to create it in the model classes)
$persona = Persona::with('residentes')->find($residente->persona_id);
and after that you can create the variable
$residente = $persona->pluck('residentes');

Yii2: How to avoid required fields in a view?

I have a view about holidays where a user uses a form to choose a place to travel and a hotel. It has two models: HolidaysPlaces and HolidaysHotels.
The user have to fill the form in this order using the view:
The user completes the fields called Place and City (related with the HolidaysPlaces model).
The user checked a checkbox if he/she wants to choose a hotel. It able a field called Hotel (related with HolidaysHotels model).
The user completes that field.
The user press a Create button.
The controller receives and saves both models.
But the problem is when the user doesn't select the checkbox (number 2 of the list): The Hotel fieldis still required (with the red asterisk as defined in its model file). So the Create button doesn't work in this case.
How can I disabled the required feature?
Add a scenario for this case in your HolidaysHotels model, and include only the fields that you want checked.
Example: If you have 3 fields name, date and age that are required, create a scenario for two only, and set the scenario in the controller. Only those two fields will be checked.
In model:
public function scenarios(){
$scenarios = parent::scenarios();
$scenarios['create'] = ['name', 'date'];
return $scenarios;
}
In controller:
$holiday = new HolidayHotels();
$holiday->scenario = 'create';
To know more about scenarios: http://www.yiiframework.com/doc-2.0/guide-structure-models.html#scenarios
You can add some condition based validation in your model rules. Here is the snippet for both client and server validation. You can many conditions inside the function block.
['field-1', 'required', 'when' => function ($model) {
return $model->check_box == '1';
}, 'whenClient' => "function (attribute, value) {
return $('#checkbox-id').is(':checked') ';
}"],
The easiest way to solve it is to send the model with empty strings. Then the controller checks if the strings are empty. If so, the model is not saved. Else, it is saved.
It was the only way that works for me.

Laravel Backpack : Storing Belongs To Many relationships using custom view

I have a flight class and this flight has a custom view field like so:
This represents a belongs to many relationship which stores website_id / flight_id and pricing as pivot data in a pivot table.
The custom view uses JS to send this data back to the controller in this format:
{"1":{"price_adult":"434","price_child":"545"},"2":{"price_adult":"323","price_child":"324"},"3":{"price_adult":"434","price_child":"43"}}
Trying to send this data with the request doesn't create the relations fields, and because I do not have a flight ID at the point of creating this within the controller I can not loop this JSON to make the relations manually.
Can anyone point out what the best course of action is or if there is support for this? I took a look at the docs but they are woefully short and patchy in terms of being much help.
EDIT:
I should have said I can probably make this work using a custom name attribute on the model for the relation, then add a set mutator to loop this data and update the prices relation but I don't want to go down this route if there is support for this I am missing out of the box in backpack.
EDIT2:
Someone asked about the relation:
$this->belongsToMany(Website::class, 'website_pricing')->withPivot('price_adult', 'price_child');
This is working fine its not a problem with the relation working its how can I get backpack to store the data as a relation when the flight has no ID yet, or how can I pass the data I posted above in such a way that the backpack crud controller can handle it?
You may need to create a flight first, if no flight id is being provided. Can you explain the database relational structure more?
Basically thought I should post what I did because no one could provide an answer to this.
So basically you have to copy the store / update functions from the parent, changing a few lines.
$this->crud->hasAccessOrFail('create');
// fallback to global request instance
if (is_null($request)) {
$request = \Request::instance();
}
// replace empty values with NULL, so that it will work with MySQL strict mode on
foreach ($request->input() as $key => $value) {
if (empty($value) && $value !== '0') {
$request->request->set($key, null);
}
}
// insert item in the db
$item = $this->crud->create($request->except(['save_action', '_token', '_method']));
$this->data['entry'] = $this->crud->entry = $item;
// show a success message
\Alert::success(trans('backpack::crud.insert_success'))->flash();
// save the redirect choice for next time
parent::setSaveAction();
return parent::performSaveAction($item->getKey());
Basically any line which references a function in the parent class using $this->method needs to be changed to parent::
This line is what I used to submit the relations JSON string passed to the controller as relations $item->prices()->sync(json_decode($request->input('prices'), true));
This is done after the line containing $item = $this->crud->create as the item id that just got stored will be available at that point.

Kendo datasource model - difference between _data[0] and get(0)

I would like to know the difference between
$("#uploadedFile").val(e.files[0].name);
var model = $("#blueprint_listview").data("kendoListView").dataSource.get(0);
model.set("filename", $("#uploadedFile").val());
And
$("#uploadedFile").val(e.files[0].name);
var model = $("#blueprint_listview").data("kendoListView").dataSource._data[0];
model.set("filename", $("#uploadedFile").val());
I am having an editable listview with a upload.
And the above code is written on the success event on the kendo upload.
The second code works fine for insert and update.
However, the first code works fine for insert, but for update it is showing an error which says - "The model is not defined"
I was wondering what could be the reason?
As stated in the documentation, get retrieves a record with the corresponding id. This way, when a new record is inserted it seems that it has the default id of 0, that's why get(0) === _data[0] but when you are updating the listview, a "real" id (>=1) is given to your new line and there is no longer an item with id=0, so model is then null.
On the other side, the internal method _data is an array with all the lines of your list view put in the order of their position in the listview. But if you want to access to this property, the equivalent "public" method is at :
$("#blueprint_listview").data("kendoListView").dataSource._data[0] ===
$("#blueprint_listview").data("kendoListView").dataSource.at(0); // allways true

Where does DropDownListFor get the model from?

You can create a dropdownlist using
#Html.DropDownListFor(model => model.SelectedId, model.DropDownItems);
but where does DropDownListFor get the value to pass into the model => model.SelectedId lambda from?
SelectedId is just the integer of the field in the model.
the model is passed in from the controller using
return View(myModel);
And the model can be defined in the view at the top
#model mymodelnamespace.RoomBookingInsert
So the dropdownbox's value is set as the SelectedId field in your model.
A proper field name for example would be RoomNo if that clarifies it better.
#Html.DropDownListFor(model => model.RoomNo, model.Rooms);
As long as model.DropDownItems (or model.Rooms in my example) Contains a item with the value of that attribute, it will set it as the selected item in the list as default.
Edit:
If you are using viewbag, rather than the model, instead use DropDownList. (each input has a for extension for use with models)
#Html.DropDownList("RoomNo",
new SelectList(ViewBag.Rooms as System.Collections.IEnumerable ?? Enumerable.Empty<SelectListItem>(), "RoomNo", "RoomName"),
new { #value = #ViewBag.RoomNo})
This above, creates an input form with id RoomNo,
and uses a SelectList that will default to empty if null. The fields that defined the values and text shown are set at the end of the select list parameters.
The last bit sets the default value to the contents of #ViewBag.RoomNo.
You can also create the select list in the controller, and then simply set the dropdown options to the viewbag.myselectList entity.

Resources