javascript - Unable to apply style to my grid after creating a div using javascript
问题描述
I am using the following grid. If you click on any of the cell of Link
column, a new grid below will be generated.
And whenever I try to add css style using the following line in the code document.getElementById("commonWindow").style.padding = "50px 10px 20px 30px";
below, it keeps on throwing error ("Uncaught TypeError: Cannot read property 'style' of null
). However, if you comment it out, it works fine. I am basically trying to add more space between the first grid and the newly created grid which gets created everytime I click on any of the cell value of Link column.
// prepare the data
var url = "https://www.jqwidgets.com/jquery-widgets-demo/demos/sampledata/feed.xml";
var source =
{
datatype: "xml",
datafields: [
{ name: 'title', type: 'string' },
{ name: 'link', type: 'string' },
{ name: 'pubDate', type: 'date' },
{ name: 'creator', map: 'dc\\:creator', type: 'string' },
],
root: "channel",
record: "item",
url: url
};
var linkrenderer = function (row, column, value) {
if (value.indexOf('#') != -1) {
value = value.substring(0, value.indexOf('#'));
}
var format = { target: '"_blank"' };
var html = $.jqx.dataFormat.formatlink(value, format);
//console.log(html)
return html;
}
var dataAdapter = new $.jqx.dataAdapter(source);
var cellsrenderer = function (row, column, value) {
var currentRowData = $("#jqxgrid").jqxGrid('getrowdata', row);
var rowDataLink = currentRowData["link"];
return "<a href =" + rowDataLink + ">" + value + "</a>";
};
// Create jqxGrid.
$("#jqxgrid").jqxGrid({
width: 850,
source: dataAdapter,
pageable: true,
autoheight: true,
sortable: true,
columns: [
{ text: 'Link', datafield: 'link', width: 550},
{ text: 'Title', datafield: 'title', width: 200, cellsrenderer: cellsrenderer },
{ text: 'Publish Date', datafield: 'pubDate', width: 250, cellsformat: "D" },
{ text: 'Creator', datafield: 'creator', width: 200 }
]
});
$("#jqxgrid").on("rowselect", function (e) {
let link = $("#jqxgrid").jqxGrid('getcell', e.args.rowindex, 'link');
console.log(link.value);
$('#commonWindow').remove();
var elem = document.createElement('div');
elem.id = 'commonWindow';
document.getElementById("commonWindow").style.padding = "50px 10px 20px 30px";
$(elem).jqxGrid({
source: dataAdapter, columns: [
{ text: 'Link', datafield: 'link', width: 550 },
{ text: 'Title', datafield: 'title', width: 200, cellsrenderer: cellsrenderer },
{ text: 'Publish Date', datafield: 'pubDate', width: 250, cellsformat: "D" },
{ text: 'Creator', datafield: 'creator', width: 200 }
] });
document.body.appendChild(elem);
});
<div id="jqxgrid">
</div>
<link href="https://jqwidgets.com/public/jqwidgets/styles/jqx.base.css" rel="stylesheet"/>
<script src="https://jqwidgets.com/public/jqwidgets/jqx-all.js"></script>
解决方案
You are searching for an element with id commonWindow
in document before putting it there. It's not there yet so you are getting null
.
Also when you already have reference to the element in the variable elem
, you can set style directly to it. You don't need to search document for it.
$("#jqxgrid").on("rowselect", function (e) {
let link = $("#jqxgrid").jqxGrid('getcell', e.args.rowindex, 'link');
console.log(link.value);
$('#commonWindow').remove();
var elem = document.createElement('div');
elem.id = 'commonWindow';
document.body.appendChild(elem);
elem.style.padding = "50px 10px 20px 30px";
$(elem).jqxGrid({
source: dataAdapter,
columns: [
{ text: 'Link', datafield: 'link', width: 550 },
{ text: 'Title', datafield: 'title', width: 200, cellsrenderer: cellsrenderer },
{ text: 'Publish Date', datafield: 'pubDate', width: 250, cellsformat: "D" },
{ text: 'Creator', datafield: 'creator', width: 200 }
]
});
});
推荐阅读
- javascript - 当输入字段的值与绑定的“数据”不匹配时,Vue 会做什么?
- c# - C# 中的图标覆盖(来自 Winform 项目)
- datatable - 数据表列输入值
- c# - 无法将任何消息检索到 Sentry 页面
- android - 有没有办法从客户端禁用 FCM 主题订阅,仅使用服务器端主题订阅?
- javascript - 承诺在减少数组方法javascript中没有得到解决
- samba - 如何使用用户/密码在独立的 linux 工作站中设置远程共享
- c# - Windows 窗体秒表经过时间
- android - GraphQL 突变在更新时返回 null
- azure - VNET 中的 Azure 功能仍然需要功能访问密钥吗?