hx-trigger only when boolean is true - htmx

I have 2 triggers here. I want to disable the second trigger (id_indoor_manufacturer) if manufacturer_boolean is false. I've tried adding a filter to the trigger, but it doesn't work (see below)
manufacturer_boolean = forms.BooleanField(
initial=True,
widget=forms.CheckboxInput(attrs={
'class':'form-control',
'autocomplete': 'off',
'hx-get': '/companies/outdoor_manufacturer/',
'hx-target': '#id_outdoor_manufacturer',
'hx-include': '#id_indoor_manufacturer, #id_outdoor_manufacturer',
'hx-trigger': 'change from:#id_manufacturer_boolean, change[target == true] from:#id_indoor_manufacturer',
}))

Related

How to use get default method on a selection field based on condition odoo 12?

What im trying to do is fetching a element from the selection field based on the state of the record.
#api.model
def _get_next_step(self):
for rec in self:
if rec.state == 'draft':
return rec.write({'next_step': 'waiting_room'})
elif rec.state == 'waiting_room':
return rec.write({'next_step': 'start_consultation'})
elif rec.state == 'start_consultation':
return rec.write({'next_step': 'finish_consultation'})
next_step = fields.Selection([
('waiting_room', 'To Waiting Room'),
('start_consultation', 'Start Consultation'),
('finish_consultation', 'Finish Consultation'),
('follow_up', 'Follow-Up'),
], string='Next Step', copy=False, index=True, track_visibility='onchange', defult='_get_next_step')
what i tried to do here is that,applying default in the selection field and wrote a function for the default method,But the field next_step is not getting updated.
The default execution environment will never have records, self is always an empty recordset. The api.model decorator is telling you that already.
You could just change the field next_step to a computed field and trigger the recomputation on state. When you store the computed field, everything like searches/grouping will work like on normal fields.

How to change many2many field route_ids value in odoo?

I have been working for my below code for few days, but still can't get it work. What I intended to do is when one select raw-material product type (customized field) in Product Create section, Buy route will be selected. On the other hand, select "Finish Good", Manufacture and MTO route will be selected. The below codes does the half because it doesn't clear the previous selected value when switch from Raw-Material to Finish Good. The previous filled up value still remain. Please help! Thx so much.
# api.onchange ('custom_product_type')
def _onchange_custom_product_type (self):
if self.custom_product_type:
self.warehouse = self.env.ref ('stock.warehouse0')
route_manufacture = self.env.ref ('stock.warehouse0'). manufacture_pull_id.route_id.id
route_mto = self.env.ref ('stock.warehouse0'). mto_pull_id.route_id.id
buy_route = self.env.ref ('stock.warehouse0'). buy_pull_id.route_id.id
if self.custom_product_type == 'RM':
self.sale_ok = False
self.purchase_ok = True
self.update ({'route_ids': [(6, 0, [buy_route])]})
elif self.custom_product_type == 'FG' or self.custom_product_type == 'HG':
self.sale_ok = True
self.purchase_ok = False
self.update ({'route_ids': [(6, 0, [route_manufacture, route_mto])]})
Your code should work, but you can try to remove them with (5, ) before adding the new routes:
self.update({'route_ids': [(5, ), (6, 0, [route_manufacture, route_mto])]})
Actually, I found out my original works after save. Althought previous selected value doesn't change in the "form", it does change after press "Save".

MVC 4 WebGrid Checkbox in a column with Condition

