简体   繁体   English

如何将 true 或 false 的值更改为文本值类型 我正在检查整个表中的行 注意该表是数据表类型吗?

[英]How do I change the value of true or false to a text value type it I am checking the row in the entire table Note the table is of type datatables?

use django How do I change the value of true or false to a text value type it I am checking the row in the entire table使用 django 如何将 true 或 false 的值更改为文本值类型我正在检查整个表中的行

name item名称项目 type items类型项目 active积极的
item1项目 1 15.0 15.0 True真的
item2项目2 15.0 15.0 False错误的

change values row active use jquery or django file view.py:更改值行活动使用 jquery 或 django 文件 view.py:

name item名称项目 type items类型项目 active积极的
item1项目 1 15.0 15.0 displayed显示
item2项目2 15.0 15.0 stope采场

help please请帮忙

The most elegant way is likely to specify choices for the BooleanField , so:最优雅的方法可能是为BooleanField指定选择,因此:

from django.db import models

ACTIVE_CHOICES = [
    (False, 'stope')
  , (True, 'displayed')
  ]

class MyModel(models.Model):
    # …
    active = models.BooleanField(choices=ACTIVE_CHOICES)

Then you can render the corresponding label in a template with:然后你可以在模板中渲染相应的标签:

{{ mymodelobject.get_active_display }}

It will also use these choices by default in a ModelForm , ModelAdmin , and serializers.默认情况下,它还将在ModelFormModelAdmin和序列化程序中使用这些选项。

Without code, it is difficult to give a precise answer.没有代码,很难给出准确的答案。

but the easiest way to set value based on a boolean in javascript is to use a ternary operator.但是在javascript中基于布尔值设置值的最简单方法是使用三元运算符。

If true, active will be set to "displayed", if false, active will be set to "stope"如果为 true,active 将设置为“displayed”,如果为 false,active 将设置为“stope”

active = boolean_value ?活动 = 布尔值? "displayed" : "stope" “显示”:“采场”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取表单元格中的type输入值? - How do I get the value of type input inside a table cell? 如何从表格的最后一行获取输入类型文本的值 - How to get the value of input type text from the last row of a table 如何更改表中变量的值? - How do I change the value of a variable in a table? 如果有 null 值或特定值,如何隐藏表格行 - How do I hide a table row if there is a null value or a specific value 如何使用 PATCH 请求更改 true/false 值并在 React 中呈现更改? - How do I use a PATCH request to change a true/false value and render the change in React? 如果提交了某个值,如何更改输入类型 =“文本”中占位符文本的颜色? - How do i change the color of the placeholder text in an input type=“text” if a certain value is submitted? 我有一个来自html表的行:$(row).text()。 如何将其输入到input type =“ hidden”元素中 - I have a row from a html table in the form : $(row).text(). How do I feed this into a input type=“hidden” element 如何在选中时设置值 true,在未选中时设置 false - How do I set a value true on checked and false on unchecked 如何获取通过JQuery插入到表行中的下拉框的值/文本 - How do I get the value/text of a dropdown box that got inserted into a table row via JQuery 数据表:如何获取具有特定单元格值的行? - Datatables: How do I get the row with certain cell value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM