javascript - Dynamically setting the sortable property of a kendo grid column
问题描述
I'm trying to set a kendo grid column's sortable
property via a variable to control when the column can have sorting facility and when it can't.
But this is not working.
If I directly set the sortable
property to true
/false
it works accordingly, but when I'm using a variable to set it, it is not, regardless of the variable value, the property is always set to 'true'.
Example:
This works as expected.
<div id="grid"></div>
<script>
$("#grid").kendoGrid({
columns: [
{ sortable: false, field: "id" },
{ field: "name" }
],
sortable: true,
dataSource: [ { id: 1, name: "Jane Doe" }, { id: 2, name: "John Doe" } ]
});
</sript>
But this does not, id
field always gets sortable
property as true
<div id="grid"></div>
<script>
// if first time it's true, then the sortable property is retaining true always,
// regardless if on second call the variable is set to false. there is no effect
var setColumnSort = canBeFalseOrTrue;
$("#grid").kendoGrid({
columns: [
{ sortable: setColumnSort, field: "id" },
{ field: "name" }
],
sortable: true,
dataSource: [ { id: 1, name: "Jane Doe" }, { id: 2, name: "John Doe" } ]
});
</script>
Is there anyway to dynamically disable/enable sorting of a column in a kendo grid?
解决方案
You need to set it dynamically through grid properties once the grid is initialized.
$('#grid').kendoGrid({
sortable: true,
columns: [
{...}
]
});
var grid = $('#grid').getKendoGrid();
var options = grid.options;
options.columns[0].sortable = false;
grid.setOptions(options);
Example: Enable/disable sorting on column
推荐阅读
- kubernetes - 如何克隆 Kubernetes 部署?
- python - 枚举函数的Python解释
- c++ - “USBSerial”在 PlatformIO / ststm32 的 STM32F411CEU6 上没有成员“printf”
- javascript - 在 Rcrawler 包中使用 ContentScraper 时出错
- vue.js - 忽略此错误 ==> 客户端渲染的虚拟 DOM 树与服务器渲染的内容不匹配
- c - 如何在 C 中创建一个变量,其值将根据当前日期发生变化?
- c# - 如何使用 XUnit、Moq 和 AutoFixture 对控制器进行单元测试?
- angular - Angular Firebase“错误:Reference.update 失败:第一个参数包含属性'users.UID.email'中的未定义”
- wordpress - Wordpress 插件 add_settings_field 确切代码在一个实例中有效,但在第二个实例中无效?
- reactjs - React:在基于类的组件中获取初始状态