I'm trying to add a new column to a .Net MVC WebGrid that includes a checkbox that is there if a specific condition is met and not there if the condition is false.
The below code works to correctly display X or Y (placeholder):
grid.Column("ID", header: "",
style: "labelcolumn",
format: (item) => item.ID != null ? "X" : "Y"),
I can't seem to get the syntax right to include the checkbox instead of X.
grid.Column("ID", header: "",
style: "labelcolumn",
format: (item) => item.ID != null ? #<text><input class="check-box" id="cbSelectedBranch" name="cbSelectedBranch" type="checkbox" value="#item.ID" /></text> : "Y"),
On this second snippet, the "(item)" variable causes this error:
CS0136: A local variable named 'item' cannot be declared in this scope
because it would give a different meaning to 'item', which is already
used in a 'parent or current' scope to denote something else
Adding the # when using the if null condition seems to cause item to throw this error. The below code, without the conditional, works correctly:
grid.Column(header: "",
style: "labelcolumn",
format: #<text><input class="check-box" id="cbSelectedBranch" name="cbSelectedBranch" type="checkbox" value="#item.ID" /></text>),
Any idea how I can make this work with a conditional and checkbox input?
try like this:
format: (item) => item.ID != null ? Html.Raw("<input class='check-box' id='cbSelectedBranch' name='cbSelectedBranch' type='checkbox' value='#item.ID' />") : "Y")

codeigniter like clause overriding where clause

At least that is what seems to be happening. I'm trying to create a search bar for a website and it does work, except it's not reading a where clause which would only pull back approved content. You can kind of see why that would be an issue.
Anyway, this is what I have in my model
$match = $this->input->post('search');
$this->db->where('approved', 'y');
$this->db->like('description', $match);
$this->db->or_like('title', $match);
$this->db->or_like('body', $match);
$this->db->or_like('author', $match);
$query = $this->db->get('story_tbl');
return $query->result();
and when I print out the query, it seems like it's seeing the where clause, but when I get the stuff back it's pulling things that are not approved or under review.
Here is my printed query
SELECT * FROM (`story_tbl`) WHERE `approved` = 'y' AND `description` LIKE
'%another%' OR `title` LIKE '%another%' OR `body` LIKE '%another%' OR
`author` LIKE '%another%'
Your query should be
SELECT * FROM (`story_tbl`) WHERE `approved` = 'y' AND (`description` LIKE
'%another%' OR `title` LIKE '%another%' OR `body` LIKE '%another%' OR
`author` LIKE '%another%')
Check those brackets. So, your best option is to use plain $this->db->query(). If you insist on using active records, you have to do it like this for those brackets -
$match = $this->input->post('search');
$this->db->where('approved', 'y');
$this->db->where("(`description` LIKE '%$match%'");
$this->db->or_where("`title` LIKE '%$match%'");
$this->db->or_where("`body` LIKE '%$match%'");
$this->db->or_where("`author` LIKE '%$match%')");
$query = $this->db->get('story_tbl');
EDIT:
true AND true OR true OR true //true
false AND true OR true OR true //true just like your where clause is ignored
false AND false OR false OR true //Still true
false AND true OR false OR false //false
false AND false OR false OR false //false
So this query will return all the rows where approved = 'y' OR where title, body, author matches 'another'
In my posted query
true AND (true OR true OR true) //true
false AND (true OR true OR true) //false, where clause is checked
true AND (false OR false OR false) //false
true AND (true OR false OR false) //true
Which will return the rows where approved = 'y' AND also either title or body or author or description matches 'another'. I believe this is what you want to achieve.
You can use codeigniters's group_start() and group_end() for this. The code can be modified like this
$match = $this->input->post('search');
$this->db->where('approved', 'y');
$this->db->group_start(); //start group
$this->db->like('description', $match);
$this->db->or_like('title', $match);
$this->db->or_like('body', $match);
$this->db->or_like('author', $match);
$this->db->group_end(); //close group
$query = $this->db->get('story_tbl');
return $query->result();

Searching with CDbCriteria

Is there any way to make a CDbCriteria search (as in compare()) in the fields I'm selecting, but using the model's search() method instead of having to manually add the compare() conditions?
Note that I'm aiming at a solution that will let me write some fewer lines, nothing more and nothing less. So, if the solution is something really hacky and/or mesy, I'll just go for the "add-a-few-compares()" method.
My current code:
$criteria = new CDbCriteria;
$criteria->with = array('A', 'B', 'C', 'D', 'E');
$criteria->compare("A.field1", "test", false, 'OR');
$criteria->compare("A.field2", "test", false, 'OR');
$criteria->compare("B.field1", "test", false, 'OR');
$criteria->compare("B.field2", "test", false, 'OR');
$dataProvider = new CActiveDataProvider('Z', array(
'criteria'=>$criteria,
//pagination...
//more options...
));
Update: It seems that you are actually looking(from comments below this answer) for partial matches, and for that you will have to pass true to your compare calls:
$criteria->compare("A.field1", "test", true, 'OR');
Even that can be passed to addCondition:
$criteria->addCondition('A.field1 LIKE "%test"','OR');
// or with params as below
$criteria->addCondition('A.field2 LIKE :test','OR');
$criteria->params=array(
':test'=>'%test%',
);
As i have already mentioned in the comments, i don't think it'll be possible to use each model's default search() method. There are other alternatives though, for instance you can use addCondition instead:
$criteria = new CDbCriteria;
$criteria->with = array('A', 'B', 'C', 'D', 'E');
$criteria->together = true; // you'll need together so that the other tables are joined in the same query
$criteria->addCondition('A.field1 = "test"','OR');
$criteria->addCondition('A.field2 = "test"','OR');
// and so on
I would suggest going with the above, because compare (doc-link) should actually be used in cases when you want to "intelligently" determine the operator for comparision, for example: if you are taking the test values from user input and the user is allowed to use operators (<,>,<= etc). After determining the operator to be used in the condition, compare calls other functions accordingly, including addCondition. So using addCondition will atleast avoid those unnecessary checks.
Further if all you have to do is check equality only, i.e if your sql's WHERE is supposed to be:
WHERE A.field1 = "test" OR A.field2 = "test"
then you don't even need addCondition, and you can simply use a more complex condition (doc) :
$criteria->condition='A.field1 = "test" OR A.field2 = "test"';
// or even better if you use params
$criteria->condition='A.field1 =:test1 OR A.field2 =:test2 OR B.field1 =:test3 OR B.field2 =:test3';
$criteria->params=array(
':test1'=>'test',
':test2'=>'anothertest',
'test3'=>'tests' // omitting ':' here for params also works
);