本人最近做了一个星星评价的东西,比较简单,要用AJAX,XML,JS,...这些东西。太让人头疼了 ~~~~
不过幸好有大家的帮助~我才度过了难关~~此处要说明的是,我自己创建了一个自定义字段,专门放星星的字段,这个代码就不写了
呈上代码~
有想要星星图片的可以Q我:290502186
首先,要先创建一个sharepoint空白项目~
在空白项目里创建三个映射文件夹
一个CONTROLTEMPLATES文件夹
另一个XML文件夹
最后是Layouts文件夹
------------------------------------------------------------------------------------------------------------------------------------
在CONTROLTEMPLATES文件夹里添加一个用户控件UserControl1.ascx(我取的名字是:StarsScoreControlTemplate.ascx)
在XML文件夹里添加一个XML文件(我取的名字是:fldtypes_StarsScore.xml)
此处要注意xml文件的名字必须要以“fldtypes_”开头
在StarsScoreControlTemplate.ascx页面里添加以下几行代码
<SharePoint:RenderingTemplate ID="starsscoreControlRenderingTemplate" runat="server">
<Template>
</Template>
</SharePoint:RenderingTemplate>
在xml里添加以下代码
<FieldTypes>
<FieldType>
<Field Name="TypeName">StarsScoreControl</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">星星评分</Field>
<Field Name="TypeShortDescription">星星评分</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowInColumnTemplateCreate">TRUE</Field>
<Field Name="CAMLRendering">TRUE</Field>
<Field Name="FieldTypeClass">SP_MIP.TechLibrary.StarsScoreControl,SP_MIP.TechLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=372913dabcbf4359</Field>
//SP_MIP.TechLibrary(命名空间). StarsScoreControl(类名),SP_MIP.TechLibrary(程序集)PublicKeyToken:密钥(在C:\Windows\assembly 找到你所创建的工程项目的名字,点击右键,属性)
<RenderPattern Name="DisplayPattern">
<HTML>
<![CDATA[<style type="text/css">
.star input
{
background: transparent url(/_LAYOUTS/image/star.png) no-repeat scroll right center;
border: 0 none;
cursor: pointer;
height: 16px;
width: 15px;
padding: 0;
vertical-align: middle;
}
</style>]]>
</HTML>
<HTML>
<![CDATA[ <script src="/_LAYOUTS/jquery.min.js" type="text/javascript" language="javascript"></script>]]>
</HTML>
<HTML>
<![CDATA[ <script type="text/javascript" language="javascript">
function DownStart]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[(scor)
{
var id=]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[; $.ajax({
type: "Post",
url: "/_LAYOUTS/SP_MIP/TechLibrary/StarsScoreSearchInfo.aspx/StarClick",
data: "{'sid':"+id+",'score':"+scor+"}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.d);
window.location.reload();
},
error: function (err) {
alert(err);
}
});
}
</script>]]>
</HTML>
<HTML>
<![CDATA[
<div class="star" id="QuacorGrading]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[">
<table>
<tr>
<td>
<input name="1" id="stat]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[" type="button" οnclick="DownStart]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[(1)" />
</td>
<td>
<input name="2" id="stat]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[" type="button" οnclick="DownStart]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[(2)" />
</td>
<td>
<input name="3" id="stat]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[" type="button" οnclick="DownStart]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[(3)" />
</td>
<td>
<input name="4" id="stat]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[" type="button" οnclick="DownStart]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[(4)" />
</td>
<td>
<input name="5" id="stat]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[" type="button" οnclick="DownStart]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[(5)" />
</td>
<td>
<asp:Label ID="Label]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[" runat="server" Text=""></asp:Label>
<span id='QuacorGradingValue]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[' style="display:none;" ><b><font size='5' color='#fd7d28'>]]>
</HTML>
<Column/>
<HTML>
<![CDATA[</font></b> </span>
</td>
<tr>
</table></div>]]>
</HTML>
<HTML>
<![CDATA[
<script type="text/javascript">
var GradList]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[ = document.getElementById("QuacorGrading]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[").getElementsByTagName("input");
for (var di = 0; di < parseInt(document.getElementById("QuacorGradingValue]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[").getElementsByTagName("font")[0].innerHTML); di++)
{ GradList]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[[di].style.backgroundPosition = 'left center'; }
for (var i = 0; i < GradList]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[.length; i++) {
GradList]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[[i].onmouseover = function () {
for (var Qi = 0; Qi < GradList]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[.length; Qi++) {
GradList]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[[Qi].style.backgroundPosition = 'right center';
}
for (var Qii = 0; Qii < this.name; Qii++) {
GradList]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[[Qii].style.backgroundPosition = 'left center';
}
//alert(GradList]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[.length);
document.getElementById("QuacorGradingValue]]>
</HTML>
<Field Name="ID"></Field>
<HTML>
<![CDATA[").innerHTML = '<b><font size="5" color="#fd7d28">' + this.name + '</font></b>';
}
}
</script>
]]>
</HTML>
</RenderPattern>
</FieldType>
</FieldTypes>
在Layouts文件夹里创建应用程序页面,此处写要逻辑代码(我取得名字是:StarsScoreSearchInfo.aspx)
先引用:using System.Web.Script.Services;
using System.Web.Services;
[WebMethod]
public static string StarClick(string sid, string score)
{
//SPSite site = new SPSite("网站地址");
//SPWeb web = site.RootWeb;
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
SPListCollection listcol = web.Lists;
//获取星星列表对象
SPList list = listcol["列表名"];
//根据ID查询一条信息
SPListItem spListitem = list.GetItemById(Convert.ToInt32(sid));
//获取登录者
//i:0#.f|spmipmp|admin
string loginname = web.CurrentUser.LoginName.ToString();
//截取字符串
loginname = loginname.Substring(loginname.LastIndexOf('|') + 1);
if (spListitem["人名字段"] == null)
{
spListitem["人名字段"] = loginname + ",";
}
else
{
//字段值用逗号分开存数组
string[] name = spListitem["人名字段"].ToString().TrimEnd(',').Split(',');
//循环比较判断
for (int i = 0; i < name.Length; i++)
{
if (loginname.Equals(name[i]))
{
//提示您已评过分
return "提示您已评过分";
}
}
//人名
spListitem["人名字段"] = spListitem["人名字段"].ToString() + loginname + ',';
}
//合计分数
if (spListitem["合计字段"] == null)
{
spListitem["合计字段"] = score;
}
else
{
spListitem["合计字段"] = Convert.ToInt32(spListitem["合计字段"]) + Convert.ToInt32(score);
}
spListitem.SystemUpdate();
SPListItem splistitem = list.GetItemById(Convert.ToInt32(sid));
string[] name1 = splistitem["人名字段"].ToString().TrimEnd(',').Split(',');
splistitem["星星字段"] = Math.Round(Convert.ToDecimal(splistitem["合计字段"]) / name1.Length, 2);
splistitem.SystemUpdate();
return "评分成功";
}
在创建一个类(我取的名字是:StarsScoreControl.cs),在StarsScoreControl类里添加以下代码
public class StarsScoreControl : SPField
{
public StarsScoreControl(SPFieldCollection fields, string fieldName)
: base(fields, fieldName) { }
public StarsScoreControl(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName) { }
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl ctr = new StarsScoreControlFieldControl();
ctr.FieldName = this.InternalName;
return ctr;
}
}
public class StarsScoreControlFieldControl : BaseFieldControl
{
protected override string DefaultTemplateName
{
get
{
return "starsscoreControlRenderingTemplate";
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
}
}
}