gpt4 book ai didi

html - polymer 造型元素

转载 作者:搜寻专家 更新时间:2023-10-31 08:55:25 24 4
gpt4 key购买 nike

目前我正在尝试将 SlickGrid 集成到 polymer 元素中,但在样式方面遇到了一些麻烦。

原始的 SlickGrid 示例具有非常原始的 css 样式,当我将它嵌入到 polymer 元素中时看起来不同。

没有 polymer 的代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SlickGrid example 1: Basic grid</title>
<style>
.slick-cell, .slick-headerrow-column {
position: absolute;
border: 1px solid transparent;
border-right: 1px dotted silver;
border-bottom-color: silver;
overflow: hidden;
-o-text-overflow: ellipsis;
text-overflow: ellipsis;
vertical-align: middle;
z-index: 1;
padding: 1px 2px 2px 1px;
margin: 0;
white-space: nowrap;
cursor: default;
}
</style>
</head>
<body>
<table width="100%">
<tr>
<td valign="top" width="50%">
<div id="myGrid" style="width:600px;height:500px;"></div>
</td>
<td valign="top">
<h2>Demonstrates:</h2>
<ul>
<li>basic grid with minimal configuration</li>
</ul>
</td>
</tr>
</table>

<script src="../lib/jquery-1.7.min.js"></script>
<script src="../lib/jquery.event.drag-2.2.js"></script>

<script src="../slick.core.js"></script>
<script src="../slick.grid.js"></script>

<script>
var grid;
var columns = [
{id: "title", name: "Title", field: "title"},
{id: "duration", name: "Duration", field: "duration"},
{id: "%", name: "% Complete", field: "percentComplete"},
{id: "start", name: "Start", field: "start"},
{id: "finish", name: "Finish", field: "finish"},
{id: "effort-driven", name: "Effort Driven", field: "effortDriven"}
];

var options = {
enableCellNavigation: true,
enableColumnReorder: false
};

$(function () {
var data = [];
for (var i = 0; i < 500; i++) {
data[i] = {
title: "Task " + i,
duration: "5 days",
percentComplete: Math.round(Math.random() * 100),
start: "01/01/2009",
finish: "01/05/2009",
effortDriven: (i % 5 == 0)
};
}

grid = new Slick.Grid("#myGrid", data, columns, options);
})
</script>
</body>
</html>

截图

original

polymer 元素代码:

<link rel="import" href="bower_components/polymer/polymer.html">


<polymer-element name="polymer-slickgrid">
<template>
<style>
.slick-cell, .slick-headerrow-column {
position: absolute;
border: 1px solid transparent;
border-right: 1px dotted silver;
border-bottom-color: silver;
overflow: hidden;
-o-text-overflow: ellipsis;
text-overflow: ellipsis;
vertical-align: middle;
z-index: 1;
padding: 1px 2px 2px 1px;
margin: 0;
white-space: nowrap;
cursor: default;
}

</style>
<div id="slickGrid" style="width:600px;height:500px;"></div>
</template>
<script src="bower_components/slickgrid/lib/jquery-1.7.min.js"></script>
<script src="bower_components/slickgrid/lib/jquery.event.drag-2.2.js"></script>

<script src="bower_components/slickgrid/slick.core.js"></script>
<script src="bower_components/slickgrid/slick.grid.js"></script>
<script>
Polymer('polymer-slickgrid', {
domReady: function(){
var grid;
var columns = [
{id: "title", name: "Title", field: "title"},
{id: "duration", name: "Duration", field: "duration"},
{id: "%", name: "% Complete", field: "percentComplete"},
{id: "start", name: "Start", field: "start"},
{id: "finish", name: "Finish", field: "finish"},
{id: "effort-driven", name: "Effort Driven", field: "effortDriven"}
];

var options = {
enableCellNavigation: true,
enableColumnReorder: false
};
var self = this;
$(function () {
var data = [];
for (var i = 0; i < 500; i++) {
data[i] = {
title: "Task " + i,
duration: "5 days",
percentComplete: Math.round(Math.random() * 100),
start: "01/01/2009",
finish: "01/05/2009",
effortDriven: (i % 5 == 0)
};
}
var element = self.$.slickGrid;
var grid = new Slick.Grid(element, data, columns, options);
})
}

});
</script>
</polymer-element>

元素的使用

<html>
<head lang="en">
<script src="bower_components/platform/platform.js"></script>
<link rel="import" href="polymer-slickgrid.html">
</head>
<body>
<polymer-slickgrid></polymer-slickgrid>
</body>
</html>

截图

polymer

编辑

似乎应用了单元格样式,但在 polymer 元素内使用时行具有不同的样式信息。

没有 polymer 的 CSS 截图

css with polymer

Polymer 元素内的 SlickGrid 截图

css without polymer

最佳答案

它看起来像你的 <polymer-slickgrid>元素显示为内联。看这里:http://www.polymer-project.org/articles/styling-elements.html#element-defined-styles

简单的解决方案:

<style>
:host {
/* Note: by default elements are always display:inline. */
display: block;
}
</style>

..或者您可以添加“ block ”属性,如下所示:<polymer-slickgrid block>

[更新]

看起来插件正在向文档添加内联样式 <head>这意味着这些样式不会应用于您的元素。

参见此处:https://github.com/mleibman/SlickGrid/blob/e6e2f88f832742c44e0fabf1f3864e5176386033/slick.grid.js#L921

作为 hacky hack 和概念证明,您可以获得这些样式并将它们内联到您的元素中:

var styles = [];
var styleSheets = window.document.styleSheets;
$.each(styleSheets, function(i, sheet) {
var sheet = styleSheets[i];
if (sheet.cssRules) {
$.each($.makeArray(sheet.cssRules), function(i, rule) {
if (rule.cssText && /slickgrid/.test(rule.cssText)) {
styles.push(rule.cssText);
}
})
}
});

var inlineStyles = '';
$.each(styles, function(i, style) {
inlineStyles += (style + "\n");
});
inlineStyles = '<style type="text/css">' + inlineStyles + '</style>';
$(this.$.slickGrid).before(inlineStyles);

我没有在很多浏览器中测试过这段代码,请不要使用它,它只是一个 POC。我在 var grid = new Slick.Grid(element, data, columns, options); 之后添加了该代码

这是网格在上面的 hack 中的样子,显示在自定义元素中(我还必须将 jquery ui 和 slick.grid 样式表添加到自定义元素中):

enter image description here

关于html - polymer 造型元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24549922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com